Caused by: org.apache.ibatis.binding.BindingException: Parameter ‘userName’ not found. Available parameters are [arg1, arg0, param1, param2]
出现这个问题的原因是因为sql语句的条件有性别条件,没有注意到把性别全部设置成一种了,debug的步骤:
1.先打断点,打到for的地方
2.debug运行,左边绿色的那个按钮是结束debug运行。
3.查看结果
4.直接去error处去找sql语句(选中部分)复制到Navicat中
5如图,把要查询的内容直接替换?,格式如图所示
UserMapper.xml代码
<select id="queryUserListLikeUserNameOrAge" resultType="User">
select * from tb_user where sex=1
<!-- choose:条件选择
when:test-判断条件,一旦有一个when成立,后续的when都不再执行
otherwise:所有的when都不成立时,才会执行
-->
<choose>
<when test="userName!=null and userName.trim()!=''">and user_name like '%' #{userName} '%'</when>
<when test="age != null">and age = #{age}</when>
<otherwise>and user_name = 'zhangsan' </otherwise>
</choose>
</select>
UserMapperTest.java代码
@Test
public void queryUserListLikeUserName(){
List<User> users = this.userMapper.queryUserListLikeUserName("zhang");
for (User user : users) {
System.out.println(user);
}
}