通用 Mapper 在 1.0.0 版本的时候增加了 MyBatis Generator (以下简称 MBG) 插件,使用该插件可以很方便的生成实体类、Mapper 接口以及对应的 XML 文件。
下面介绍了 mybatis-generator 在 spring-boot 中的使用过程
一、引入pom依赖
<dependencies><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.7</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.19</version></dependency></dependencies>
(这里我使用了pagehelper-spring-boot-starter,因为用到了分页查询,它里面有mybatis的相关依赖,下一篇文章再介绍pagehelper的使用)
二、Mybatis-Generator 的 maven 插件
在 pom.xml 的插件列表中加入 mybatis-generator 的 maven 插件,这样就可以生成实体类、Mapper 接口以及对应的 XML 文件。
<build><plugins><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.4.0</version><configuration><!--mybatis的代码生成器的配置文件--><configurationFile>src/main/resources/generator-configuration.xml</configurationFile><!--允许覆盖生成的文件--><!--有时候我们的数据库表添加了新字段,需要重新生成对应的文件。常规做法是手动删除旧文件,然后在用 MyBatis Generator 生成新文件。当然你也可以选择让 MyBatis Generator 覆盖旧文件,省下手动删除的步骤。--><!--值得注意的是,MyBatis Generator只会覆盖旧的 po、dao、而 *mapper.xml 不会覆盖,而是追加,这样做的目的是防止用户自己写的 sql 语句一不小心都被 MyBatis Generator 给覆盖了--><overwrite>true</overwrite><verbose>true</verbose><!--将当前pom的依赖项添加到生成器的类路径中--><!--<includeCompileDependencies>true</includeCompileDependencies>--></configuration><dependencies><!-- mysql的JDBC驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.19</version></dependency><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.4.0</version></dependency></dependencies></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
三、Mybatis-Generator 配置
上面配置的路径指向
src/main/resources/generator-configuration.xml,我们在这个文件中加入配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration><!-- 引入配置文件 --><!--<properties resource="jdbc.properties"></properties>--><!-- 目标数据库 --><!-- 一个数据库一个context, context子元素必须按照如下顺序property*、plugin*、commentGenerator?、jdbcConnection、javaTypeResolver?javaModelGenerator、sqlMapGenerator?、javaClientGenerator?、table+--><!--id : 随便填,保证多个 context id 不重复就行defaultModelType : 可以不填,默认值 conditional,flat表示一张表对应一个potargetRuntime :可以不填,默认值 MyBatis3,常用的还有 MyBatis3Simple,这个配置会影响生成的 dao 和 mapper.xml的内容targetRuntime = MyBatis3Simple,生成的 dao 和 mapper.xml,接口方法会少很多,只包含最最常用的--><context id="myContext" targetRuntime="MyBatis3"><property name="javaFileEncoding" value="UTF-8"/><!-- 生成的pojo,将implements Serializable --><plugin type="org.mybatis.generator.plugins.SerializablePlugin" /><!-- 为生成的pojo创建一个toString方法 --><plugin type="org.mybatis.generator.plugins.ToStringPlugin"/><!-- 生成的pojo,增加了equals 和 hashCode方法--><plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" /><!--生成mapper.xml时覆盖原文件--><plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" /><!-- 自定义注释 --><commentGenerator><!-- 是否去除自动生成的注释 true:是 : false:否 --><property name="suppressAllComments" value="true"/><property name="suppressDate" value="false"/><!--添加 db 表中字段的注释--><property name="addRemarkComments" value="true"/></commentGenerator><!-- 是否去除自动生成的注释 true:是 : false:否 --><!--<commentGenerator><property name="suppressAllComments" value="false" /></commentGenerator>--><!--数据库连接信息:驱动类、链接地址、用户名、密码 --><jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/database?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai"userId="root"password="password"><!--高版本的 mysql-connector-java 需要设置 nullCatalogMeansCurrent=true--><!--解决mysql驱动升级到8.0后不生成指定数据库代码的问题--><property name="nullCatalogMeansCurrent" value="true"/></jdbcConnection><javaTypeResolver><!--类型解析器--><!-- 默认false,把jdbc decimal 和 numeric 类型解析为integer --><!-- true,把jdbc decimal 和 numeric 类型解析为java.math.bigdecimal--><property name="forceBigDecimals" value="false"/><!--默认falsefalse,将所有 JDBC 的时间类型解析为 java.util.Datetrue,将 JDBC 的时间类型按如下规则解析DATE -> java.time.LocalDateTIME -> java.time.LocalTimeTIMESTAMP -> java.time.LocalDateTimeTIME_WITH_TIMEZONE -> java.time.OffsetTimeTIMESTAMP_WITH_TIMEZONE -> java.time.OffsetDateTime--><property name="useJSR310Types" value="false"/></javaTypeResolver><!-- java实体类路径 --><javaModelGenerator targetPackage="com.bjrhc.boot.pojo" targetProject="src/main/java"><!-- 是否让schema作为包后缀 默认是false会在 po 目录下在创建一个 “数据库名” 的文件夹,生成的 po 会放在该文件夹下,也就是说会多一层目录--><property name="enableSubPackages" value="false"/><!-- 从数据库返回的值被清理前后的空格--><property name="trimStrings" value="true"/></javaModelGenerator><!-- 生成映射文件xml的包名和位置--><sqlMapGenerator targetPackage="com.hhh.boot.mapper" targetProject="src/main/resources"><!-- 是否让schema作为包后缀--><property name="enableSubPackages" value="false"/></sqlMapGenerator><!-- 生成Mapper接口的包名和位置type="XMLMAPPER" 会将接口的实现放在 mapper.xml中,也推荐这样配置。type="ANNOTATEDMAPPER",接口的实现通过注解写在接口上面--><javaClientGenerator type="XMLMAPPER" targetPackage="com.hhh.boot.mapper"targetProject="src/main/java"><!-- 是否让schema作为包后缀--><property name="enableSubPackages" value="false"/></javaClientGenerator><!-- 用于自动生成代码的数据库表;生成哪些表;schema为数据库名,oracle需要配置,mysql不需要配置。tableName为对应的数据库表名domainObjectName 是要生成的实体类名(可以不指定)(其中 domainObjectName 不配置时,它会按照帕斯卡命名法将表名转换成类名)enableXXXByExample 默认为 true, 为 true 会生成一个对应Example帮助类,帮助你进行条件查询,不想要可以设为false生成全部表tableName设为 %--><table tableName="%"></table><table schema="table" tableName="tableName" domainObjectName="Alar" enableCountByExample="true"enableDeleteByExample="true" enableSelectByExample="true"enableUpdateByExample="true"></table></context>
</generatorConfiguration>
然后将配置文件里的信息修改为自己需要的
四、点击按钮
在右侧插件中找到这个,点一下,看到控制台出现build-success就生成好了