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 ?
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.
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.
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.
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.
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)
For my current project I've created mappers for Java 8 time API classes.
You can see my implementation here jneat/mybatis-types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With