mybatis中使用Java8的日期LocalDate、LocalDateTime
2017-07-13 13:26
766 查看
大家知道,在实体Entity里面,可以使用Java.sql.Date、java.sql.Timestamp、java.util.Date来映射到数据库的date、timestamp、datetime等字段
但是,java.sql.Date、java.sql.Timestamp、java.util.Date这些类都不好用,很多方法都过时了。
Java8里面新出来了一些API,LocalDate、LocalTime、LocalDateTime 非常好用
默认的情况下,在mybatis里面不支持java8的时间、日期。直接使用,会报如下错误
[java] view
plain copy
Caused by: java.lang.IllegalStateException: No typehandler found for property createTime
at org.apache.ibatis.mapping.ResultMapping$Builder.validate(ResultMapping.java:151)
at org.apache.ibatis.mapping.ResultMapping$Builder.build(ResultMapping.java:140)
at org.apache.ibatis.builder.MapperBuilderAssistant.buildResultMapping(MapperBuilderAssistant.java:382)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildResultMappingFromContext(XMLMapperBuilder.java:378)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:280)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:252)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:244)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:116)
... 81 common frames omitted
解决方法如下:
直接加入如下依赖
[html] view
plain copy
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-typehandlers-jsr310</artifactId>
<version>1.0.1</version>
</dependency>
配置好这个依赖之后,就可以把Entity里面的Date替换成LocalDate、LocalDateTime了,其他的不用改
[java] view
plain copy
public class User {
private Integer id;
private String name;
private LocalDate createDate;
private LocalDateTime createTime;
}
我个人的测试核心代码:
首先创建表:
create table tb_java8date (id int not null primary key auto_increment, t_datetime datetime);
package com.mmall.pojo;
import java.time.LocalDateTime;
public class DateTest {
private Integer id;
private LocalDateTime tDateTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public LocalDateTime gettDateTime() {
return tDateTime;
}
public void settDateTime(LocalDateTime tDateTime) {
this.tDateTime = tDateTime;
}
}
测试代码,比较懒+新手,直接在原来订单项目加了一个Controller。
以上仅在mybatis 3.4.0版本中测试有效
如果使用的mybatis版本低于3.4.0,则还需要配置如下
[html] view
plain copy
<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>
但是,java.sql.Date、java.sql.Timestamp、java.util.Date这些类都不好用,很多方法都过时了。
Java8里面新出来了一些API,LocalDate、LocalTime、LocalDateTime 非常好用
默认的情况下,在mybatis里面不支持java8的时间、日期。直接使用,会报如下错误
[java] view
plain copy
Caused by: java.lang.IllegalStateException: No typehandler found for property createTime
at org.apache.ibatis.mapping.ResultMapping$Builder.validate(ResultMapping.java:151)
at org.apache.ibatis.mapping.ResultMapping$Builder.build(ResultMapping.java:140)
at org.apache.ibatis.builder.MapperBuilderAssistant.buildResultMapping(MapperBuilderAssistant.java:382)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildResultMappingFromContext(XMLMapperBuilder.java:378)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:280)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:252)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:244)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:116)
... 81 common frames omitted
解决方法如下:
直接加入如下依赖
[html] view
plain copy
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-typehandlers-jsr310</artifactId>
<version>1.0.1</version>
</dependency>
配置好这个依赖之后,就可以把Entity里面的Date替换成LocalDate、LocalDateTime了,其他的不用改
[java] view
plain copy
public class User {
private Integer id;
private String name;
private LocalDate createDate;
private LocalDateTime createTime;
}
我个人的测试核心代码:
首先创建表:
create table tb_java8date (id int not null primary key auto_increment, t_datetime datetime);
mapper层:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.mmall.dao.DateTestMapper"> <insert id="insert" parameterType="com.mmall.pojo.DateTest"> INSERT INTO tb_java8date (id, t_datetime) VALUES ( #{id,jdbcType=INTEGER}, /*#{tDateTime,jdbcType=TIMESTAMP}*/ now() ) </insert> </mapper>
dao层:
package com.mmall.dao; import com.mmall.pojo.DateTest; public interface DateTestMapper { int insert(DateTest record); }pojo层:
package com.mmall.pojo;
import java.time.LocalDateTime;
public class DateTest {
private Integer id;
private LocalDateTime tDateTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public LocalDateTime gettDateTime() {
return tDateTime;
}
public void settDateTime(LocalDateTime tDateTime) {
this.tDateTime = tDateTime;
}
}
测试代码,比较懒+新手,直接在原来订单项目加了一个Controller。
package com.mmall.controller.portal; import com.mmall.common.Const; import com.mmall.common.ResponseCode; import com.mmall.common.ServerResponse; import com.mmall.dao.DateTestMapper; import com.mmall.pojo.DateTest; import com.mmall.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpSession; import java.time.LocalDateTime; import java.util.Date; @Controller @RequestMapping("/cart/") public class CartTestController { @Autowired private DateTestMapper dateTestMapper; @RequestMapping("add1.do") @ResponseBody public ServerResponse add(HttpSession session) { User user = (User) session.getAttribute(Const.CURRENT_USER); if (user == null) { return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc()); } DateTest dateTest=new DateTest();
//按照需求 是否在此设置当前日期时间 或者 在mapper中直接用mysql获取当前日期时间的函数 now( )
// dateTest.settDateTime(LocalDateTime.now()); int rowCount=dateTestMapper.insert(dateTest); if (rowCount>0){ return ServerResponse.createBySuccessMessage("插入LocalDateTime成功"); } return ServerResponse.createByErrorMessage("插入LocalDateTime失败"); } }
以上仅在mybatis 3.4.0版本中测试有效
如果使用的mybatis版本低于3.4.0,则还需要配置如下
[html] view
plain copy
<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>
相关文章推荐
- mybatis中使用Java8的日期LocalDate、LocalDateTime
- JDBC中使用Java8的日期LocalDate、LocalDateTime
- JDBC中使用Java8的日期LocalDate、LocalDateTime
- mybatis如何使用Java8的日期LocalDate和LocalDateTime详解
- 在JDBC中使用Java8的日期LocalDate、LocalDateTime
- JPA(Hibernate)中使用Java8的日期LocalDate、LocalDateTime
- LocalDate LocalTime LocalDateTime DateTimeFormatter Java 8 新出的时间(time)和日期(date)的API的使用详细实例
- Java 使用jdk8的新特性LocalDate和LocalDateTime 实现当前时间,日期加减,是否闰年,时间戳,时间线程每秒改变一次
- JDBC中使用Java8的日期LocalDate和LocalDateTime操作mysql、postgresql
- Java 线程安全LocalTime 和LocaldateTime 新的Date和Time类 -JDK8新时间类的简单使用
- java 关于使用java1.8的localDateTime日期操作的基本使用以及一些使用技巧 持续更新...
- Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题
- 解决Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题
- Java中使用LocalDate根据日期来计算年龄的实现方法
- Java 8---LocalDate,LocalTime处理日期时间
- 使用LocalDate计算两个日期相差多少天
- java学习记录(3)-- Java8日期API LocalDate & LocalTime &Junit
- jdk1.8 日期新API LocalDateTime,LocalDate,LocalTime 在Hibernate中无法反序列化解决方法
- java8 时间/日期 localDateTime比较,获取年月日时分秒
- java学习之日期类(3):Date类,DateFormat类,SimpleDateFormat类的说明与使用