mybatis的typeHandler

typeHandler作用:

  1.传参时将javaType类型转换成jdbcType

  2.结果集中ResultSet中取值时,jdbcType转换为javaType;

系统自定义的typeHandler:

  mybatis系统内部定义了一系列的typeHandler;基本涵盖了我们正常使用的类型转换;如下

mybatis的typeHandler_第1张图片

选取一个系统自定义的typeHandler看看;

在包org.apache.ibatis.type下有一个StringTypeHandler.java

源码如下:

 View Code

StringTypeHandler继承了BaseTypeHandler;而BaseTypeHandler实现了接口TypeHandler,

BaseTypeHandler中代码

 View Code

 

getResult分别用columnName和columnIndex从ResultSet中获取数据,CallableStatement表示从存储过程中获取结果及数据的方法;

下面我们创建自定义typeHandler

mybatis的typeHandler_第2张图片

RoleMapper.java

 View Code

Role.java

 View Code

SqlSessionFactoryUtil.java

 View Code

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

 1  
 2 
 3     
 4         
 5         
 6     
 7     
 8         
 9             
10                 
11             
12             
13                 
14                 
15                 
16                 
17             
18         
19     
20     
21         
22     
23 

roleMapper.xml

 1  
 2 
 3     
 4     
 5         
 6         
 7         
 8         
 9     
10 
11 
12     
15     
16     
29     
30     
31         insert into t_role
32         (role_name,note)
33         values
34         (#{roleName},#{note})
35     
36     
37         delete from t_role where id=#{id}
38     
39 

MyIntergerTypeHandler.java

 1 package com.learn.charter2.util;
 2 
 3 
 4 import java.sql.CallableStatement;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 
 9 import org.apache.ibatis.type.JdbcType;
10 import org.apache.ibatis.type.MappedJdbcTypes;
11 import org.apache.ibatis.type.MappedTypes;
12 import org.apache.ibatis.type.TypeHandler;
13 import org.apache.log4j.Logger;
14 
15 import sun.security.action.GetIntegerAction;
16 
17 
18 /**
19  * @MappedTypes({Integer.class})
20    @MappedJdbcTypes(JdbcType.VARCHAR)
21  *
22  */
23 public class MyIntergerTypeHandler implements TypeHandler {
24     
25     private Logger log=Logger.getLogger(MyIntergerTypeHandler.class);
26 
27     private int getInt(String v){
28         if("a".equals(v)){
29             return 10;
30         }else if("b".equals(v)){
31             return 20;
32         }else if("c".equals(v)){
33              return 30;
34         }else{
35             return 60;
36         }
37     }
38     @Override
39     public Integer getResult(CallableStatement cs, int index)
40             throws SQLException {
41         log.info("使用我的IntegerTypeHandler,CallbleStatment下表获取字符串");
42         return getInt(cs.getString(index));
43     }
44 
45     @Override
46     public Integer getResult(ResultSet rs, int index) throws SQLException {
47         log.info("使用我的IntegerTypeHandler,ResultSet下标获取字符串");
48         return getInt(rs.getString(index));
49     }
50 
51     @Override
52     public Integer getResult(ResultSet rs, String colName) throws SQLException {
53         log.info("使用我的IntegerTypeHandler,ResultSet 列名获取字符串");
54         return getInt(rs.getString(colName));
55     }
56 
57     @Override
58     public void setParameter(PreparedStatement ps, int index, Integer value,JdbcType jt) throws SQLException {
59         log.info("使用我的IntegerTypeHandler==index:"+index+";value:"+value);
60         String v="";
61         if(value==1){
62             v="a";
63         }else if(value==2){
64             v="b";
65         }else if(value==3){
66             v="c";
67         }else {
68             v="guoyansi";
69         }
70         ps.setString(index, v);
71     }
72 }

MyStringTypeHandler.java

 1 package com.learn.charter2.util;
 2 
 3 
 4 import java.sql.CallableStatement;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 
 9 import org.apache.ibatis.type.JdbcType;
10 import org.apache.ibatis.type.MappedJdbcTypes;
11 import org.apache.ibatis.type.MappedTypes;
12 import org.apache.ibatis.type.TypeHandler;
13 import org.apache.log4j.Logger;
14 
15 
16 @MappedTypes({String.class})
17 @MappedJdbcTypes(JdbcType.INTEGER)
18 public class MyStringTypeHandler implements TypeHandler {
19     private Logger log=Logger.getLogger(MyStringTypeHandler.class);
20     
21     @Override
22     public String getResult(CallableStatement cs, int index)
23             throws SQLException {
24         log.info("使用我的StringTypeHandler,CallbleStatment下表获取字符串");
25         return cs.getString(index);
26     }
27 
28     @Override
29     public String getResult(ResultSet rs, int index) throws SQLException {
30         log.info("使用我的StringTypeHandler,ResultSet下标获取字符串");
31         return rs.getString(index);
32     }
33 
34     @Override
35     public String getResult(ResultSet rs, String colName) throws SQLException {
36         log.info("使用我的StringTypeHandler,ResultSet 列名获取字符串");
37         return rs.getString(colName);
38     }
39 
40     @Override
41     public void setParameter(PreparedStatement ps, int index, String value,
42             JdbcType jt) throws SQLException {
43         value=value+"m";
44         log.info("使用我的StringTypeHandler==index:"+index+";value:"+value);
45         ps.setString(index, value);
46     }
47     
48     
49 }

Charter2Main.java

 1 package com.learn.charter2.main;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.session.SqlSession;
 6 
 7 import com.learn.charter2.mapper.RoleMapper;
 8 import com.learn.charter2.po.Role;
 9 import com.learn.charter2.util.SqlSessionFactoryUtil;
10 
11 public class Charter2Main {
12     public static void main(String[] args) {
13         SqlSession sqlSession = null;
14         try {
15             sqlSession = SqlSessionFactoryUtil.openSqlSession();
16             RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
17             Role role = new Role();
18             role.setPk(10);
19             List list=roleMapper.findRole(role);
20             for(Role r:list){
21                 System.out.println("id:"+r.getId()+";roleName:"+r.getRoleName()+";note:"+r.getNote()+";pk:"+r.getPk());
22             }
23         } catch (Exception e) {
24             System.err.println(e.getMessage());
25             sqlSession.rollback();
26         }finally{
27             if(sqlSession!=null){
28                 sqlSession.close();
29             }
30         }
31     }
32 
33 }

 

自定义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

mybatis的typeHandler_第3张图片

Charter2Main 中传入的pk参数是10,被typeHandler默默的转换成了guoyansi;
因为guoyansi在数据库中找到了值,在被返回到java中,guoyansi又被typeHandler转化成了60;
这个例子展示的就是Integer和String之间的转换。

你可能感兴趣的:(mybatis的typeHandler)