23

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 ?

Scrooge McD
  • 335
  • 1
  • 3
  • 12

4 Answers4

38

Look here: 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.

<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)

mit
  • 545
  • 1
  • 8
  • 10
10

For my current project I've created mappers for Java 8 time API classes.

You can see my implementation here jneat/mybatis-types

rumatoest
  • 403
  • 5
  • 9
7

Please, look here: http://mybatis.github.io/mybatis-3/configuration.html#typeHandlers

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 your config.xml like this:

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

It should help.

Nat
  • 3,587
  • 20
  • 22
Aliaksei
  • 211
  • 1
  • 7
  • Thanks for your help. I referred to this link, for a sample implementation of the type handler. [LocalDateTypeHandler] (https://github.com/LukeL99/joda-time-mybatis/blob/master/src/main/java/org/joda/time/mybatis/handlers/LocalDateTypeHandler.java) – Scrooge McD Aug 04 '14 at 18:27
  • 1
    The answer below (jsr310) looks more appropriate and updated. – Daniele Jun 30 '17 at 12:08
2

You should just define the entire class name:

resultType="java.time.LocalDate"
Houssam Badri
  • 2,441
  • 3
  • 29
  • 60