详情
"nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'num' of 'xxx' with value '[]' Cause: java.lang.IllegalArgumentException: argument type mismatch"
其中num
字段为Integer类型
原因
自定义了一个List<Integer>
与json
数组互相转换的TypeHandler
,并且在工程中注册了,于是mybatis在自动映射时将所有Integer类型的字段都当成了List<Integer>
来处理,所以才出现了以上的错误。
public class IntJsonArrayTypeHandler extends JsonArrayTypeHandler<Integer> {
@Override
protected TypeReference<List<Integer>> specificType() {
return new TypeReference<List<Integer>>() {
};
}
}
@MappedTypes({ List.class })
@MappedJdbcTypes({ JdbcType.VARCHAR })
public abstract class JsonArrayTypeHandler<T> extends BaseTypeHandler<List<T>> {
protected abstract TypeReference<List<T>> specificType();
private List<T> getListByJsonArrayString(String content) {
return StringUtils.hasText(content) ? JsonUtils.readList(content, this.specificType()) : new ArrayList<>();
}
...
...
}
解决
取消IntJsonArrayTypeHandler
的注册,在mapper xml中只对需要自定义映射的字段使用typeHandler
<result column="num" property="num" typeHandler="com.xxx.IntJsonArrayTypeHandler"/>