2017-07-18 74 views
0

我有一個userModel包含兩個枚舉。我爲它們編寫了一個自定義枚舉處理程序。當選擇userModel時,處理程序將獲得第二個枚舉類型,但是會獲得第一個枚舉類型列名稱。所以有錯誤。你知道如何解決這個問題嗎?mybatis枚舉自定義處理程序錯誤

我的處理程序:

@MappedTypes({UserType.class, AccountType.class}) 
public class HasValueEnumTypeHandler<E extends Enum<E> & HasValue> extends BaseTypeHandler<E> { 
private Class<E> type; 
private final E[] enums; 
private final Logger logger = Logger.getLogger(this.getClass()); 

public HasValueEnumTypeHandler(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 E getNullableResult(ResultSet rs, String columnName) throws SQLException { 
    int value = rs.getInt(columnName); 
    if (rs.wasNull()) { 
     return null; 
    } 
    for (E enm : enums) { 
     if (value == enm.getValue()) { 
      return enm; 
     } 
    } 
    throw new IllegalArgumentException("Cannot convert " + value + " to " + type.getSimpleName()); 
} 

@Override 
public E getNullableResult(ResultSet rs, int columnName) throws SQLException { 
    int value = rs.getInt(columnName); 
    if (rs.wasNull()) { 
     return null; 
    } 
    for (E enm : enums) { 
     if (value == enm.getValue()) { 
      return enm; 
     } 
    } 
    throw new IllegalArgumentException("Cannot convert " + value + " to " + type.getSimpleName()); 
} 

@Override 
public E getNullableResult(CallableStatement cs, int columnName) throws SQLException { 
    int value = cs.getInt(columnName); 
    if (cs.wasNull()) { 
     return null; 
    } 
    for (E enm : enums) { 
     if (value == enm.getValue()) { 
      return enm; 
     } 
    } 
    throw new IllegalArgumentException("Cannot convert " + value + " to " + type.getSimpleName()); 
} 

@Override 
public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException { 
    ps.setInt(i, parameter.getValue()); 
} 

}

我mapper.xml:

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 
<mapper namespace="com.rmword.mapper.UserModelMapper"> 

<resultMap type="com.rmword.model.UserModel" id="usermap"> 
    <id column="user_id" property="userId" /> 
    <result column="password" property="password" /> 
    <result column="token" property="token" /> 
    <result column="register_time" property="registerTime" /> 
    <result column="user_type" property="userType" 
     typeHandler="com.rmword.utils.HasValueEnumTypeHandler" /> 
    <result column="account_type" property="accountType" 
     typeHandler="com.rmword.utils.HasValueEnumTypeHandler" /> 
</resultMap> 

<insert id="insertUser"> 
    insert into user (user_id, password, token, 
    register_time, user_type, account_type) values (
    #{userId}, 
    #{password}, #{token}, #{registerTime}, 
    #{userType, typeHandler=com.rmword.utils.HasValueEnumTypeHandler}, 
    #{accountType, 
    typeHandler=com.rmword.utils.HasValueEnumTypeHandler} 
    ) 
</insert> 

<select id="selectUserById" parameterType="String" resultMap="usermap"> 
    select * from user where user_id = #{userId} 
</select> 

我的兩個枚舉:

public enum AccountType implements HasValue { 
EMAIL(1), 
QQ(2), 
WEIBO(3); 

int value; 

AccountType(int value) { 
    this.value = value; 
} 

public int getValue() { 
    return value; 
} 
} 

public enum UserType implements HasValue { 
RMWORD(1); 

int value; 

UserType(int value) { 
    this.value = value; 
} 

public int getValue() { 
    return value; 
} 
} 

錯誤是

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'userType' of 'class com.rmword.model.UserModel' with value 'EMAIL' Cause: java.lang.IllegalArgumentException: argument type mismatch 

它混合使用這兩種枚舉。

回答