我遇到这个问题的原因是:
我在java代码mapper层中的定义是这样的
public List<StaffDepApply > queryStaff(@Param("staffDepApply")StaffDepApply staffDepApply
,@Param("auth")Collection<?> authorities);
使用了@param注解来标注参数。
mapper.xml文件如下定义的
<select id="queryStaff" resultType="staffDepApply">
select * from tb_staff_deploy_apply <where>
<if test="apply_org_id!=null and apply_org_id!=''">
and apply_org_id =#{apply_org_id,jdbcType=VARCHAR}
</if>
<if test="apply_status!=null and apply_status!=''">
and apply_status =#{apply_status,jdbcType=VARCHAR}
</if> </where>
</select>
这样就会报错,Parameter 'XXX' not found. Available parameters are [1, 0, param1, param2]
报错原因:
Mybatis在解析XML对应的Java接口文件mapper时,当mapper接口传递超过一个以上的参数时,需要使用注解@Param绑定具体的参数名,这样才能在XML中通过绑定的名称来解析对应的属性值。
解决办法:
例如:mapper接口中存在两个参数对象,那么在XML文件中如果使用到绑定名称为staffDepApply对象中的属性apply_org_id,则应该这样传值:
staffDepApply.apply_org_id
修改后的mapper.xml文件
<select id="queryStaff" resultType="staffDepApply">
select * from tb_staff_deploy_apply <where>
<if test="staffDepApply.apply_org_id!=null and staffDepApply.apply_org_id!=''">
and apply_org_id =#{staffDepApply.apply_org_id,jdbcType=VARCHAR}
</if>
<if test="staffDepApply.apply_status!=null and staffDepApply.apply_status!=''">
and apply_status =#{staffDepApply.apply_status,jdbcType=VARCHAR}
</if> </where>
</select>
在使用到的参数名之前加入绑定对象名称,就不报错了。