mybatis低版本jsr310(LocalDateTime,LocalDate等) Joda Time支持

<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <!--映射下划线风格到驼峰风格-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <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>
</configuration>

在配置文件mybatis-config.xml中添加

<typeHandlers>节点所有内容,并添加maven依赖

 <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-typehandlers-jsr310</artifactId>
      <version>1.0.2</version>
    </dependency>

Joda Time

JSR310伴随着JDK8的发布,带来了新的时间类、日期类、日期时间类、以及时间戳相关类。

笔者曾经用Joda处理了7-8个时区的日期时间、joda游刃有余。jsr310有抄袭Joda time的嫌疑,

这从一定程序上拉拢了用户不愿放弃java的时间处理类。

在JDK8之前,如果你曾经使用Joda-time处理时间,可以添加以下时间处理类的委托。

(没具体测试)

maven依赖:

<dependency>
    <groupId>io.eliez</groupId>
    <artifactId>joda-time-mybatis</artifactId>
    <version>2.0.0</version>
</dependency>

mybatis-config.xml添加代码如下

<typeHandlers>
...
<typeHandler handler="org.joda.time.mybatis.handlers.DateTimeTypeHandler" />
        <typeHandler handler="org.joda.time.mybatis.handlers.LocalDateTypeHandler" />
        <typeHandler handler="org.joda.time.mybatis.handlers.LocalTimeTypeHandler" />
...
</typeHandlers>

猜你喜欢

转载自www.cnblogs.com/passedbylove/p/11388659.html