org.apache.ibatis.type.EnumOrdinalTypeHandler<E>
:該類實現了枚舉類型和Integer類型的相互轉換。可是給轉換僅僅是將對應的枚舉轉換爲其索引位置,也就是"ordinal()"方法獲取到的值。對應自定義的int值,該類無能爲力。java
org.apache.ibatis.type.EnumTypeHandler<E>
:該類實現了枚舉類型和String類型的相互轉換。對於想將枚舉在數據庫中存儲爲對應的int值的狀況,該類沒辦法實現。sql
基於以上mybatis提供的兩個枚舉處理類的能力有限,所以只能本身定義對枚舉的轉換了。數據庫
EnumValueTypeHandler
該類須要繼承org.apache.ibatis.type.BaseTypeHandler<E>
,而後在重定義的方法中實現自有邏輯。apache
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
若是在 mybatis 的 xml 中配置 該 typehandler,則須要添加註解@MappedTypes
。在添加 typeHandler 註冊時使用具體的實現類註冊。app
配置文件以下:ide
<typeAliases> <!-- 爲自定義的 TypeHandler 指定別名,在 sql 的 xml 中便可使用別名訪問了 --> <typeAlias type="cn.followtry.mybatis.EnumValueTypeHandler" alias="enumValueTypeHandler"/> </typeAliases> <typeHandlers> <!--處理枚舉的值轉換--> <typeHandler handler="cn.followtry.mybatis.EnumValueTypeHandler"/> </typeHandlers>
自定義的 Enum 須要實現的接口this
/** * @author jingzz * @time 2016年8月17日 上午9:22:46 * @since 2016年8月17日 上午9:22:46 */ public interface EsnBaseEnum { public int getValue(); }
而實現了EsnBaseEnum
的枚舉示例以下:code
/** * 同步的類型 * @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方法獲取到對應枚舉的值。xml
到此,對於枚舉的簡單處理邏輯已經實現完成了,接下來就是如何配置來使用該自定義枚舉處理邏輯
首先,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指定的處理類來處理枚舉類型。