我有一个包含两个枚举的userModel。我为它们编写了一个自定义枚举处理程序。当选择userModel时,处理程序获取第二个枚举类型,但第一个枚举类型列名称。因此,有错误。你知道怎么解决这个问题吗?
我的经纪人:
@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
它混合了两个枚举。