一直用hibernate,习惯了枚举类的使用,非常方便,现在用mybatis,发现枚举的处理,还是需要单独处理下。
如果想使用mybatis自带的枚举类处理,有2种方式,一个是EnumTypeHandler,一个是EnumOrdinalTypeHandler。
 2者的区别是EnumTypeHandler直接存储name值,而EnumOrdinalTypeHandler会存储enum类里的序号值,此时数据库表字段一般用int类型的处理。
 
使用方式比较简单,直接在mapper文件里的字段上,加上
typeHandler=“org.apache.ibatis.type.EnumOrdinalTypeHandler”
或
typeHandler=“org.apache.ibatis.type.EnumTypeHandler”
然后insert或者update的地方,字段处也加上相应的typeHandler即可。
但通常的情况下,我们并不希望用序号来标记,而希望通过自定义的id值来存储,这时我们就需要自己写enum的处理类。
为了使处理类通用,我们用泛型来处理,首先定义一个接口类:
-  public interface IntEnum<E extends Enum<E>> {
-  int getIntValue();
-  }
===
然后自己的枚举类,去实现这个接口,主要是实现getIntValue方法:
-  public enum TestStatusEnum implements IntEnum<TestStatusEnum> {
-  FAILURE(0, "failure"), SUCCESS(1, "success");
-  private int index;
-  private String name;
-  //....get set
-  private TestStatusEnum(int index, String name) {
-  this.index = index;
-  this.name = name;
-  }
 ===
-  public static String fromIndex(int index) {
 =============================================
-  for (TestStatusEnump : TestStatusEnum.values()) {
 ===================================================
-  if (index == p.getIndex())
 ============================
-  return p.name;
 ================
-  }
 ===
-  return null;
 ==============
-  }
 ===
-  @Override
 ===========
-  public int getIntValue() {
 ============================
-  return this.index;
 ====================
-  }
 ===
pojo类里的status字段属性,定义为 TestStatusEnum status。
然后自定义一个hanler类:
-  public class IntEnumTypeHandler<E extends Enum<E> & IntEnum<E>> extends
-  BaseTypeHandler<IntEnum> {
-  private Class<IntEnum> type;
-  public IntEnumTypeHandler(Class<IntEnum> type) {
-  if (type == null)
-  throw new IllegalArgumentException("Type argument cannot be null");
-  this.type = type;
-  }
-  private IntEnum convert(int status) {
 =======================================
-  IntEnum[] objs = type.getEnumConstants();
 ===========================================
-  for (IntEnum em : objs) {
 ===========================
-  if (em.getIntValue() == status) {
 ===================================
-  return em;
 ============
-  }
 ===
-  }
 ===
-  return null;
 ==============
-  }
 ===
-  @Override
 ===========
-  public IntEnum getNullableResult(ResultSet rs, String columnName)
 ===================================================================
-  throws SQLException {
 =======================
-  return convert(rs.getInt(columnName));
 ========================================
-  }
 ===
-  @Override
 ===========
-  public IntEnum getNullableResult(ResultSet rs, int columnIndex)
 =================================================================
-  throws SQLException {
 =======================
-  return convert(rs.getInt(columnIndex));
 =========================================
-  }
 ===
-  @Override
 ===========
-  public IntEnum getNullableResult(CallableStatement cs, int columnIndex)
 =========================================================================
-  throws SQLException {
 =======================
-  return convert(cs.getInt(columnIndex));
 =========================================
-  }
 ===
-  @Override
 ===========
-  public void setNonNullParameter(PreparedStatement ps, int i,
 ==============================================================
-  IntEnum enumObj, JdbcType jdbcType) throws SQLException {
 ===========================================================
-  // baseTypeHandler已经帮我们做了parameter的null判断
 ===========================================
-  ps.setInt(i, enumObj.getIntValue());
 ======================================
-  }
 ===
-  }
 ===
===










