1、依赖导入
- 依赖
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
- 插件
<plugin>
<!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
2、编写mybatis-generator.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>
<!-- 此处location是maven本地仓库存放mysql jar包的路径 -->
<classPathEntry location="D:\Maven\apache-maven-3.6.0\localRepository\mysql\mysql-connector-java\8.0.26\mysql-connector-java-8.0.26.jar" />
<context id="mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`" />
<property name="endingDelimiter" value="`" />
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<!-- 数据库连接配置 -->
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/***?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8"
userId="***" password="***">
<!--MySQL 8.x 需要指定服务器的时区-->
<property name="serverTimezone" value="UTC"/>
<!--MySQL 不支持 schema 或者 catalog 所以需要添加这个-->
<!--参考 : MyBatis Generator Core – MySql Usage Notes-->
<property name="nullCatalogMeansCurrent" value="true"/>
<!-- MySQL8默认启用 SSL ,不关闭会有警告-->
<property name="useSSL" value="false"/>
</jdbcConnection>
<!-- 实体类生成位置 -->
<javaModelGenerator targetPackage="com.test.generator.pojo" targetProject="D:\Idea_work\zookeeper-study\poi\src\main\java" />
<!-- mapper -->
<!-- <sqlMapGenerator targetPackage="mybatis.mapper" targetProject="D:\Idea_work\zookeeper-study\poi\src\main\resources" />-->
<!-- dao -->
<javaClientGenerator targetPackage="com.test.generator.dao" targetProject="D:\Idea_work\zookeeper-study\poi\src\main\java" type="ANNOTATEDMAPPER" />
<table tableName="project" domainObjectName="Project"
enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
selectByExampleQueryId="true">
<property name="useActualColumnNames" value="false" />
</table>
<table tableName="transmissionsystem" domainObjectName="TransmissionSystem"
enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
selectByExampleQueryId="true">
<property name="useActualColumnNames" value="false" />
</table>
</context>
</generatorConfiguration>
3、运行
新建maven执行器,按如下配置:
4、使用注意事项:
- 每次执行生成器都会重新生成文件,如果我们在生成的接口中接着写其他接口,下一次生成的文件并不会保留我们自己写的代码。所以接下来介绍两种平时工作中的做法:
- 我们在生成完一张表的代码后,就将关于这张表的配置代码注释或者删除,这样我们就可以在生成的文件中编写自己的代码了,不用担心代码被覆盖了。
- 我们可以让自定义的接口继承生成的接口,并在自定义接口中编写自己的代码,在使用时只需注入自定义接口即可。
- 关于生成的实体类属性名:
当表中字段名以下划线分割多个单词时,生成的实体类属性是驼峰形式的;
当表中字段名是驼峰形式的,则生成的实体类属性名都是小写字母。
所以推荐以后表中的字段名都以下划线来分割多个单词。
5、参考:
入门参考: Mybatis-generator自动生成器 - 蜀云泉 - 博客园
更详细配置参考: Mybatis代码生成器Mybatis-Generator使用详解 - throwable - 博客园