文章目录
SpringBoot 中使用 MyBatis Generator 生成相关的实体类及Mapper
添加 generator 的 Maven 插件
项目使用的是 SpringBoot 框架,使用 Maven 来作为构建工具,于是使用 Maven 插件来对 MyBatis Generator 进行配置。根据官方文档,在 pom.xml 中添加如下插件代码
<project ...>
...
<build>
...
<plugins>
...
<!--mybatis generator 插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
</dependencies>
</plugin>
...
</plugins>
...
</build>
...
</project>
因为连接的是 MySQL 数据库,需要添加 MySQL 数据库驱动,在 <plugin>
中添加驱动的 Maven 依赖即可。
添加 generatorConfig.xml 文件
Mybatis Generator 默认在项目根目录下寻找 generatorConfig.xml 作为代码生成的配置文件,可在 src/main/resources/
下添加 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>
<!--<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/dx?characterEncoding=utf8"
userId="root"
password="12345678">
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.dx.ibatis.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.dx.ibatis.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="USER" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" ></table>
</context>
</generatorConfiguration>
注意事项
targetProject
targetProject 应该为目标文件所在的目录,与 maven 命令执行时所在目录的相对路径。Maven 插件在 IDEA 中运行的目录即为项目所在的根目录,因此生成的类文件在SpringBoot 项目中,应该在 src/main/java
路径下的包中,mapper 文件可以放在 src/main/resources
路径的文件夹中。
因此 <javaModelGenerator>
和 <javaClientGenerator>
都指定了 targetProject 为 src/main/java
,再根据 targetPackage 存放到不同的包中;<sqlMapGenerator>
指定了 targetProject 为 src/main/resources
,再根据 targetPackage 存放到不同的文件夹中。
UserExample
Mybatis Generator 默认生成实体类对应的 xxxExample 类,用于构造复杂的筛选条件。一般开发中会去掉这些 Example 类,在 <table>
标签中添加属性 enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"
,即可阻止生成此类。
UserKey 和 UserWithBlobs
对于 MySQL 数据库,在生成 User 实体类时,还会同时生成 Userkey 和 UserWithBlobs 实体类,原因在于 MySQL 不支持 catalogs 和 schema。在 MySQL 8.0 以上的版本中,在生成 User 表的实体类时,Mybatis Generator 会找到多张 User 表,包括 MySQL information schemas 中的多张 User 表,生成了多个 User 相关的实体类。
要保证只生成自己指定的 database 中的 User 表,首先要在 <jdbcConnection>
中的 connectionURL 中指定数据库的实例名,然后在 <jdbcConnection>
中添加相关配置信息,即 <property name="nullCatalogMeansCurrent" value="true"/>
,即可保证只生成自己需要的 User 类。
参考文档:
http://www.mybatis.org/generator/running/runningWithMaven.html
http://www.mybatis.org/generator/usage/mysql.html