报错信息
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的属性