MyBatis中使用LocalDateTime,‘java.sql.SQLFeatureNotSupportedException’

记录一次遇到的Bug解决过程。
MyBatis中使用了LocalDateTime。大家都知道在JDK8中新增了LocalDateTime,是线程安全的,而JDK8之前的java.sql.Date并不好用,而且还是非线程安全的。由于转换麻烦所以直接在实体中使用了LocalDateTime,但是在之后却抛出了异常信息,原来是MyBatis并不支持LocalDateTime

错误信息

[2019-09-27 14:56:47.565] ERROR com.dxyl.controller.home.HomeController 52 getRealTimeOverviewInfo - org.springframework.dao.InvalidDataAccessApiUsageException: Error attempting to get column 'create_time' from result set.  Cause: java.sql.SQLFeatureNotSupportedException
; null; nested exception is java.sql.SQLFeatureNotSupportedException
	at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:96) ~[spring-jdbc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) ~[spring-jdbc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73) ~[mybatis-spring-2.0.1.jar:2.0.1]
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446) ~[mybatis-spring-2.0.1.jar:2.0.1]
	at com.sun.proxy.$Proxy102.selectList(Unknown Source) ~[?:?]
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230) ~[mybatis-spring-2.0.1.jar:2.0.1]
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147) ~[mybatis-3.5.1.jar:3.5.1]
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80) ~[mybatis-3.5.1.jar:3.5.1]
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58) ~[mybatis-3.5.1.jar:3.5.1]
	at com.sun.proxy.$Proxy118.findByCreateTime(Unknown Source) ~[?:?]
	......

解决方法如下

引入如下依赖:

<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatis-typehandlers-jsr310artifactId>
    <version>1.0.2version>
dependency>

但是在增加了如上jar之后还是报错,继续检查,发现和mybatis版本有关系。在SpringBoot中引入的是 3.5.1 版本的。并不支持,所以需要排除3.5.1jar,重新引入低版本的jar

<dependency>
     <groupId>org.mybatis.spring.bootgroupId>
     <artifactId>mybatis-spring-boot-starterartifactId>
     <version>2.0.1version>
     <exclusions>
         <exclusion>
             <groupId>org.mybatisgroupId>
             <artifactId>mybatisartifactId>
         exclusion>
     exclusions>
 dependency>
 <dependency>
     <groupId>org.mybatisgroupId>
     <artifactId>mybatisartifactId>
     <version>3.5.0version>
 dependency>

配置完如上依赖后,再使用LocalDateTime就不会报错了

你可能感兴趣的:(Java,MySQL,MyBatis)