mybatis-generator-maven-plugin生成java8中的日期类型LocalDate等等
基于mysql8.0与mybatis-generator-maven-plugin 1.3.7
加入依赖插件
<plugins>
<plugin>
<!--
用maven mybatis插件如果不在plugin里面添加依赖包得引用的话,会找不到相关得jar包,
在plugin外部得jar包,他不会去找到并执行,所以要把plugin运行依赖得jar配置都放在里面
-->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
</dependencies>
</plugin>
</plugins>
添加配置文件
在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>
<context id="testTables" targetRuntime="MyBatis3">
<!-- 生成的Java文件的编码 -->
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 格式化java代码 -->
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
<!-- 格式化XML代码 -->
<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/db?serverTimezone=UTC"
userId="root"
password="******">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="true"/>
<!-- This property is used to specify whether MyBatis Generator should force the use of JSR-310 data types for DATE, TIME,
and TIMESTAMP fields, rather than using java.util.Date -->
<property name="useJSR310Types" value="true"/>
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.github.*.domain"
targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="mappings"
targetProject="src/main/resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.github.*.dao"
targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="t_sys_resource"
<!-- 指明实体名称 -->
domainObjectName="SysResourceDo"
<!-- 指明Mapper类名称 -->
mapperName="SysResourceDao"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<!-- 有些表的字段需要指定java类型
<table schema="" tableName="">
<columnOverride column="" javaType="" />
</table> -->
</context>
</generatorConfiguration>
注意,在连接mysql8.0的时候要指明timeZone,即serverTimezone=UTC
这个不能忘
关键的来了,在javaTypeResolver
结点中加入属性useJSR310Types
,当useJSR310Types
为true时,就会jdbc对应的日期类型会转成java8中的LocateDateTime类型,如果useJSR310Types
为false,则还是转成java.util.Date
类型
如果不出意外,生成的日期就是java8中的日期类型:
public class SysResourceDo {
private Integer id;
private String resourceNo;
private String zhName;
private String enName;
private String level;
private Integer parentId;
private Integer seq;
private String url;
private Boolean atDeleted;
private LocalDateTime atCreated;
private LocalDateTime atModified;
//omit....
}
MyBatis Generator中javaTypeResolver
的相关文档地址:http://www.mybatis.org/generator/configreference/javaTypeResolver.html
后序
一旦mybatis中使用了java8中的日期类型,记得要在项目中引入mybatis-typehandlers-jsr310
,这个主要是处理java8中的日期类型
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-typehandlers-jsr310</artifactId>
<version>1.0.2</version>
</dependency>