MyBatis中的自定義TypeHandler詳解
要實(shí)現(xiàn) typeHandler 就需要去實(shí)現(xiàn)接口 typeHandler,或者繼承 BaseTypeHandler(實(shí)際上,BaseTypeHandler 實(shí)現(xiàn)了 typeHandler 接口)。
自定義String類型的TypeHandler:
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.apache.log4j.Logger;
public class MyTypeHandler implements TypeHandler<String> {
Logger logger = Logger.getLogger(MyTypeHandler.class);
@Override
public void setParameter(PreparedStatement ps, int i, String parameter,
JdbcType jdbcType) throws SQLException {
logger.info("設(shè)置 string 參數(shù)【" + parameter + "】");
ps.setString(i, parameter);
}
@Override
public String getResult(ResultSet rs, String columnName)
throws SQLException {
String result = rs.getString(columnName);
logger.info("讀取 string 參數(shù) 1 【" + result + "】");
return result;
}
@Override
public String getResult(ResultSet rs, int columnIndex) throws SQLException {
String result = rs.getString(columnIndex);
logger.info("讀取string 參數(shù) 2【" + result + "】");
return result;
}
@Override
public String getResult(CallableStatement cs, int columnIndex)
throws SQLException {
String result = cs.getString(columnIndex);
logger.info("讀取 string 參數(shù) 3 【" + result + "】");
return result;
}
}定義的 typeHandler 泛型為 String,顯然我們要把數(shù)據(jù)庫的數(shù)據(jù)類型轉(zhuǎn)化為 String 型,然后實(shí)現(xiàn)設(shè)置參數(shù)和獲取結(jié)果集的方法。
但是這個(gè)時(shí)候還沒有啟用 typeHandler
它還需要做如下所示的配置。
<typeHandlers>
<typeHandler jdbcType="VARCHAR" javaType="string" handler="com.mybatis.test.MyTypeHandler"/>
</typeHandlers>配置完成后系統(tǒng)才會(huì)讀取它,這樣注冊(cè)后,當(dāng) jdbcType 和 javaType 能與 MyTypeHandler 對(duì)應(yīng)的時(shí)候,它就會(huì)啟動(dòng) MyTypeHandler。
有時(shí)候還可以顯式啟用 typeHandler,一般而言啟用這個(gè) typeHandler 有兩種方式
如下所示。
....
<resultMap id="roleMapper" type="role">
<result property="id" column="id"/>
<result property="roleName" column="role_name" jdbcType="VARCHAR" javaType="string"/>
<result property="note" column="note" typeHandler=" com.mybatis.test.MyTypeHandler"/>
</resultMap>
<select id="getRole" parameterType="long" resultMap="roleMapper">
select id,role_name,note from t_role where id = #{id}
</select>
<select id="findRoles" parameterType="string" resultMap="roleMapper">
select id, role_name, note from t_role
where role_name like concat('%',#{roleName, jdbcType=VARCHAR,
javaType=string}, '%')
</select>
<select id="findRoles2" parameterType="string" resultMap="roleMapper">
select id, role_name, note from t_role
where note like concat ('%', # {note, typeHandler=com.mybatis.test.MyTypeHandler},'%')
</select>
......注意,要么指定了與自定義 typeHandler 一致的 jdbcType 和 javaType,要么直接使用 typeHandler 指定具體的實(shí)現(xiàn)類。
在一些因?yàn)閿?shù)據(jù)庫返回為空導(dǎo)致無法斷定采用哪個(gè) typeHandler 來處理,而又沒有注冊(cè)對(duì)應(yīng)的 javaType 的 typeHandler 時(shí),MyBatis 無法知道使用哪個(gè) typeHandler 轉(zhuǎn)換數(shù)據(jù),我們可以采用這樣的方式來確定采用哪個(gè) typeHandler 處理,這樣就不會(huì)有異常出現(xiàn)了。
有時(shí)候由于枚舉類型很多,系統(tǒng)需要的 typeHandler 也會(huì)很多,如果采用配置也會(huì)很麻煩,這個(gè)時(shí)候可以考慮使用包掃描的形式,那么就需要按照以下代碼配置了。
<typeHandlertype>
<package name="com.mybatis.test"/>
</typeHandlertype>只是這樣就沒法指定 jdbcType 和 javaType 了,不過我們可以使用注解來處理它們。我們把 MyTypeHandler 的聲明修改一下,如下所示。
@MappedTypes(String.class)
@MappedjdbcTypes(jdbcType.VARCHAR)
public class MyTypeHandler implements TypeHandler<String>{
......
}到此這篇關(guān)于MyBatis中的自定義TypeHandler詳解的文章就介紹到這了,更多相關(guān)MyBatis自定義TypeHandler內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis?typeHandler接口的定義和使用
- Mybatis中自定義TypeHandler處理枚舉的示例代碼
- Mybatis的TypeHandler實(shí)現(xiàn)數(shù)據(jù)加解密詳解
- Mybatis中TypeHandler使用小結(jié)
- SpringBoot中MyBatis使用自定義TypeHandler的實(shí)現(xiàn)
- Mybatis使用typeHandler加密的實(shí)現(xiàn)
- MyBatis-Plus?中?typeHandler?的使用實(shí)例詳解
- MyBatis中TypeHandler的使用教程詳解
- MyBatis類型處理器TypeHandler的作用及說明
- MyBatis自定義TypeHandler實(shí)現(xiàn)字段加密解密
相關(guān)文章
MyBatis-Plus實(shí)現(xiàn)字段自動(dòng)填充功能的示例
本文主要介紹了MyBatis-Plus實(shí)現(xiàn)字段自動(dòng)填充功能的示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Java編程Iterator迭代器設(shè)計(jì)原理及實(shí)現(xiàn)代碼示例
這篇文章主要介紹了Java編程Iterator迭代器設(shè)計(jì)原理及實(shí)現(xiàn)代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
Junit Mockito實(shí)現(xiàn)單元測(cè)試方法介紹
JUnit是用于編寫和運(yùn)行可重復(fù)的自動(dòng)化測(cè)試開源測(cè)試項(xiàng)目框架,這樣可以保證我們的代碼按與其工作。JUnit可廣泛用于工業(yè)和作為支架(從命令行)或IDE(如IDE)內(nèi)單獨(dú)的java程序2022-09-09

