使用mybatis进行Java 8 LocalDate映射 [英] Java 8 LocalDate mapping with mybatis

查看:3124
本文介绍了使用mybatis进行Java 8 LocalDate映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用java.time.LocalDate(Java 8)来表示Java类中的一些成员字段。

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 
}

我也在使用mybatis来与我的数据库进行交互。

I am also using mybatis, to interact with my database.

但是,如果我使用java.util.Date,就像在,

If ,however, I use java.util.Date, as in,

 private Date startDate;
 private Date endDate;

当我将它们声明为java时,我在这两个字段(startDate和endDate)中检索到正确的值.util.Date。

I get the proper values retrieved in these two fields (startDate and endDate) when I declare them as java.util.Date.

是不是因为mybatis目前没有将'Timestamp'(SQL Server)映射到java.time?

Is it because mybatis does not currently have a mappping of 'Timestamp'(SQL Server) to java.time ?

我应该如何使用java.time.LocalDate与MyBatis进行映射?

How should I go about using java.time.LocalDate to map with MyBatis ?

推荐答案

请看这里: http://mybatis.github.io/mybatis-3/configuration.html#typeHandlers

To使用LocalDate和Timestamp你必须编写一个自定义的typeHandler,如下所示:

To use LocalDate and Timestamp you have to write a custom typeHandler, like this:

// ExampleTypeHandler.java
@MappedTypes(LocalDate.class)
public class LocalDateTypeHandler extends BaseTypeHandler<LocalDate> {

  //implement all methods
}

配置你的像这样的config.xml:

config your config.xml like this:

<!-- mybatis-config.xml -->
<typeHandlers>
  <typeHandler handler="your.package.LocalDateTypeHandler"/>
</typeHandlers>

它应该有帮助。

这篇关于使用mybatis进行Java 8 LocalDate映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆