<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "" >
<mapper namespace="com.mdd.mybatis.dao.UserDao" >
<resultMap 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 >
user_id,name,password, age
</sql>
<insert>
INSERT INTO t_user(user_id,name, password, age) VALUES (
#{userId,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},#{age,jdbcType=INTEGER}
)
</insert>
<insert>
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 resultMap="BaseResultMap">
select * from t_user where user_id = #{userId,jdbcType=VARCHAR}
</select>
</mapper>
通过上面的配置,自定义的TypeHandler就会生效,敏感字段的加解密在dao层就可以解决,对上层业务无感,使用相当方便,也更加灵活。
参考
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx