十九、Spring boot中整合mybatis-generator自动生成代码
(一)添加插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!--配置自动生成工具的配置文件所在的位置-->
<configurationFile>${basedir}/src/main/resources/mybatis/mybatis-generator-config.xml</configurationFile>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
(二)配置自动生成工具
<?xml version="1.0" encoding="UTF-8"?>
<!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="tomcatTables">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/tomcat"
userId="root"
password="root"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--Java模型对象生成器-->
<javaModelGenerator targetPackage="org.pc.entity"
targetProject="${user.dir}/src/main/java"/>
<!--Sql Mapper XML生成器-->
<sqlMapGenerator targetPackage="/mybatis/mappers"
targetProject="${user.dir}/src/main/resources"/>
<!--Sql Map对应的Java接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="org.pc.dao"
targetProject="${user.dir}/src/main/java">
</javaClientGenerator>
<!--配置表及其映射 domainObjectName是实体类名-->
<table tableName="user" domainObjectName="User"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
<!-- sqlStatement:什么类型的数据库;identity:是不是组件-->
<generatedKey column="id" sqlStatement="mysql" identity="true"/>
<columnOverride column="name" property="name"/>
<columnOverride column="age" property="age"/>
</table>
</context>
</generatorConfiguration>
(三)启动自动生成工具插件