解决mybatis使用枚举的转换

解决mybatis使用枚举的转换

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

蕃薯耀 2015年9月6日 16:21:28 星期日

http://fanshuyao.iteye.com/

一、第一种解决方法就是使用自带的typeHandler

org.apache.ibatis.type.EnumOrdinalTypeHandler

结果返回的例子:

<resultMap id="huserResultMap" type="com.chinagas.authorization.model.HUsers" >

    <result column="genders" property="genders" typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler"/>
  </resultMap>

更新数据的例子:

<update id="updateBasicInfoByHuser" parameterType="com.chinagas.authorization.model.HUsers" >
    update H_USERS
    set 
      REAL_NAME = #{realName},
      GENDERS = #{genders,typeHandler=org.apache.ibatis.type.EnumOrdinalTypeHandler},
      ADDRESS = #{address},
      NICKNAME = #{nickname}
    where USER_ID = #{userId}
  </update>

二、第二种是使用自定义的转换器

1、枚举

public enum GenderEnum {
	MALE{
		@Override
		public String getValue(){
			return "男";
		}
	},
	FEMALE{
		@Override
		public String getValue() {
			return "女";
		}
	},
	OTHER{
		@Override
		public String getValue() {
			return "未知";
		}
	};
	
	public int getKey(){
		return this.ordinal();
	}
	public abstract String getValue();
}

2、自定义的枚举处理转换器

public class GenderHandler extends BaseTypeHandler<GenderEnum> {

	private Class<GenderEnum> type;  
    
    private final GenderEnum[] enums;
    
    /** 
     * 设置配置文件设置的转换类以及枚举类内容,供其他方法更便捷高效的实现 
     * @param type 配置文件中设置的转换类 
     */  
    public GenderHandler(Class<GenderEnum> 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 GenderEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
		// 根据数据库存储类型决定获取类型,本例子中数据库中存放INT类型  
        int i = rs.getInt(columnName);  
        if (rs.wasNull()) {  
            return null;  
        } else {  
            // 根据数据库中的code值,定位EnumStatus子类  
            return locateGender(i);  
        }  
	}

	@Override
	public GenderEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		// 根据数据库存储类型决定获取类型,本例子中数据库中存放INT类型  
        int i = rs.getInt(columnIndex);  
        if (rs.wasNull()) {  
            return null;  
        } else {  
            // 根据数据库中的code值,定位EnumStatus子类  
            return locateGender(i);  
        } 
	}

	@Override
	public GenderEnum getNullableResult(CallableStatement cs, int columnIndex)
			throws SQLException {
		// 根据数据库存储类型决定获取类型,本例子中数据库中存放INT类型  
        int i = cs.getInt(columnIndex);  
        if (cs.wasNull()) {  
            return null;  
        } else {  
            // 根据数据库中的code值,定位EnumStatus子类  
            return locateGender(i);  
        }  
	}

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, GenderEnum parameter,
			JdbcType arg3) throws SQLException {
		ps.setInt(i, parameter.getKey()); 
		
	}
	
	/** 
     * 枚举类型转换,由于构造函数获取了枚举的子类enums,让遍历更加高效快捷 
     * @param code 数据库中存储的自定义code属性 
     * @return code对应的枚举类 
     */  
    private GenderEnum locateGender(int code) {  
        for(GenderEnum gender : enums) {  
            if(gender.getKey()==(Integer.valueOf(code))) {  
                return gender;  
            }  
        }  
        throw new IllegalArgumentException("未知的枚举类型:" + code + ",请核对" + type.getSimpleName());  
    } 

}

3、类似于第一种,需要在处理枚举类型上加上

typeHandler=com.xxx.xxx.xxx.GenderHandler

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

蕃薯耀 2015年9月6日 16:21:28 星期日

http://fanshuyao.iteye.com/

原文地址:https://www.cnblogs.com/fanshuyao/p/6227183.html