Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 LocalDate mapping with mybatis

Tags:

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 ?

like image 419
Scrooge McD Avatar asked Aug 04 '14 07:08

Scrooge McD


People also ask

What is the difference between iBatis and MyBatis?

It is the same thing, a persistence framework! But until June 2010, iBatis was under Apache license and since then, the framework founders decided to move it to Google Code and they renamed it to MyBatis. The framework is still the same though, it just has a different name now.

What is mapper in MyBatis?

Mapper XML is an important file in MyBatis, which contains a set of statements to configure various SQL statements such as select, insert, update, and delete. These statements are known as Mapped Statements or Mapped SQL Statements. All the statements have unique id.

What is TypeHandler in MyBatis?

typeHandlers. Whenever MyBatis sets a parameter on a PreparedStatement or retrieves a value from a ResultSet, a TypeHandler is used to retrieve the value in a means appropriate to the Java type.

What is LocalDate NOW () in Java?

now() now() method of a LocalDate class used to obtain the current date from the system clock in the default time-zone. This method will return LocalDate based on system clock with default time-zone to obtain the current date.


2 Answers

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)

like image 193
mit Avatar answered Sep 22 '22 13:09

mit


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

You can see my implementation here jneat/mybatis-types

like image 45
rumatoest Avatar answered Sep 22 '22 13:09

rumatoest