SpringBoot整合Mybatis LocalDateTime 映射失效的解决
SpringBoot整合Mybatis LocalDateTime映射失效
一、概述
最近在开发一个项目,在使用SpringBoot继承Mybatis时,做单元测试时,由于需要根据参数(类型LocaDateTime)去更新数据,发现更新记录为0。
刚开始以为是没有提交事务(Mybatis默认没有开启自动提交),后来句时,是能成功的。所以排除没有提交事务。
二、具体原因
在实体PO类里面,可以使用Java.sql.Date,Java.sql.Timestamp,java.util.Date来映射到数据库date,timestamp,datetime。但是这些类许多方法都已经过时。
Java8的API的LocalDate,LocalDateTime,LocalTime,现在比较常用。但是,
我mybatis的版本(3.5.3)是不支持Java8的日期、时间。(默认情况下,Mybatis是不支持Java8的时间和日期)
三、解决办法
1.引入类型转换的依赖
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-typehandlers-jsr310</artifactId> <version>1.0.1</version> </dependency>
2.在Mybatis.config.xml文件配置
<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>
四、小结一下
发现问题,并解决问题,也一种能力的提升。以上如果有错,还望多多指正。
使用LocalDateTime报错问题
在使用mybatis做查询时, 时间字段设置为了LocalDatetime,报错
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
查看了网上的解决办法全部千篇一律,最后在评论里发现了有效的办法.
解决方法
升级druid数据源,我升级到21版本可以了. 总之使用高版本的数据源
<!-- 阿里Druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.21</version> </dependency>
栏 目:JAVA代码
本文标题:SpringBoot整合Mybatis LocalDateTime 映射失效的解决
本文地址:http://www.codeinn.net/misctech/196925.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




