mybatis error: Could not set property ‘num‘ of ‘xxx‘ with value ‘[]‘

文章目录

详情

"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"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property"这个异常是MyBatis在设置属性时发生的反射异常。这个异常通常发生在设置属性值时传入的参数类型与属性类型不匹配的情况下。根据引用中的错误信息,异常是由于无法将值'1539102131662880769'赋给属性'id',导致反射异常。 为了解决这个问题,你可以检查变量类型是否正确。确保传入的参数类型与属性类型一致。另外,还可以检查是否在配置文件中正确设置了主键自增的方式,以确保在插入数据时能够正确生成主键值。引用中提到的配置可以尝试添加到MyBatis的配置文件中,确保使用数据库的主键自增方式。 如果仍然无法解决该问题,你可以查看其他相关资料,了解更多关于这个异常的处理方法,引用提供的文章可能会对你有所帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property ‘id‘ ...](https://blog.csdn.net/m0_45877477/article/details/125388756)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Mybatis报错: org.apache.ibatis.exceptions.PersistenceException解决办法](https://download.csdn.net/download/weixin_38524871/12828533)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值