解决:nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘XXX‘ not found

报错信息

org.mybatis.spring.MyBatisSystemException: 
nested exception is org.apache.ibatis.binding.BindingException: Parameter 'xxx' not found. Available parameters are [0, 1, param1, param2]

报错原因

1、在使用mybatis开发的时候,有时候需要传入多个参数进行查询,当传入多个参数时,不处理便会出现上面的异常报错,这时需要@Param注解
2、mapper.xml映射没有得到传入的参数,这个时候需要在mapper层变化传参。

现象一:多参未处理

错误代码(mapper.java):

void batchInsert(Integer userId,Integer[] roleIds);

正确代码(mapper.java):

void batchInsert(@Param("userId")Integer userId,@Param("roleIds")Integer[] roleIds);

---------------------------------------------------------------------------------------------------------------------------------
@Param:

1、用注解来简化xml配置的时候,@Param注解的作用是给参数命名,参数命名后就能根据名字得到参数值,正确的将参数传入sql语句中。
2、简单说就是 @Param(“x2”) String x1 在启动时生成一个 x2 的属性,把参数 x1 的值赋给 x2,这样就可以在申请了中使用 #{x2} 或者 ${x2} 获取变量
3、如果不配置@Param(“x2”),就不会有 x2 属性,只能用默认的 #{x1} 来取值。

现象二:Mapper.java传递参数与Mapper.xml使用参数不同

错误代码:
mapper.java

int batchUpdate(@Param("item") List<T> list);

mapper.xml

  <update id="batchUpdate">
    <foreach collection="list" index="index" item="item" open="" separator=";" close="">
        update community_fence_data set id = #{item.id}
        <if test="null != item.key1"> ,key1 = #{item.key1}</if>
        <if test="null != item.key2"> ,key2 = #{item.key2}</if>
        <if test="null != item.result"> ,result = #{item.result}</if>
        <if test="null != item.gaodeFence"> ,gaode_fence = #{item.gaodeFence}</if>
        where id = #{item.id}
	</foreach>
  </update>

改正方法:
在这里插入图片描述
---------------------------------------------------------------------------------------------------------------------------------
Mybatis中foreach的属性
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

--流星。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值