首页 > 技术文章 > Mybatis TypeHandler接口

xh_chiang 2017-06-04 00:02 原文

 1 public interface TypeHandler<T> {  
 2 
 3    
 4 
 5     /** 
 6 
 7      * 用于定义在Mybatis设置参数时该如何把Java类型的参数转换为对应的数据库类型 
 8 
 9      * @param ps 当前的PreparedStatement对象 
10 
11      * @param i 当前参数的位置 
12 
13      * @param parameter 当前参数的Java对象 
14 
15      * @param jdbcType 当前参数的数据库类型 
16 
17      * @throws SQLException 
18 
19      */  
20 
21     void setParameter(PreparedStatement ps, int i, T parameter,  
22 
23            JdbcType jdbcType) throws SQLException;  
24 
25    
26 
27     /** 
28 
29      * 用于在Mybatis获取数据结果集时如何把数据库类型转换为对应的Java类型 
30 
31      * @param rs 当前的结果集 
32 
33      * @param columnName 当前的字段名称 
34 
35      * @return 转换后的Java对象 
36 
37      * @throws SQLException 
38 
39      */  
40 
41     T getResult(ResultSet rs, String columnName) throws SQLException;  
42 
43    
44 
45     /** 
46 
47      * 用于在Mybatis通过字段位置获取字段数据时把数据库类型转换为对应的Java类型 
48 
49      * @param rs 当前的结果集 
50 
51      * @param columnIndex 当前字段的位置 
52 
53      * @return 转换后的Java对象 
54 
55      * @throws SQLException 
56 
57      */  
58 
59     T getResult(ResultSet rs, int columnIndex) throws SQLException;  
60 
61    
62 
63     /** 
64 
65      * 用于Mybatis在调用存储过程后把数据库类型的数据转换为对应的Java类型 
66 
67      * @param cs 当前的CallableStatement执行后的CallableStatement 
68 
69      * @param columnIndex 当前输出参数的位置 
70 
71      * @return 
72 
73      * @throws SQLException 
74 
75      */  
76 
77     T getResult(CallableStatement cs, int columnIndex) throws SQLException;  
78 
79    
80 
81 }  

 

推荐阅读