mybatis映射date类型_使用mybatis进行Java 8 LocalDate映射
I am using java.time.LocalDate (Java 8) to represent some of the member fields in a Java class.class Test{private LocalDate startDate;private LocalDate endDate;//other fields//getters and setters}I am
I am using java.time.LocalDate (Java 8) to represent some of the member fields in a Java class.
class Test{
private LocalDate startDate;
private LocalDate endDate;
//other fields
//getters and setters
}
I am also using mybatis, to interact with my database.
On retrieving some data from the DB, all other fields get populated properly,
but the startDate and endDate fields end up as null.
If ,however, I use java.util.Date, as in,
private Date startDate;
private Date endDate;
I get the proper values retrieved in these two fields (startDate and endDate) when I declare them as java.util.Date.
Is it because mybatis does not currently have a mappping of 'Timestamp'(SQL Server) to java.time ?
How should I go about using java.time.LocalDate to map with MyBatis ?
解决方案
To use LocalDate and Timestamp you have to write a custom typeHandler, like this:
// ExampleTypeHandler.java
@MappedTypes(LocalDate.class)
public class LocalDateTypeHandler extends BaseTypeHandler {
//implement all methods
}
config your config.xml like this:
It should help.

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)