现在项目决定不再使用第三方类库的是时间格式而使用用java8提供的新的日期类型,使用mybatis插件时有些点需要进行更改
注意事项
mybatis需要使用3.4.0以上版本(不要只改插件中的版本,项目中的版本也要同时更改)
mybatis-generator-maven-plugin 我使用的是1.3.7
mysql-connector-java 我是用的是8.0.13(注意jdbc配置文件中url后面的配置需要加上时区serverTimezone的配置)
jdbc.url=jdbc:mysql://1.1.1.1:3306/***?useSSL=false&nullCatalogMeansCurrent=true&serverTimezone=Hongkong
一旦mybatis中使用的java8的日期类型那么项目中需要引入mybatis-typehandlers-jsr310
用来处理java8中的日期类型
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-typehandlers-jsr310</artifactId>
<version>1.0.2</version>
</dependency>
同时提醒为了不必要的麻烦,请检查项目的mybatis-spring是否为1.3.0及以上版本与commons-dbcp是否为1.4及以上版本
<!--mybatis-spring整合包,这样就可以通过spring配置bean的方式进行mybatis配置了,不然需要单独使用mybatis的配置-->
<!--同时搭配spring-jdbc使用(此jar已经在spring中引入了)-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- 导入dbcp的jar包,用来在spring-mybatis.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
依赖插件
在要使用插件的项目pom中加入如下插件
<plugin>
<!--用maven mybatis插件 要把plugin运行依赖得jar配置都放在里面,插件并不会扫描使用项目中的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>
同时修改mybatis的config.xml文件中的javaTypeResolver处配置
<!-- 类型解析器 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
<!-- 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 -->
<!--当useJSR310Types为true时,就会jdbc对应的日期类型会转成java8中的LocateDateTime类型,如果useJSR310Types为false,则还是转成java.util.Date类型-->
<property name="useJSR310Types" value="true"/>
</javaTypeResolver>
至此改进完毕。
下面附上我的整个mybatis的config.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>
<!-- 引入配置文件 -->
<properties resource="jdbc.properties"/>
<!-- 指定数据连接驱动jar地址 -->
<!--<classPathEntry location="${classPath}" />-->
<!--对应的模块 -->
<context id="lanly">
<property name="javaFileEncoding" value="UTF-8"/>
<!--分页插件,但是此插件并不是使用limit,而是通过ResultSet的游标来实现分页,所以数据量大的情况下会影响效率,之后会更新为limit分页插件-->
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"/>
<!--这个插件主要用来为生成的Java模型类添加序列化接口,并生成serialVersionUID字段-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!--这个插件用来在XXXExample类中生成大小写敏感的LIKE方法(插件本身用处不大,但是我们可以通过这个插件学习给XXXExample类添加额外的方法)-->
<plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin"/>
<!-- 注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<!-- 是否取消注释 -->
<property name="suppressDate" value="true"/>
<!-- 是否生成注释代时间戳-->
</commentGenerator>
<!-- jdbc连接 -->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}" userId="${jdbc.username}"
password="${jdbc.password}"/>
<!-- 类型解析器 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
<!-- 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 -->
<!--当useJSR310Types为true时,就会jdbc对应的日期类型会转成java8中的LocateDateTime类型,如果useJSR310Types为false,则还是转成java.util.Date类型-->
<property name="useJSR310Types" value="true"/>
</javaTypeResolver>
<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="${domainPackage}.lanly"
targetProject="${domainProject}">
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
<!-- 是否针对string类型的字段在set的时候进行trim调用 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成mapxml文件 -->
<sqlMapGenerator targetPackage="${mapxmlPackage}.lanly"
targetProject="${mapxmlProject}">
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成mapxml对应client,也就是接口dao -->
<javaClientGenerator targetPackage="${daoPackage}.lanly"
targetProject="${daoProject}" type="XMLMAPPER">
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 配置表信息 -->
<table tableName="lanly_%">
<!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
是否生成 example类 -->
<!-- 忽略列,不生成bean 字段 -->
<!--<ignoreColumn column="FRED" />-->
<!-- 指定列的java数据类型 -->
<!--<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />-->
<!--<generatedKey column="ID" sqlStatement="SELECT SEQ_BA_MENU.NEXTVAL FROM dual"/>-->
</table>
</context>
</generatorConfiguration>