与 mybatis 的 Java 8 LocalDate 映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25113579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java 8 LocalDate mapping with mybatis
提问by Scrooge McD
I am using java.time.LocalDate
(Java 8) to represent some of the member fields in a Java class.
我使用java.time.LocalDate
(Java 8) 来表示 Java 类中的一些成员字段。
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 startDate
and endDate
fields end up as null.
我也在使用 MyBatis,与我的数据库进行交互。在检索从DB一些数据,所有其他的领域得到正确填充,但startDate
和endDate
领域最终为空。
If, however, I use java.util.Date
, I get the proper values retrieved in these two fields
但是,如果我使用java.util.Date
,我会在这两个字段中检索到正确的值
private Date startDate;
private Date endDate;
Is it because MyBatis does not currently have a mappping from Timestamp
(SQL Server) to java.time
?
是不是因为 MyBatis 目前没有从Timestamp
(SQL Server)到的映射java.time
?
How should I go about using java.time.LocalDate
to map with MyBatis?
我应该如何使用java.time.LocalDate
MyBatis 进行映射?
采纳答案by Aliaksei
Please, look here: http://mybatis.github.io/mybatis-3/configuration.html#typeHandlers
请看这里:http: //mybatis.github.io/mybatis-3/configuration.html#typeHandlers
To use LocalDate and Timestamp you have to write a custom typeHandler, like this:
要使用 LocalDate 和 Timestamp,您必须编写一个自定义的 typeHandler,如下所示:
// ExampleTypeHandler.java
@MappedTypes(LocalDate.class)
public class LocalDateTypeHandler extends BaseTypeHandler<LocalDate> {
//implement all methods
}
config your config.xml like this:
像这样配置你的 config.xml:
<!-- mybatis-config.xml -->
<typeHandlers>
<typeHandler handler="your.package.LocalDateTypeHandler"/>
</typeHandlers>
It should help.
它应该有帮助。
回答by rumatoest
For my current project I've created mappers for Java 8 time API classes.
对于我当前的项目,我为 Java 8 时间 API 类创建了映射器。
You can see my implementation here jneat/mybatis-types
你可以在这里看到我的实现jneat/mybatis-types
回答by mit
Look here: https://github.com/mybatis/typehandlers-jsr310
看这里:https: //github.com/mybatis/typehandlers-jsr310
If you are using mybatis version 3.4 or later, you can simply add this artifact on your classpath and MyBatis will automatically register the provided type handlers.
如果你使用的是 mybatis 3.4 或更高版本,你可以简单地在你的类路径中添加这个工件,MyBatis 将自动注册提供的类型处理程序。
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-typehandlers-jsr310</artifactId>
<version>1.0.0</version>
</dependency>
If you are using an older version you need to register the type handlers manually.
如果您使用的是旧版本,则需要手动注册类型处理程序。
<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>
UPD:
更新:
Type handlers for "JSR 310: Date and Time API" has been merged into mybatis core since 3.4.5.(See #974)
自 3.4.5 起,“JSR 310:日期和时间 API”的类型处理程序已合并到 mybatis 核心中。(参见#974)
回答by Houssem Badri
You should just define the entire class name:
您应该只定义整个类名:
resultType="java.time.LocalDate"