参数传递为List时:
当传递一个 List 实例或者数组作为参数对象传给 Mybatis。此时Mybatis 会自动将它包装在一个 Map 中,用名称在作为键。List 实例将会以“list” 作为键,而数组实例将会以“array”作为键。所以,当我们传递的是一个List集合时,mybatis会自动把我们的list集合包装成以list为Key值的map。
DAO 层:
List<User> selectUserByIDs( List IDs);
XML文件:
<select id="selectUserByIDs" parameterType="java.util.List" resultType="user">
select * from user
<where>
<if test="IDs != null and IDs.size() >0">
<foreach collection="IDs" open=" and id in (" close=")" item="uid" separator=",">
#{uid}
</foreach>
</if>
</where>
</select>
报错信息:
org.apache.ibatis.binding.BindingException: Parameter ‘IDs’ not found. Available parameters are [collection, list]
方法一:将我们的XML中collection属性值直接设置为list
DAO 层:
List<User> selectUserByIDs( List IDs);
XML文件:
<select id="selectUserByIDs" parameterType="java.util.List" resultType="user">
select * from user
<where>
<if test="list != null and list.size() >0">
<foreach collection="list" open=" and id in (" close=")" item="uid" separator=",">
#{uid}
</foreach>
</if>
</where>
</select>
方法二: 利用注解@Param指定我们的入参名称
DAO层:
List<User> selectUserByIDs(@Param("IDs") List IDs);
XML文件:
<select id="selectUserByIDs" parameterType="java.util.List" resultType="user">
select * from user
<where>
<if test="IDs != null and IDs.size() >0">
<foreach collection="IDs" open=" and id in (" close=")" item="uid" separator=",">
#{uid}
</foreach>
</if>
</where>
</select>
提枪策马乘胜追击04-21 20:01
代码小兵92504-17 16:07
代码小兵98804-25 13:57
杨晶珍05-11 14:54