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指定的處理類來處理列舉型別。