MyBatis 提供了很多默認類型處理器,參考官網地址:鏈接,除了官網提供的類型處理器,我們也可以自定義類型處理器。
具體做法爲:實現 org.apache.ibatis.type.TypeHandler
接口, 或繼承 org.apache.ibatis.type.BaseTypeHandler
類 , 然後可以選擇性地將它映射到一個 JDBC 類型。
測試類:com.yjw.demo.TypeHandlerTest
比如我們要自定義一個性別的枚舉類型處理器,實現步驟如下所示:
創建類型處理器
定義性別的枚舉
/**
* 性格枚舉
*
* @author yinjianwei
* @date 2018/09/27
*/
public enum Sex {
MALE(1, "男"), FEMALE(2, "女");
private Integer code;
private String value;
private Sex(Integer code, String value) {
this.code = code;
this.value = value;
}
/**
* 根據code獲得value
*
* @param code
* @return
*/
public static String getValue(Integer code) {
String value = null;
for (Sex sex : Sex.values()) {
if (sex.getCode().equals(code)) {
value = sex.getValue();
}
}
return value;
}
/**
* 根據code獲取sex
*
* @param code
* @return
*/
public static Sex getSex(Integer code) {
for (Sex sex : Sex.values()) {
if (sex.getCode().equals(code)) {
return sex;
}
}
return null;
}
/**
* @return the code
*/
public Integer getCode() {
return code;
}
/**
* @param code the code to set
*/
public void setCode(Integer code) {
this.code = code;
}
/**
* @return the value
*/
public String getValue() {
return value;
}
/**
* @param value the value to set
*/
public void setValue(String value) {
this.value = value;
}
}
創建性別類型處理器 SexEnumTypeHandler
/**
* 性別類型處理器
*
* @author yinjianwei
* @date 2018/09/27
*/
public class SexEnumTypeHandler extends BaseTypeHandler<Sex> {
/**
* 入參處理
*/
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Sex parameter, JdbcType jdbcType) throws SQLException {
ps.setInt(i, parameter.getCode());
}
/**
* 返回結果處理
*/
@Override
public Sex getNullableResult(ResultSet rs, String columnName) throws SQLException {
int code = rs.getInt(columnName);
if (rs.wasNull()) {
return null;
} else {
return Sex.getSex(code);
}
}
/**
* 返回結果處理
*/
@Override
public Sex getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
int code = rs.getInt(columnIndex);
if (rs.wasNull()) {
return null;
} else {
return Sex.getSex(code);
}
}
/**
* 存儲過程返回結果(CallableStatement)處理
*/
@Override
public Sex getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
int code = cs.getInt(columnIndex);
if (cs.wasNull()) {
return null;
} else {
return Sex.getSex(code);
}
}
}
這裏使用的是繼承 org.apache.ibatis.type.BaseTypeHandler
類的方式,重寫父類的四個方法,分別對入參和返回結果做了類型轉換處理。
配置類型處理器
有兩種方式配置類型處理器,一種是在 MyBatis 的配置文件中配置,可以實現類型處理器的自動發現;另外一種是顯式地爲那些 SQL 語句設置要使用的類型處理器。
MyBatis 配置文件中配置
application-dev.yml
mybatis: type-aliases-package: com.yjw.demo.mybatis.biz.pojo.entity;com.yjw.demo.mybatis.biz.pojo.query mapper-locations: classpath:mapper/*.xml configLocation: classpath:mybatis-config.xml # configuration: # lazy-loading-enabled: true # aggressive-lazy-loading: false
mybatis.configLocation:指定 MyBatis 的 XML 配置文件路徑。
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 javaType="com.yjw.demo.mybatis.common.constant.Sex" jdbcType="TINYINT" handler="com.yjw.demo.mybatis.common.type.SexEnumTypeHandler"/> </typeHandlers> </configuration>
mybatis-config.xml 文件配置 typeHandler,通過顯示的指定 javaType 和 jdbcType 實現類型處理器的自動發現,比如在調用如下 insert 配置的時候就不需要顯示的指定 typeHandler,就可以實現類型轉換的功能。
StudentMapper.xml
<insert id="insert" parameterType="studentDO" keyProperty="id" useGeneratedKeys="true">
insert into t_student (name, sex, selfcard_no, note)
values (
#{name,jdbcType=VARCHAR},
#{sex,jdbcType=TINYINT},
#{selfcardNo,jdbcType=BIGINT},
#{note,jdbcType=VARCHAR}
)
</insert>
顯示指定類型處理器
如果在 mybatis-config.xml 配置文件中沒有配置 typeHandler,可以在各個映射文件中顯示配置需要使用的類型處理器,也可以實現類型轉換的功能。
比如在調用如下 insert 配置的時候顯示的指定 typeHandler。
StudentMapper.xml
<insert id="insert" parameterType="studentDO" keyProperty="id" useGeneratedKeys="true">
insert into t_student (name, sex, selfcard_no, note)
values (
#{name,jdbcType=VARCHAR},
#{sex,jdbcType=TINYINT,typeHandler=com.yjw.demo.mybatis.common.type.SexEnumTypeHandler},
#{selfcardNo,jdbcType=BIGINT},
#{note,jdbcType=VARCHAR}
)
</insert>
上面的例子只展現了入參的類型轉換的效果,返回結果的效果參考 com.yjw.demo.mybatis.biz.dao.StudentDao#listByConditions 方法,typeHandler 的配置如下。
<resultMap id="BaseResultMap" type="studentDO"> <id column="id" jdbcType="BIGINT" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="sex" jdbcType="TINYINT" property="sex" /> <!--<result column="sex" jdbcType="TINYINT" property="sex" typeHandler="com.yjw.demo.mybatis.common.type.SexEnumTypeHandler"/>--> <result column="selfcard_no" jdbcType="BIGINT" property="selfcardNo" /> <result column="note" jdbcType="VARCHAR" property="note" /> </resultMap>
如果在 MyBatis 的配置文件(mybatis-config.xml)中配置了 typeHandler 了,這裏就不需要顯示的配置了。
MyBatis 實用篇