Mybatis is pretty cool library for mapping your database queries. Unfortunately it does not know anything about Java8 time API (JSR 310). Thus you can not yous in your POJO new date/time types like LocalDate, LocalDateTime, ZonedDate, ZonedDateTime etc.

You can solve this problem by creating your own mapper for mybatis, or you can use mappers I have created.

My mapper implementation provides next handlers for core java.time.* types:

  • java.time.Instant (via java.sql.Timestamp)
  • java.time.LocalDate (via java.sql.Date)
  • java.time.LocalDateTime (via java.sql.Timestamp)
  • java.time.LocalTime (via java.sql.Time)
  • java.time.OffsetDateTime (via java.sql.Timestamp)
  • java.time.ZonedDateTime (via java.sql.Timestamp)

You can get sources here

Adding dependency into your project

You can add artifact using gradle, maven or similar tools. More instructions available at github repo.

Project configuration

Using mybatis configuration

Preferred way is to add package search option.

<!-- in mybatis-config.xml add -->
  <package name="com.github.jneat.mybatis"/>

Also you can directly point at each handler you are require.

<!-- in mybatis-config.xml add -->
  <typeHandler handler="com.github.jneat.mybatis.InstantHandler"/>
  <typeHandler handler="com.github.jneat.mybatis.LocalDateHandler"/>
  <typeHandler handler="com.github.jneat.mybatis.LocalDateTimeHandler"/>
  <typeHandler handler="com.github.jneat.mybatis.LocalTimeHandler"/>
  <typeHandler handler="com.github.jneat.mybatis.OffsetDateTimeHandler"/>
  <typeHandler handler="com.github.jneat.mybatis.ZonedDateTimeHandler"/>

Using mybatis with spring

I will not describe how to configure mybatis with spring, only will show my part.

<bean id="yourBeanId" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- your bean configuration goes here -->
    <property name="typeHandlersPackage" value="com.github.jneat.mybatis" />

Now you will be able to use Java 8 time API in your POJO classes. No additional configuration requires.