mapper.java
public interface MyBlogMapper {
List<MyBlog> selectByIds(List<Long> ids);
}
mapper.xml
<select id="selectByIds" resultType="org.apache.ibatis.example.domain.MyBlog">
select * from my_blog
<where>
id in
<foreach collection="ids" open="(" close=")" separator="," item="idValue">
#{idValue}
</foreach>
</where>
</select>
测试
@Test
public void testCollectionTag() {
List<Long> ids = new ArrayList<>();
ids.add(1L);
ids.add(2L);
List<MyBlog> myBlogList = mapper.selectByIds(ids);
}
运行结果
源码分析
根据控制台报错, 定位到抛异常的那一行代码
return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
private Object wrapCollection(final Object object) {
if (object instanceof Collection) {
StrictMap<Object> map = new StrictMap<Object>();
map.put("collection", object);
if (object instanceof List) {
map.put("list", object);
}
return map;
} else if (object != null && object.getClass().isArray()) {
StrictMap<Object> map = new StrictMap<Object>();
map.put("array", object);
return map;
}
return object;
}
根据该段代码可知, 当我们 mapper 方法中的参数是一个集合时, mybatis 会把这个集合参数重新封装到一个 map中, 而 map 中这个集合对应的key 写死成了 list.
然而我们 mapper.xml 中, 用的是 ids. map 中根本不存在这个 key, 所以就间接触发了标题中所描述的异常.
<select id="selectByIds" resultType="org.apache.ibatis.example.domain.MyBlog">
select * from my_blog
<where>
id in
<foreach collection="ids" open="(" close=")" separator="," item="idValue">
#{idValue}
</foreach>
</where>
</select>
正确的写法
ids 改成 list
<select id="selectByIds" resultType="org.apache.ibatis.example.domain.MyBlog">
select * from my_blog
<where>
id in
<foreach collection="list" open="(" close=")" separator="," item="idValue">
#{idValue}
</foreach>
</where>
</select>