参考文章 http://www.cnblogs.com/caoyajun33-blog/p/6875169.html
在学习mybatis的时候,使用foreach遍历集合,出现如标题一样的错误
### Error querying database. Cause: org.apache.ibatis.binding.BindingException:
Parameter 'ids' not found. Available parameters are [collection, list]
接口代码:
public interface EmployeeMapperDynamicSQL {
//根据多个id查询
public List<Employee> getEmpsByConditionForeach(List<Integer> ids) ;
}
xml代码:
<!-- public List<Employee> getEmpsByConditionForeach(List<Integer> ids) -->
<select id="getEmpsByConditionForeach" resultType="com.atguigu.bean.Employee">
<!-- select * from tbl_employee where id in(1,2,3) 用foreach标签处理-->
select * from tbl_employee where id in
<!--
collection:指定要遍历的集合,
list类型的参数会特殊处理封装在map中,map的key就叫list
item:将当前便利出的元素赋值给指定的变量
separator:每个元素之间的分隔符
open:遍历出所有结果拼接一个开始的字符
close:遍历出所有结果拼接一个结束的字符
index:索引。遍历list的时候index是索引,item是当前值
如果遍历map,index表示的就是map的key,item就是map的值
#{变量名}:能取出变量的值,也就是当前遍历出的元素
-->
<foreach collection="ids" item="item_id" separator="," open="(" close=")">
#{item_id}
</foreach>
</select>
collection里我填的是ids,然后就报了找不到ids的错误。后来看了一下别人的解释和老师给的讲解注释,发现是
collection:指定要遍历的集合,
list类型的参数会特殊处理封装在map中,map的key就叫list
key是list,所以改成list就正常执行了。
over