MyBatis Custom Data Type Wizard

Posted 我要做个程序员

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis Custom Data Type Wizard相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

为什么在使用GDB检查时,某些局部变量未在相应的堆栈帧中列出?

Mybatis - if test条件判断,踩坑记录

Mybatis - if test条件判断,踩坑记录

Mybatis - if test条件判断,踩坑记录

salesforce Custom Setting VS Custom metadata type(CMT)

Spring boot MyBatis基本操作