搜文章
推荐 原创 视频 Java开发 iOS开发 前端开发 JavaScript开发 Android开发 PHP开发 数据库 开发工具 Python开发 Kotlin开发 Ruby开发 .NET开发 服务器运维 开放平台 架构师 大数据 云计算 人工智能 开发语言 其它开发
Lambda在线 > 我要做个程序员 > MyBatis Custom Data Type Wizard

MyBatis Custom Data Type Wizard

我要做个程序员 2018-06-28

Although the type handlers in Mybatis can cover 90% situations, sometimes we have to define new custom data type handler.

Now I will take java.util.Calendar as an example to show you how define new custom data type hanlder.

1. Custom Data Type Handler Class

The Custom Type Handler is generated as a stub class implemented either for the following interfaces of iBatis or MyBatis:

  • Batis - org.apache.ibatis.type.TypeHandler

  • atis - com.ibatis.sqlmap.client.extensions.TypeHandlerCallback

The custom Type Handler needs to be implemented to do special processing for the custom type.

 1import org.apache.ibatis.type.JdbcType;
2import org.apache.ibatis.type.TypeHandler;
3import java.sql.*;
4import java.util.Calendar;
5
6public class CalendarHandler implements TypeHandler<Calendar{
7    @Override
8    public Calendar getResult(ResultSet resultSet, String string) throws SQLException {
9        Timestamp timeStamp = resultSet.getTimestamp(string);
10        Calendar calendar = Calendar.getInstance();
11        calendar.setTimeInMillis(timeStamp.getTime());
12        return calendar;
13    }
14
15    @Override
16    public Calendar getResult(ResultSet resultSet, int i) throws SQLException {
17        Timestamp timeStamp = resultSet.getTimestamp(i);
18        Calendar calendar = Calendar.getInstance();
19        calendar.setTimeInMillis(timeStamp.getTime());
20        return calendar;
21    }
22
23    @Override
24    public Calendar getResult(CallableStatement cs, int i) throws SQLException {
25        Timestamp timeStamp = cs.getTimestamp(i);
26        Calendar calendar = Calendar.getInstance();
27        calendar.setTimeInMillis(timeStamp.getTime());
28        return calendar;
29    }
30
31    @Override
32    public void setParameter(PreparedStatement ps, int i, Calendar t, JdbcType jdbcType) throws SQLException {
33        ps.setTimestamp(i, new Timestamp(t.getTimeInMillis()));
34    }
35}

The main methods that need to be implemented are setParameter and  getResult. The setParameter uses the custom type to set a parameter in  JDBC. The getResult method returns the Custom type which is created from a result returned from JDBC.

2. Register Type Handler

Create a Mybatis xml configuration file, then type the following content:

 1<?xml version="1.0" encoding="UTF-8" ?>
2<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
3        "http://mybatis.org/dtd/mybatis-3-config.dtd">

4
5<configuration>
6    <typeHandlers>
7        <typeHandler handler="site.siyu.promanagement.util.CalendarHandler"
8                     javaType="java.util.Calendar" jdbcType="TIMESTAMP"/>

9    </typeHandlers>
10</configuration>

Note: This file should be loaded before other Mybatis mapper files, so I put it into the resources folder.

3. Utilize This Handler

 1<mapper namespace="site.siyu.promanagement.dao.ProjectDao">
2    <insert id="add" parameterType="site.siyu.promanagement.domain.Project">
3        <selectKey keyProperty="id" resultType="int" order="AFTER">
4            SELECT LAST_INSERT_ID()
5        </selectKey>
6        INSERT INTO tb_project(name, introduce, tech, price, discount, owner_id,
7            contact_id, start_time, end_time)
8        VALUES
9            (#{name}, #{introduce}, #{tech}, #{price}, #{discount}, #{owner.id}, #{contact.id},
10            #{startTime, javaType = java.util.Calendar, jdbcType = TIMESTAMP, typeHandler = site.siyu.promanagement.util.CalendarHandler},
11            #{endTime, javaType = java.util.Calendar, jdbcType = TIMESTAMP, typeHandler = site.siyu.promanagement.util.CalendarHandler})
12     </insert>


版权声明:本站内容全部来自于腾讯微信公众号,属第三方自助推荐收录。《MyBatis Custom Data Type Wizard》的版权归原作者「我要做个程序员」所有,文章言论观点不代表Lambda在线的观点, Lambda在线不承担任何法律责任。如需删除可联系QQ:516101458

文章来源: 阅读原文

相关阅读

关注我要做个程序员微信公众号

我要做个程序员微信公众号:To_be_a_programmer

我要做个程序员

手机扫描上方二维码即可关注我要做个程序员微信公众号

我要做个程序员最新文章

精品公众号随机推荐