SpringBoot整合Mybatis Generator
Mybatis Generator官方文档地址:http://mybatis.org/generator/quickstart.html
1. pom.xml文件引入依赖
<build>
<plugins>
<!-- mybatis-generator Mybatis代码自动生成插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/mybatis-generator/generatorConfig.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
2. 编写Mybatis Generator的配置文件
2.1 文件位置
2.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="mybatis-generator/mybatisGeneratorinit.properties"/>
<!-- 连接数据库jar包的路径-->
<classPathEntry location="E:\maven_repo\repository\mysql\mysql-connector-java\8.0.27\mysql-connector-java-8.0.27.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接参数 -->
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="${jdbc_driver}" connectionURL="${jdbc_url}" userId="${jdbc_user}" password="${jdbc_password}" >
<property name="serverTimezone" value="Asia/Shanghai"/>
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.xiqing.study.project.user.model.po" targetProject="${po}">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="mapper" targetProject="${resources}">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成mapper的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.xiqing.study.project.user.mapper" targetProject="${mapper}">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- tableName:数据库中的表名或视图名;domainObjectName:生成的实体类的类名-->
<table tableName="user" domainObjectName="People"
enableCountByExample="true"
enableUpdateByExample="true"
enableDeleteByExample="true"
enableSelectByExample="true"
selectByExampleQueryId="true">
<property name="useActualColumnNames" value="false" />
<!-- 数据库表主键 -->
<generatedKey column="id" sqlStatement="Mysql" identity="true" />
</table>
</context>
</generatorConfiguration>
2.3 mybatisGeneratorinit.properties文件内容
#Mybatis Generator configuration
# 生成代码的文件路径
po=E:/MyCode/xiqing-study-project/xiqing-study-project-user/xiqing-study-project-user-api/src/main/java/
mapper=E:/MyCode/xiqing-study-project/xiqing-study-project-user/xiqing-study-project-user-provider/src/main/java/
resources=E:/MyCode/xiqing-study-project/xiqing-study-project-user/xiqing-study-project-user-provider/src/main/resources/
jdbc_driver =com.mysql.cj.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3307/test01?useUnicode=true&characterEncoding=UTF-8&useSSL=false&rewriteBatchedStatements=true
jdbc_user=root
jdbc_password=123456