java.sql.Date
、java.sql.Timestamp
、java.util.Date
等,来映射数据库中的date、timestamp、datetime等类型的字段,虽然使用JDK1.8也近三年多了,但是依旧没有用到JDK1.8的新特性,比如lambda表达式,stream流,以及日期类型。所以现在也是强迫自己能用新特性就用新特性,绝不偷懒,慢慢地也是差不多都掌握了其用法,写起来确实不错,但是在将数据保存到mysql中时,出现了如下异常
Caused by: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'birthday'. It was either not specified and/or could not be found for the javaType / jdbcType combination specified. at org.apache.ibatis.mapping.ParameterMapping$Builder.validate(ParameterMapping.java:117) at org.apache.ibatis.mapping.ParameterMapping$Builder.build(ParameterMapping.java:104) at org.apache.ibatis.builder.SqlSourceBuilder$ParameterMappingTokenHandler.buildParameterMapping(SqlSourceBuilder.java:123) at org.apache.ibatis.builder.SqlSourceBuilder$ParameterMappingTokenHandler.handleToken(SqlSourceBuilder.java:67) at org.apache.ibatis.parsing.GenericTokenParser.parse(GenericTokenParser.java:53) at org.apache.ibatis.builder.SqlSourceBuilder.parse(SqlSourceBuilder.java:45) at org.apache.ibatis.scripting.defaults.RawSqlSource.<init>(RawSqlSource.java:46) at org.apache.ibatis.scripting.xmltags.XMLLanguageDriver.createSqlSource(XMLLanguageDriver.java:60) at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.buildSqlSourceFromStrings(MapperAnnotationBuilder.java:439) at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.getSqlSourceFromAnnotations(MapperAnnotationBuilder.java:422) ... 47 more
目前使用的mybatis版本是3.3.0,mybatis-spring版本为1.2.3,面向Google一把后发现是因为低版本的mybatis不支持JDK1.8的typehandler,解决办法有两种:
1.加入新的依赖,配置JDK1.8的type handler
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-typehandlers-jsr310</artifactId> <version>1.0.2</version> </dependency>
在mybatis-config.xml中加入如下配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 参考:http://mybatis.github.io/mybatis-3/configuration.html --> <settings> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="multipleResultSetsEnabled" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="useGeneratedKeys" value="true"/> <setting name="autoMappingBehavior" value="PARTIAL"/> <setting name="defaultExecutorType" value="SIMPLE"/> <setting name="defaultStatementTimeout" value="15"/> <setting name="safeRowBoundsEnabled" value="false"/> <setting name="mapUnderscoreToCamelCase" value="false"/> <setting name="localCacheScope" value="SESSION"/> <setting name="jdbcTypeForNull" value="OTHER"/> <setting name="logImpl" value="SLF4J"/> </settings> <typeHandlers> <typeHandler handler="org.apache.ibatis.type.InstantTypeHandler"/> <typeHandler handler="org.apache.ibatis.type.LocalDateTimeTypeHandler"/> <typeHandler handler="org.apache.ibatis.type.LocalDateTypeHandler"/> <typeHandler handler="org.apache.ibatis.type.LocalTimeTypeHandler"/> <typeHandler handler="org.apache.ibatis.type.OffsetDateTimeTypeHandler"/> <typeHandler handler="org.apache.ibatis.type.OffsetTimeTypeHandler"/> <typeHandler handler="org.apache.ibatis.type.ZonedDateTimeTypeHandler"/> </typeHandlers> </configuration>
修改sqlSessionFactoryBean
的配置,新增如下配置:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="pro.liyang.spring.entity"/> <property name="configLocation" value="mybatis-config.xml"/> </bean>
此时就就可以解决以上问题,该方法适合比较古老的项目,大多都是mybatis3.4一下的版本。
还有一种办法就是升级mybatis的版本(推荐使用)。
如果不想加入新的依赖,也不想加入配置文件,则只需要将mybatis升级到3.4.6
,将mybatis-spring的版本是升级到1.3.2
即可,此时可以直接支持JDK1.8的日期类型。
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency>