1、配置pom.xml
<!-- mybatis generator 自动生成代码插件 -->
<!--generator 实现model、mapper等自动生成-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--generatorConfig.xml路径--> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
</plugin>
2、generatorConfig.xml配置文件
<?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>
<properties resource="application-dev.properties" />
<classPathEntry location="${generator.location}" />
<context id="Tables" targetRuntime="MyBatis3">
<!-- 注释 -->
<commentGenerator>
<!-- 是否生成注释代时间戳 -->
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- JDBC连接 -->
<jdbcConnection
driverClass="${generator.datasource.driver-class-name}"
connectionURL="${generator.datasource.url}"
userId="${generator.datasource.username}"
password="${generator.datasource.password}">
</jdbcConnection>
<!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="com.generator.page.model" targetProject="src/main/java">
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaModelGenerator>
<!-- 生成mapper xml文件 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!--生成mapper xml对应Client-->
<javaClientGenerator targetPackage="com.generator.page.dao" targetProject="src/main/java" type="XMLMAPPER">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 配置表信息 -->
<!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
是否生成 example类 -->
<table schema="sql_test" tableName="member"
domainObjectName="member" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
</table>
<!--<table schema="ysaas" tableName="b_txlog"-->
<!--domainObjectName="TXLog" enableCountByExample="false"-->
<!--enableDeleteByExample="false" enableSelectByExample="false"-->
<!--enableUpdateByExample="false">-->
<!--</table>-->
</context>
</generatorConfiguration>
3、配置generatorConfig.xml配置文件中引用的值
注意:generatorConfig.xml文件无法引用.yml文件,所以单独配置在application-dev.properies文件中,而不是引用数据库连接的参数
generator.location = D:/mavenRepository/mysql/mysql-connector-java/8.0.15/mysql-connector-java-8.0.15.jar
generator.datasource.url=jdbc:mysql://127.0.0.1:3306/sql_test?useSSL=false&allowMultiQueries=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
generator.datasource.username=root
generator.datasource.password=123456
generator.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
4、测试 运行
运行结果