mybatis處理枚舉類的簡單方法
mybatis自帶對枚舉的處理類
org.apache.ibatis.type.EnumOrdinalTypeHandler<E> :該類實現了枚舉類型和Integer類型的相互轉換。
但是給轉換僅僅是將對應的枚舉轉換為其索引位置,也就是"ordinal()"方法獲取到的值。對應自定義的int值,該類無能為力。
org.apache.ibatis.type.EnumTypeHandler<E> :該類實現了枚舉類型和String類型的相互轉換。
對于想將枚舉在數據庫中存儲為對應的int值的情況,該類沒辦法實現。
基于以上mybatis提供的兩個枚舉處理類的能力有限,因此只能自己定義對枚舉的轉換了。
自定義mybatis的枚舉處理類EnumValueTypeHandler
該類需要繼承org.apache.ibatis.type.BaseTypeHandler<E> ,然后在重定義的方法中實現自有邏輯。
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
/**
* 處理實現了{@link EsnBaseEnum}接口的枚舉類
* @author followtry
* @time 2016年8月16日 下午8:06:49
* @since 2016年8月16日 下午8:06:49
*/
//在 xml 中添加該 TypeHandler 時需要使用該注解
@MappedTypes(value = {
QcListTypeEnum.class,
SellingQcBizTypeEnum.class
})
public class EnumValueTypeHandler<E extends EsnBaseEnum> extends BaseTypeHandler<E> {
private Class<E> type;
private final E[] enums;
public EnumValueTypeHandler(Class<E> type) {
if (type == null) {
throw new IllegalArgumentException("Type argument cannot be null");
}
this.type = type;
this.enums = type.getEnumConstants();
if (this.enums == null) {
throw new IllegalArgumentException(type.getSimpleName() + " does not represent an enum type.");
}
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
//獲取非空的枚舉的int值并設置到statement中
ps.setInt(i, parameter.getValue());
}
@Override
public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
int i = rs.getInt(columnName);
if (rs.wasNull()) {
return null;
} else {
try {
return getEnumByValue(i);
} catch (Exception ex) {
throw new IllegalArgumentException(
"Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
}
}
}
/**
* 通過枚舉類型的int值,獲取到對應的枚舉類型
* @author jingzz
* @param i
*/
protected E getEnumByValue(int i) {
for (E e : enums) {
if (e.getValue() == i) {
return e;
}
}
return null;
}
@Override
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
int i = rs.getInt(columnIndex);
if (rs.wasNull()) {
return null;
} else {
try {
return getEnumByValue(i);
} catch (Exception ex) {
throw new IllegalArgumentException(
"Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
}
}
}
@Override
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
int i = cs.getInt(columnIndex);
if (cs.wasNull()) {
return null;
} else {
try {
return getEnumByValue(i);
} catch (Exception ex) {
throw new IllegalArgumentException(
"Cannot convert " + i + " to " + type.getSimpleName() + " by ordinal value.", ex);
}
}
}
}
該處理器是處理繼承了EsnBaseEnum接口的枚舉類,因為該接口中定義了獲取自定義int值的方法。
如果在 mybatis 的 xml 中配置 該 typehandler,則需要添加注解@MappedTypes。在添加 typeHandler 注冊時使用具體的實現類注冊。
配置文件如下:
<typeAliases> <!-- 為自定義的 TypeHandler 指定別名,在 sql 的 xml 中即可使用別名訪問了 --> <typeAlias type="cn.followtry.mybatis.EnumValueTypeHandler" alias="enumValueTypeHandler"/> </typeAliases> <typeHandlers> <!--處理枚舉的值轉換--> <typeHandler handler="cn.followtry.mybatis.EnumValueTypeHandler"/> </typeHandlers>
自定義的 Enum 需要實現的接口
/**
* @author jingzz
* @time 2016年8月17日 上午9:22:46
* @since 2016年8月17日 上午9:22:46
*/
public interface EsnBaseEnum {
public int getValue();
}
而實現了EsnBaseEnum的枚舉示例如下:
/**
* 同步的類型
* @author jingzz
* @time 2016年8月3日 上午11:13:06
*/
public enum SyncType implements EsnBaseEnum {
DEPT(3),//部門
PERSON(1);//人員
private int value;
private SyncType(int value) {
this.value = value;
}
@Override
public int getValue(){
return this.value;
}
}
只有在實現了EsnBaseEnum的接口,EnumValueTypeHandler才能通過接口的getValue方法獲取到對應枚舉的值。
到此,對于枚舉的簡單處理邏輯已經實現完成了,接下來就是如何配置來使用該自定義枚舉處理邏輯
配置對枚舉的處理
首先,mybatis中對于處理邏輯的設置是在sql的映射文件中,如EsnSyncLogMapper.xml。
關鍵的設置枚舉處理的位置如下:
<resultMap id="BaseResultMap" type="com.test.dao.model.EsnSyncLog" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="sync_time" property="syncTime" jdbcType="TIMESTAMP" /> <result column="total_num" property="totalNum" jdbcType="INTEGER" /> <result column="success_total_num" property="successTotalNum" jdbcType="INTEGER" /> <result column="type" property="type" typeHandler="com.test.common.EnumValueTypeHandler" /> <result column="msg" property="msg" jdbcType="VARCHAR" /> </resultMap>
其中type列設置了屬性typeHandler,其值為自定義的枚舉處理邏輯。
而在具體sql中也需要使用typeHandler屬性,如:
<if test="type != null" >
#{type, typeHandler=com.test.common.EnumValueTypeHandler},
</if>
在mybatis處理到該位置時,就會調用typeHandler指定的處理類來處理枚舉類型。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
相關文章
解決日期轉化Json異常- Date JSON parse error
這篇文章主要介紹了解決日期轉化Json異常- Date JSON parse error問題。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
spring-cloud-gateway動態(tài)路由的實現方法
這篇文章主要介紹了spring-cloud-gateway動態(tài)路由的實現方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Java中l(wèi)ong類型與Long類型的區(qū)別和大小比較詳解
這篇文章主要給大家介紹了Java中l(wèi)ong類型與Long類型區(qū)別和大小比較的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-11-11
SpringBoot配置Profile實現多環(huán)境支持
這篇文章主要介紹了SpringBoot配置Profile實現多環(huán)境支持操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08

