想快速开始,请直接拉到最后,看总体配置。html
MyBatis Generator 是 MyBatis 提供的一个代码生成工具。能够帮咱们生成 表对应的持久化对象(po)、操做数据库的接口(dao)、CRUD sql的xml(mapper)。java
MyBatis Generator 是一个独立工具,你能够下载它的jar包来运行、也能够在 Ant 和 maven 运行。mysql
我是在 maven 中配置并使用的。这篇文章也是基于 maven 环境来说解。spring
既然使用了 MyBatis Generator ,那么你的项目必定使用了 MyBatis, 而且必定使用了某一种数据库,而且这些依赖应该已经在 maven 中配置好了。sql
例如 数据库
接下来须要在 pom 中引入 MyBatis Generator 插件bash
在 pom 的根节点下添加如下配置mybatis
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
</plugin>
<plugins>
</build>
复制代码
光引入 MyBatis Generator 插件还不行,还得配置 MyBatis Generator插件oracle
MyBatis Generator 插件须要根据一个 MyBatis Generator config 文件,来具体运行app
配置以下,版本我用的是目前最新的版本 1.3.7
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!--mybatis的代码生成器的配置文件-->
<configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
</configuration>
</plugin>
<plugins>
</build>
复制代码
注意,这个路径是你的配置文件相对于该 pom 文件的路径
至于这个文件该怎么配置咱们待会在
有时候咱们的数据库表添加了新字段,须要从新生成对应的文件。常规作法是手动删除旧文件,而后在用 MyBatis Generator 生成新文件。固然你也能够选择让 MyBatis Generator 覆盖旧文件,省下手动删除的步骤。
配置以下
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!--mybatis的代码生成器的配置文件-->
<configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
<!--容许覆盖生成的文件-->
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugins>
</build>
复制代码
值得注意的是,MyBatis Generator 只会覆盖旧的 po、dao、而 *mapper.xml 不会覆盖,而是追加,这样作的目的是防止用户本身写的 sql 语句一不当心都被 MyBatis Generator 给覆盖了
MyBatis Generator 须要连接数据库,确定是须要对应数据库驱动的依赖的。
以下,给 MyBatis Generator 添加数据库驱动依赖
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!--mybatis的代码生成器的配置文件-->
<configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
<!--容许覆盖生成的文件-->
<overwrite>true</overwrite>
</configuration>
<dependencies>
<!-- mysql的JDBC驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
</dependencies>
</plugin>
<plugins>
</build>
复制代码
我用的数据库是 mysql ,其余数据库同理。注意数据库驱动的版本号,不一样的版本对应的 MyBatis Generator 配置有些许不一样,以后会讲。
大部分状况下,咱们的项目中已经配置过了对应数据库的JDBC驱动,以下
如今在插件中又配置一次,感受有些冗余,maven 提供了 includeCompileDependencies 属性,让咱们在插件中引用 dependencies 的依赖,这样就不须要重复配置了。
配置以下
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!--mybatis的代码生成器的配置文件-->
<configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
<!--容许覆盖生成的文件-->
<overwrite>true</overwrite>
<!--将当前pom的依赖项添加到生成器的类路径中-->
<includeCompileDependencies>true</includeCompileDependencies>
</configuration>
</plugin>
<plugins>
</build>
复制代码
通常配置了 includeCompileDependencies 后就不须要配置其余依赖了,由于 includeCompileDependencies 会将当前 pom 的 dependencies 中因此 Compile 期的依赖所有添加到生成器的类路径中。
但有的人不想配置 includeCompileDependencies ,或者想在MyBatis Generator插件中使用另外一个版本的依赖,就能够配置 dependencies
如图
另外,我看到网上大部分文章都会配置 mybatis-generator-core 这个依赖,可是 MyBatis Generator 官网的案例中都没有提到说要配置这个依赖,我没有配置,而且能够正常使用 MyBatis Generator
MyBatis Generator 插件启动后,会根据你在 pom 中配置都路径找到该配置文件。
这个配置文件才是详细都配置 MyBatis Generator 生成代码的各类细节。
其中最重要的就是 context ,你的配置文件至少得包含一个context
MyBatis Generator config 是能够引入外部配置文件的,以下,路径为相对于当前配置文件的路径
代码以下,注意是配置在 <generatorConfiguration>
下
<!-- 引入配置文件 -->
<properties resource="application-dev.properties"/>
复制代码
配置文件中的内容以下
以后能够经过 ${xxx} 来引用外部配置文件中的值注意是配置在 <generatorConfiguration>
下
<context id="myContext" targetRuntime="MyBatis3" defaultModelType="flat">
</context>
复制代码
targetRuntime = MyBatis3,生成的 dao 和 mapper.xml 以下
targetRuntime = MyBatis3Simple,生成的 dao 和 mapper.xml 以下,接口会少不少,只包含最最经常使用的
context 节点就讲完了,惟一须要注意的就是targetRuntime的值,该配置成什么看我的喜爱
上一节只是配置了 context 节点, context 里面还有子元素须要配置。
context的子元素必须按照如下给出的个数、顺序配置。(是的,没错 MyBatis Generator 对配置的循序还有要求)
配置一个插件,例如
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
复制代码
这个插件给生成的Java模型对象增长了equals和hashCode方法
commentGenerator 用来配置生成的注释。默认是生成注释的,而且会生成时间戳,以下
若是你想要保留注释和时间戳,能够不配置 commentGenerator。若是你不想保留时间戳,须要以下配置
<commentGenerator>
<!-- 不但愿生成的注释中包含时间戳 -->
<property name="suppressDate" value="true"/>
</commentGenerator>
复制代码
默认生成的注释是不会有 db 表中字段的注释,若是你想知道每一个字段在数据库中的含义(前提是数据库中对应表的字段你添加了注释),能够以下配置
<commentGenerator>
<!-- 添加 db 表中字段的注释 -->
<property name="addRemarkComments" value="true"/>
</commentGenerator>
复制代码
但说实话,MyBatis Generator 生成注释无用信息太多了,因此我通常都选择不生成注释
<commentGenerator>
<!-- 是否不生成注释 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
复制代码
MyBatis Generator 须要连接数据库,因此须要配置 jdbcConnection,具体以下
<jdbcConnection driverClass="${spring.datasource.driverClassName}"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}">
<!--高版本的 mysql-connector-java 须要设置 nullCatalogMeansCurrent=true-->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
复制代码
${}里面是外部配置文件中的"name"
你也能够写死,那就不须要配置<properties resource="application-dev.properties"/>
了
这里面值得注意的是<property name="nullCatalogMeansCurrent" value="true"/>
,由于我用的 mysql-connector-java 版本是 8.0.17,若是配置这一项,会找不到对应的数据库,官网对此的解释是
具体缘由参考这篇文章 MyBatis Generator踩坑与自救
javaTypeResolver 是配置 JDBC 与 java 的类型转换规则,或者你也能够不用配置,使用它默认的转换规则。
就算配置也只能配置 bigDecimal 类型和时间类型的转换
<javaTypeResolver>
<!--是否使用 bigDecimal,默认false。
false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal-->
<property name="forceBigDecimals" value="true"/>
<!--默认false
false,将全部 JDBC 的时间类型解析为 java.util.Date
true,将 JDBC 的时间类型按以下规则解析
DATE -> java.time.LocalDate
TIME -> java.time.LocalTime
TIMESTAMP -> java.time.LocalDateTime
TIME_WITH_TIMEZONE -> java.time.OffsetTime
TIMESTAMP_WITH_TIMEZONE -> java.time.OffsetDateTime
-->
<property name="useJSR310Types" value="true"/>
</javaTypeResolver>
复制代码
配置 po 生成的包路径和项目路径,以下
<javaModelGenerator targetPackage="com.wqlm.boot.user.po" targetProject="src/main/java">
<!-- 是否让schema做为包的后缀,默认为false -->
<!--<property name="enableSubPackages" value="false"/>-->
<!-- 是否针对string类型的字段在set方法中进行修剪,默认false -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
复制代码
<property name="trimStrings" value="true"/>
生成出来的 set 方法以下
<property name="enableSubPackages" value="true"/>
时,可能会在 po 目录下在建立一个 “数据库名”
的文件夹,生成的 po 会放在该文件夹下,也就是说会多一层目录,用的上的能够配置
配置 Mapper.xml 文件的生成目录
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<!--<property name="enableSubPackages" value="false"/>-->
</sqlMapGenerator>
复制代码
配置 XxxMapper.java 文件的生成目录
<javaClientGenerator targetPackage="com.wqlm.boot.user.dao" targetProject="src/main/java" type="XMLMAPPER">
<!--<property name="enableSubPackages" value="false"/>-->
</javaClientGenerator>
复制代码
type="XMLMAPPER"
会将接口的实现放在 mapper.xml中,也推荐这样配置。也能够设置 type 为其余值,好比 type="ANNOTATEDMAPPER"
,接口的实现经过注解写在接口上面,如图
若是采用这种方式,不会生成 mapper.xml 也不用配置 <sqlMapGenerator>
,可是采用注解来实现接口应对简单查询还好,若是是复杂查询并不如xml方便,因此仍是建议将type
配置成XMLMAPPER
一个 table 对应一张表,若是想同时生成多张表,须要配置多个 table
<!-- schema为数据库名,oracle须要配置,mysql不须要配置。
tableName为对应的数据库表名
domainObjectName 是要生成的实体类名(能够不指定)
enableXXXByExample 默认为 true, 为 true 会生成一个对应Example帮助类,帮助你进行条件查询,不想要能够设为false
-->
<table schema="" tableName="user" domainObjectName="User"
enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
<!--是否使用实际列名,默认为false-->
<!--<property name="useActualColumnNames" value="false" />-->
</table>
复制代码
其中 domainObjectName 不配置时,它会按照帕斯卡命名法将表名转换成类名
enableXXXByExample 默认为true,但只有在targetRuntime="MyBatis3"
时才生效
生效时,会在po下多生成一个 XxxExample.java
的文件,以下
当 targetRuntime="MyBatis3Simple"
时,enableXXXByExample 无论为true、仍是false 都不生效
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!--mybatis的代码生成器的配置文件-->
<configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
<!--容许覆盖生成的文件-->
<overwrite>true</overwrite>
<!--将当前pom的依赖项添加到生成器的类路径中-->
<!--<includeCompileDependencies>true</includeCompileDependencies>-->
</configuration>
<dependencies>
<!--mybatis-generator插件的依赖包-->
<!--<dependency>-->
<!--<groupId>org.mybatis.generator</groupId>-->
<!--<artifactId>mybatis-generator-core</artifactId>-->
<!--<version>1.3.7</version>-->
<!--</dependency>-->
<!-- mysql的JDBC驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<build>
复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!--mybatis的代码生成器相关配置-->
<!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"/>
<!-- 一个数据库一个context,context的子元素必须按照它给出的顺序
property*,plugin*,commentGenerator?,jdbcConnection,javaTypeResolver?,
javaModelGenerator,sqlMapGenerator?,javaClientGenerator?,table+
-->
<context id="myContext" targetRuntime="MyBatis3" defaultModelType="flat">
<!-- 这个插件给生成的Java模型对象增长了equals和hashCode方法 -->
<!--<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>-->
<!-- 注释 -->
<commentGenerator>
<!-- 是否不生成注释 -->
<property name="suppressAllComments" value="true"/>
<!-- 不但愿生成的注释中包含时间戳 -->
<!--<property name="suppressDate" value="true"/>-->
<!-- 添加 db 表中字段的注释,只有suppressAllComments为false时才生效-->
<!--<property name="addRemarkComments" value="true"/>-->
</commentGenerator>
<!-- jdbc链接 -->
<jdbcConnection driverClass="${spring.datasource.driverClassName}"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}">
<!--高版本的 mysql-connector-java 须要设置 nullCatalogMeansCurrent=true-->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!-- 类型转换 -->
<javaTypeResolver>
<!--是否使用bigDecimal,默认false。
false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal-->
<property name="forceBigDecimals" value="true"/>
<!--默认false
false,将全部 JDBC 的时间类型解析为 java.util.Date
true,将 JDBC 的时间类型按以下规则解析
DATE -> java.time.LocalDate
TIME -> java.time.LocalTime
TIMESTAMP -> java.time.LocalDateTime
TIME_WITH_TIMEZONE -> java.time.OffsetTime
TIMESTAMP_WITH_TIMEZONE -> java.time.OffsetDateTime
-->
<!--<property name="useJSR310Types" value="false"/>-->
</javaTypeResolver>
<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="com.wqlm.boot.user.po" targetProject="src/main/java">
<!-- 是否让 schema 做为包的后缀,默认为false -->
<!--<property name="enableSubPackages" value="false"/>-->
<!-- 是否针对string类型的字段在set方法中进行修剪,默认false -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成Mapper.xml文件 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<!--<property name="enableSubPackages" value="false"/>-->
</sqlMapGenerator>
<!-- 生成 XxxMapper.java 接口-->
<javaClientGenerator targetPackage="com.wqlm.boot.user.dao" targetProject="src/main/java" type="XMLMAPPER">
<!--<property name="enableSubPackages" value="false"/>-->
</javaClientGenerator>
<!-- schema为数据库名,oracle须要配置,mysql不须要配置。
tableName为对应的数据库表名
domainObjectName 是要生成的实体类名(能够不指定,默认按帕斯卡命名法将表名转换成类名)
enableXXXByExample 默认为 true, 为 true 会生成一个对应Example帮助类,帮助你进行条件查询,不想要能够设为false
-->
<table schema="" tableName="user" domainObjectName="User"
enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" selectByExampleQueryId="false">
<!--是否使用实际列名,默认为false-->
<!--<property name="useActualColumnNames" value="false" />-->
</table>
</context>
</generatorConfiguration>
复制代码
MyBatis Generator Config 引用的外部配置文件内容以下
# mysql
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/boot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
复制代码
配置好后,双击 maven 中的 MyBatis Generator 运行