1.目标语句
select * from test where id in(1,2,3);
2.Book.java中定义ArrayList类型
private List<Integer> bookIdList;
public void setBookIdList(List<Integer> bookIdList) {
this.bookIdList = bookIdList;
}
3.在controller中调用
List<Integer>list = new ArrayList<Integer>();
Book Book = new Book();
book.setBookIdList(list);
4.Mapper.xml中定义,注意parameterType的值,以及foreach的用法
<select id="getBookDetail"parameterType="com.ssm.shopping.pojo.Book" resultType="com.ssm.shopping.pojo.Book">
select * from test where id
<foreach collection="list" item="id" open="in("close=")" separator=",">#{id}</foreach>
</select>
这样拼接出来的sql语句就是目标语句
本文介绍了一种在MyBatis中使用List作为参数进行SQL拼接的方法,通过Book.java实体类中的ArrayList属性及相应的setter方法,在Controller层设置List内容,并在Mapper.xml文件中利用foreach标签实现动态SQL拼接。
1万+

被折叠的 条评论
为什么被折叠?



