Mybatis实现自定义类型转换器TypeHandler的方法


当大家使用mybatis作为持久层框架时,在存储和查询数据时,只需要在mapper.xml文件中配置好对应字段的JdbcType和JavaType,mybatis就可以帮我们转化对应的类型。这背后是有mybatis内置的类型转换器做转换(可见源码TypeHandlerRegistry)。但是有时候,我们会对某些字段做特殊处理,比如加密和解密、状态转换、类型转换等。这个时候我们需要自定义类型转换器。

类架构

从上面的图中可以看出MyBatis中整个类型处理器实现架构,TypeHandler接口定义了类型处理器,而TypeReference抽象类则定义了一个类型引用,用于引用一个泛型类型(此处很抽象,不好理解,详见后续解析),BaseTypeHandler则是类型处理器的基础,是所有类型处理器的公共模块,几乎所有的类型处理器都是通过直接继承BaseTypeHandler来实现的。

一、原理

使用场景:mybatis在预处理语句(PreparedStatement)中设置一个参数时,或者从结果集(ResultSet)中取出一个值时,都会用到TypeHandler。它的作用就是将java类型(javaType)转化为jdbc类型(jdbcType),或者将jdbc类型(jdbcType)转化为java类型(javaType)。

二、自定义类型处理器

实现TypeHandler接口或者继承BaseTypehandler

TypeHandler是一个接口,它定义了如下四个方法,实现类必须去实现,方法如下:

?
1
2
3
4
5
6
7
8
void setParameter(PreparedStatement var1, int var2, T var3,JdbcType var4) throws SQLException;
 
  T getResult(ResultSet var1, String var2) throws SQLException;
 
  T getResult(ResultSet var1, int var2) throws SQLException;
 
  T getResult(CallableStatement var1, int var2) throws SQLException;
}

setParameter:通过preparedStatement对象设置参数,将T类型的数据存入数据库。

getResult:通过列名或者下标来获取结果数据,也可以通过CallableStatement获取数据。

三、案例(自定义敏感字段加解密处理器)

MyTypeHandler实现TypeHandler接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.mdd.mybatis.typehandle;
 
import com.mdd.mybatis.util.DESUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
 
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class MyTypeHandle implements TypeHandler<String> {
  private static String KEY = "123456" ;
 
  @Override
  public void setParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {
   try {
    String encrypt = DESUtil.encrypt(s, KEY);
    preparedStatement.setString(i, encrypt);
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 
  @Override
  public String getResult(ResultSet resultSet, String s) throws SQLException {
   String result = resultSet.getString(s);
   if (StringUtils.isNotEmpty(result)) {
    try {
     return DESUtil.decrypt(result, KEY);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
   return result;
  }
 
  @Override
  public String getResult(ResultSet resultSet, int i) throws SQLException {
   String result = resultSet.getString(i);
   if (StringUtils.isNotEmpty(result)) {
    try {
     return DESUtil.decrypt(result, KEY);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
   return result;
  }
 
  @Override
  public String getResult(CallableStatement callableStatement, int i) throws SQLException {
   String result = callableStatement.getString(i);
   if (StringUtils.isNotEmpty(result)) {
    try {
     return DESUtil.decrypt(result, KEY);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
   return result;
  }
}

配置注册自定义处理器(mybatis.cfg.xml)

?
1
2
3
4
<!--自定义类型处理器-->
<typeHandlers>
  <typeHandler handler= "com.mdd.mybatis.typehandle.MyTypeHandle" ></typeHandler>
</typeHandlers>

使用自定义处理器(mapper文件)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version= "1.0" encoding= "UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace= "com.mdd.mybatis.dao.UserDao" >
  <resultMap id= "BaseResultMap" type= "com.mdd.mybatis.dao.vo.User" >
   <id column= "user_id" property= "userId" jdbcType= "VARCHAR" />
   <result column= "name" property= "name" typeHandler= "com.mdd.mybatis.typehandle.MyTypeHandle" />
   <result column= "password" property= "password" jdbcType= "VARCHAR" />
   <result column= "age" property= "age" jdbcType= "INTEGER" />
  </resultMap>
 
  <sql id= "Base_Column_List" >
   user_id,name,password, age
  </sql>
 
  <insert id= "saveUser" >
   INSERT INTO t_user(user_id,name, password, age) VALUES (
   #{userId,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR},
   #{password,jdbcType=VARCHAR},#{age,jdbcType=INTEGER}
   )
  </insert>
 
  <insert id= "saveUserWithType" >
   INSERT INTO t_user(user_id,name, password, age) VALUES (
   #{userId,jdbcType=VARCHAR},#{name,typeHandler=com.mdd.mybatis.typehandle.MyTypeHandle},
   #{password,jdbcType=VARCHAR},#{age,jdbcType=INTEGER}
   )
  </insert>
 
  <select id= "queryUser" resultMap= "BaseResultMap" >
   select * from t_user where user_id = #{userId,jdbcType=VARCHAR}
  </select>
 
</mapper>

通过上面的配置,自定义的TypeHandler就会生效,敏感字段的加解密在dao层就可以解决,对上层业务无感,使用相当方便,也更加灵活。

参考 http://www.mybatis.org/mybatis-3/configuration.html#typeHandler

https://www.jb51.net/article/89073.htm

 

http://www.manongjc.com/article/15577.html

https://www.cnblogs.com/sin90lzc/archive/2012/06/30/2570847.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM