mybatis的typeHandler
typeHandler作用:
1.传参时将javaType类型转换成jdbcType
2.结果集中ResultSet中取值时,jdbcType转换为javaType;
系统自定义的typeHandler:
mybatis系统内部定义了一系列的typeHandler;基本涵盖了我们正常使用的类型转换;如下

选取一个系统自定义的typeHandler看看;
在包org.apache.ibatis.type下有一个StringTypeHandler.java
源码如下:
public class StringTypeHandler extends BaseTypeHandler<String> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
throws SQLException {
ps.setString(i, parameter);
}
@Override
public String getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return rs.getString(columnName);
}
@Override
public String getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return rs.getString(columnIndex);
}
@Override
public String getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
return cs.getString(columnIndex);
}
}
StringTypeHandler继承了BaseTypeHandler;而BaseTypeHandler实现了接口TypeHandler,
BaseTypeHandler中代码
/**
* Copyright 2009-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.type; import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import org.apache.ibatis.executor.result.ResultMapException;
import org.apache.ibatis.session.Configuration; /**
* @author Clinton Begin
* @author Simone Tripodi
*/
public abstract class BaseTypeHandler<T> extends TypeReference<T> implements TypeHandler<T> { protected Configuration configuration; public void setConfiguration(Configuration c) {
this.configuration = c;
} @Override
public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
if (parameter == null) {
if (jdbcType == null) {
throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters.");
}
try {
ps.setNull(i, jdbcType.TYPE_CODE);
} catch (SQLException e) {
throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . " +
"Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. " +
"Cause: " + e, e);
}
} else {
try {
setNonNullParameter(ps, i, parameter, jdbcType);
} catch (Exception e) {
throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType + " . " +
"Try setting a different JdbcType for this parameter or a different configuration property. " +
"Cause: " + e, e);
}
}
} @Override
public T getResult(ResultSet rs, String columnName) throws SQLException {
T result;
try {
result = getNullableResult(rs, columnName);
} catch (Exception e) {
throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set. Cause: " + e, e);
}
if (rs.wasNull()) {
return null;
} else {
return result;
}
} @Override
public T getResult(ResultSet rs, int columnIndex) throws SQLException {
T result;
try {
result = getNullableResult(rs, columnIndex);
} catch (Exception e) {
throw new ResultMapException("Error attempting to get column #" + columnIndex+ " from result set. Cause: " + e, e);
}
if (rs.wasNull()) {
return null;
} else {
return result;
}
} @Override
public T getResult(CallableStatement cs, int columnIndex) throws SQLException {
T result;
try {
result = getNullableResult(cs, columnIndex);
} catch (Exception e) {
throw new ResultMapException("Error attempting to get column #" + columnIndex+ " from callable statement. Cause: " + e, e);
}
if (cs.wasNull()) {
return null;
} else {
return result;
}
} public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException; public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException; public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException; public abstract T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException; }
getResult分别用columnName和columnIndex从ResultSet中获取数据,CallableStatement表示从存储过程中获取结果及数据的方法;
下面我们创建自定义typeHandler

RoleMapper.java
package com.learn.charter2.mapper;
import java.util.List;
import com.learn.charter2.po.Role;
public interface RoleMapper {
Role getRole(Long id) throws Exception;
int deleteRole(Long id) throws Exception;
int insertRole(Role role) throws Exception;
List<Role> findRole(Role role) throws Exception;
}
Role.java
package com.learn.charter2.po;
public class Role {
private Long id;
private String roleName;
private String note;
private Integer pk;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public Integer getPk() {
return pk;
}
public void setPk(Integer pk) {
this.pk = pk;
}
}
SqlSessionFactoryUtil.java
package com.learn.charter2.util; import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class SqlSessionFactoryUtil {
private static SqlSessionFactory sqlSessionFactory=null;
private static final Class class_lock=SqlSessionFactoryUtil.class;
private SqlSessionFactoryUtil() {
}
public static SqlSessionFactory initSqlSessionFactory(){
String resource="mybatis-config.xml";
InputStream inputStream=null;
try {
inputStream=Resources.getResourceAsStream(resource);
} catch (Exception e) {
Logger.getLogger(SqlSessionFactoryUtil.class.getName()).log(Level.SEVERE,null,e);
}
synchronized (class_lock) {
if(sqlSessionFactory==null){
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}
}
return sqlSessionFactory;
}
public static SqlSession openSqlSession(){
if(sqlSessionFactory==null){
initSqlSessionFactory();
}
return sqlSessionFactory.openSession();
}
}
log4j.properties
log4j.rootLogger=debug,stdout
log4j.logger.org.mybatis=debug
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C:%m%n
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeHandlers>
<typeHandler handler="com.learn.charter2.util.MyStringTypeHandler" javaType="string" jdbcType="VARCHAR"/>
<typeHandler handler="com.learn.charter2.util.MyIntergerTypeHandler" javaType="int" jdbcType="VARCHAR"/>
</typeHandlers>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="autoCommit" value="false" />
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="gys" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/learn/charter2/mapper/roleMapper.xml" />
</mappers>
</configuration>
roleMapper.xml
<?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.learn.charter2.mapper.RoleMapper"> <resultMap type="com.learn.charter2.po.Role" id="roleMap">
<id column="id" property="id" javaType="long" jdbcType="BIGINT"/>
<result column="role_name" property="roleName" javaType="string" jdbcType="VARCHAR"/>
<result column="note" property="note" typeHandler="com.learn.charter2.util.MyStringTypeHandler"/>
<result column="pk" property="pk" typeHandler="com.learn.charter2.util.MyIntergerTypeHandler"/>
</resultMap> <select id="getRole" parameterType="long" resultMap="roleMap">
select id,role_name as roleName,note from t_role where id=#{id}
</select> <select id="findRole" parameterType="com.learn.charter2.po.Role" resultMap="roleMap">
select id,role_name as roleName,note,pk from t_role
<where>
<if test="roleName !=null">
role_name like concat('%',#{roleName,javaType=string,jdbcType=VARCHAR,typeHandler=com.learn.charter2.util.MyStringTypeHandler},'%')
</if>
<if test="pk !=null">
and pk =#{pk,javaType=int,jdbcType=VARCHAR,typeHandler=com.learn.charter2.util.MyIntergerTypeHandler}
</if>
</where> </select> <insert id="insertRole" parameterType="com.learn.charter2.po.Role">
insert into t_role
(role_name,note)
values
(#{roleName},#{note})
</insert>
<delete id="deleteRole" parameterType="long">
delete from t_role where id=#{id}
</delete>
</mapper>
MyIntergerTypeHandler.java
package com.learn.charter2.util; import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import org.apache.log4j.Logger; import sun.security.action.GetIntegerAction; /**
* @MappedTypes({Integer.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
*
*/
public class MyIntergerTypeHandler implements TypeHandler<Integer> { private Logger log=Logger.getLogger(MyIntergerTypeHandler.class); private int getInt(String v){
if("a".equals(v)){
return 10;
}else if("b".equals(v)){
return 20;
}else if("c".equals(v)){
return 30;
}else{
return 60;
}
}
@Override
public Integer getResult(CallableStatement cs, int index)
throws SQLException {
log.info("使用我的IntegerTypeHandler,CallbleStatment下表获取字符串");
return getInt(cs.getString(index));
} @Override
public Integer getResult(ResultSet rs, int index) throws SQLException {
log.info("使用我的IntegerTypeHandler,ResultSet下标获取字符串");
return getInt(rs.getString(index));
} @Override
public Integer getResult(ResultSet rs, String colName) throws SQLException {
log.info("使用我的IntegerTypeHandler,ResultSet 列名获取字符串");
return getInt(rs.getString(colName));
} @Override
public void setParameter(PreparedStatement ps, int index, Integer value,JdbcType jt) throws SQLException {
log.info("使用我的IntegerTypeHandler==index:"+index+";value:"+value);
String v="";
if(value==1){
v="a";
}else if(value==2){
v="b";
}else if(value==3){
v="c";
}else {
v="guoyansi";
}
ps.setString(index, v);
}
}
MyStringTypeHandler.java
package com.learn.charter2.util; import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import org.apache.log4j.Logger; @MappedTypes({String.class})
@MappedJdbcTypes(JdbcType.INTEGER)
public class MyStringTypeHandler implements TypeHandler<String> {
private Logger log=Logger.getLogger(MyStringTypeHandler.class); @Override
public String getResult(CallableStatement cs, int index)
throws SQLException {
log.info("使用我的StringTypeHandler,CallbleStatment下表获取字符串");
return cs.getString(index);
} @Override
public String getResult(ResultSet rs, int index) throws SQLException {
log.info("使用我的StringTypeHandler,ResultSet下标获取字符串");
return rs.getString(index);
} @Override
public String getResult(ResultSet rs, String colName) throws SQLException {
log.info("使用我的StringTypeHandler,ResultSet 列名获取字符串");
return rs.getString(colName);
} @Override
public void setParameter(PreparedStatement ps, int index, String value,
JdbcType jt) throws SQLException {
value=value+"m";
log.info("使用我的StringTypeHandler==index:"+index+";value:"+value);
ps.setString(index, value);
} }
Charter2Main.java
package com.learn.charter2.main; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.learn.charter2.mapper.RoleMapper;
import com.learn.charter2.po.Role;
import com.learn.charter2.util.SqlSessionFactoryUtil; public class Charter2Main {
public static void main(String[] args) {
SqlSession sqlSession = null;
try {
sqlSession = SqlSessionFactoryUtil.openSqlSession();
RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
Role role = new Role();
role.setPk(10);
List<Role> list=roleMapper.findRole(role);
for(Role r:list){
System.out.println("id:"+r.getId()+";roleName:"+r.getRoleName()+";note:"+r.getNote()+";pk:"+r.getPk());
}
} catch (Exception e) {
System.err.println(e.getMessage());
sqlSession.rollback();
}finally{
if(sqlSession!=null){
sqlSession.close();
}
}
} }
自定义typeHandler的三个步骤:
1.定义typeHandler(MyIntergerTypeHandler.java MyStringTypeHandler.java)
2.配置typeHandler(mybatis-config.xml中3-6行)
3.指定入参中哪个字段使用typeHandler(mapper.xml中19-24行)
4.指定出参中哪个字段使用typeHandler(mapper.xml中7-8行)
以MyIntergerTypeHandler为例,运行一下Charter2Main.java

Charter2Main 中传入的pk参数是10,被typeHandler默默的转换成了guoyansi;
因为guoyansi在数据库中找到了值,在被返回到java中,guoyansi又被typeHandler转化成了60;
这个例子展示的就是Integer和String之间的转换。
mybatis的typeHandler的更多相关文章
- Mybatis使用TypeHandler实现数据的加解密转换
参考: MyBatis之TypeHandler: https://www.cnblogs.com/yulinfeng/p/5991170.html 前段时间收到这么个需求:为安全起见,要求在数据库 ...
- mybatis 枚举typeHandler
枚举typeHandler 在绝大多数情况下,typeHandler因为枚举而使用,MyBatis已经定义了两个类作为枚举类型的支持,这两个类分别是: •EnumOrdinalTypeHandler. ...
- [转]Mybatis之TypeHandler使用教程
Mybatis之TypeHandler使用教程 https://blog.csdn.net/jokemqc/article/details/81326109 深入浅出Mybatis系列(五)---Ty ...
- 使用Mybatis的TypeHandler加解密数据
使用Mybatis的TypeHandler加解密数据 一.背景 二.解决方案 三.需求 四.实现思路 1.编写一个实体类,凡是此实体类的数据都表示需要加解密的 2.编写一个加解密的`TypeHandl ...
- MyBatis之TypeHandler
在大学写web应用的时候经常会遇到这么个问题,当我要插入一条数据,某个数据是Date类型,数据库中却是VARCHAR类型,这个时候可能会傻乎乎的先把这个数据自己手动转换成String类型再插入到数据库 ...
- 关于mybatis中typeHandler的两个案例
在做开发时,我们经常会遇到这样一些问题,比如我有一个Java中的Date数据类型,我想将之存到数据库的时候存成一个1970年至今的毫秒数,怎么实现?再比如我有一个User类,User类中有一个属性叫做 ...
- mybatis的TypeHandler 的使用
今天看了别人的mybatis的教学视频,自己手写了一个简单的自定义的TypeHandler,有些细节记录一下. 1.定义自己的TypeHandler,代码如下: package com.example ...
- Mybatis自定义TypeHandler解决特殊类型转换问题
我们知道,Java和MySQL中的数据类型是不同的,Java中除了基本数据类型,还有对象. 有时候使用MySQL存储数据,或者从MySQL中读取数据时,会有一些特殊需求
- 深入浅出Mybatis系列(五)---TypeHandler简介及配置(mybatis源码篇)
上篇文章<深入浅出Mybatis系列(四)---配置详解之typeAliases别名(mybatis源码篇)>为大家介绍了mybatis中别名的使用,以及其源码.本篇将为大家介绍TypeH ...
随机推荐
- python 二分法模板——牢记
class Solution: # @param nums: The integer array # @param target: Target number to find # @return th ...
- [Leetcode 18]四数之和 4 Sum
[题目] Given an array nums of n integers and an integer target, are there elements a, b, c, and d in n ...
- LeetCode 47 全排列II
题目: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 与上一题相比,这题多了一 ...
- 理解linux下的load
我们在做Linux负载计算的时候,我们需要了解负载的几个概念 1)Linux负载是什么 2)Linux负载怎么计算 3)如何区分目前负载是“好”还是“坏” 4)什么时候应该注意哪些不正常的值 1) ...
- document.write与document.getElementById的区别
document.write改变的是整个HTML页面(文档),document.getElementById("demo").innerHTML= 改变的是局部属性
- ubuntu svn服务本地搭建使用
安装 sudo apt-get install subversion 创建一个仓库 svnadmin create mysvn 编辑配置文件 /home/exayong/mysvn就是上面创建的目录 ...
- HashMap & Hashtable & CocurrentHashMap 与 ArrayList & CopyOnWriteArrayList
1. 同步集合类如Hashtable和Vector虽能做到线程安全,但分别使用Collections.synchronizedMap()方法和Collections.synchronizedList( ...
- Verilog中的$display和$write任务
$display(p1,p2, …,pn); $write(p1,p2, …,pn); 这两个函数和系统任务的作用都是用来输出信息,即将参数p2到pn按参数p1给定的格式输出.参数p1通常称为:“格式 ...
- winform rar压缩包解压缩
/// <summary> /// 解压缩 /// </summary> /// <param name="path& ...
- tp5阿里云短信发送
到阿里云下载php版demo,下完整版的,不是轻量级的; 框架 :TP5 把下载下来的文件放到extend里面 文件名:alimsg 里面的文件 import('alimsg.api_demo.Sm ...