Mybatis****创建方法
****的有点是可以省去写pojo实体类和mapper接口工作,下面介绍一下在Spring Boot中如何创建****。
1.首先,创建好Spring Boot工程后,应在pom文件中导入相关依赖。导入后自动下载jar包。
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
2.之后我们手动创建一个全局配置文件mybatis-config.xml,这个名字不固定。
我们在这里面给它做一些配置:
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="test" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 抑制警告 -->
<property name="suppressTypeWarnings" value="true" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false" />
<!-- 是否生成注释代时间戳-->
<property name="suppressDate" value="true" />
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/demo" userId="root" password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="com.springboot.dao.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.springboot.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<!--生成表之前,注意检查该表是否已经在工程中存在。
如果存在,不要再次生成!会覆盖原有文件!生成过的表要注释或者删除 -->
<table tableName="user" domainObjectName="UserEntity" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
将数据库名字改为自己的数据库名,这里我的是叫demo,账号密码填写自己的MySQL账号密码。
还要设置Model类、以及映射文件存放的目录。
最下面的table用于关联数据库中的表,tableName中填写表名。
3。点击右侧的Maven Projects,点击运行。
如图所示:
代表运行成功!
4.查看工程目录,会发现多出了三个文件。
创建成功。
如有问题,欢迎一起讨论~
桃李不言,下自成蹊