概述
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
- 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key
下面分别来看看上述三种情况的示例代码:
1.单参数List的类型:
- <select id="dynamicForeachTest" resultType="Blog">
- select * from t_blog where id in
- <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
- #{item}
- </foreach>
- </select>
上述collection的值为list,对应的Mapper是这样的
测试代码:
- @Test
- public void dynamicForeachTest() {
- SqlSession session = Util.getSqlSessionFactory().openSession();
- BlogMapper blogMapper = session.getMapper(BlogMapper.class);
- List<Integer> ids = new ArrayList<Integer>();
- ids.add(1);
- ids.add(3);
- ids.add(6);
- List<Blog> blogs = blogMapper.dynamicForeachTest(ids);
- for (Blog blog : blogs)
- System.out.println(blog);
- session.close();
- }
2.单参数array数组的类型:
- <select id="dynamicForeach2Test" resultType="Blog">
- select * from t_blog where id in
- <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
- #{item}
- </foreach>
- </select>
上述collection为array,对应的Mapper代码:
对应的测试代码:
- @Test
- public void dynamicForeach2Test() {
- SqlSession session = Util.getSqlSessionFactory().openSession();
- BlogMapper blogMapper = session.getMapper(BlogMapper.class);
- int[] ids = new int[] {1,3,6,9};
- List<Blog> blogs = blogMapper.dynamicForeach2Test(ids);
- for (Blog blog : blogs)
- System.out.println(blog);
- session.close();
- }
3.自己把参数封装成Map的类型
- <select id="dynamicForeach3Test" resultType="Blog">
- select * from t_blog where title like "%"#{title}"%" and id in
- <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
- #{item}
- </foreach>
- </select>
上述collection的值为ids,是传入的参数Map的key,对应的Mapper代码:
对应测试代码:
- @Test
- public void dynamicForeach3Test() {
- SqlSession session = Util.getSqlSessionFactory().openSession();
- BlogMapper blogMapper = session.getMapper(BlogMapper.class);
- final List<Integer> ids = new ArrayList<Integer>();
- ids.add(1);
- ids.add(2);
- ids.add(3);
- ids.add(6);
- ids.add(7);
- ids.add(9);
- Map<String, Object> params = new HashMap<String, Object>();
- params.put("ids", ids);
- params.put("title", "中国");
- List<Blog> blogs = blogMapper.dynamicForeach3Test(params);
- for (Blog blog : blogs)
- System.out.println(blog);
- session.close();
- }
详情请见:http://blog.csdn.net/changjh1/article/details/54563490
目前在用mybatis框架写,所以每当遇到foreach,基本上传的参数都是两个以上的,现在最常用的,是采用list形式
先建立一个实体类,写上所传参数,写上对应的get、set方法
实体类代码如下:
public class Comment {
//需要从service层传到dao层的两个参数:id,creator
String id;
String creator;
public Comment() {
super();
// TODO Auto-generated constructor stub
}
public Comment(String id, String creator) {
super();
this.id = id;
this.creator = creator;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
@Override
public String toString() {
return "Comment [id=" + id + ", creator=" + creator + "]";
}
}
//需要从service层传到dao层的两个参数:id,creator
String id;
String creator;
public Comment() {
super();
// TODO Auto-generated constructor stub
}
public Comment(String id, String creator) {
super();
this.id = id;
this.creator = creator;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
@Override
public String toString() {
return "Comment [id=" + id + ", creator=" + creator + "]";
}
}
对应的service层代码:
public void setRead(Map map) throws Exception{
Map map1 = new HashMap();
Map map1 = new HashMap();
//新建一个list,存放Comment实体类
List<Comment> list = new ArrayList<Comment>();
List<Comment> list = new ArrayList<Comment>();
String creator = map.get("userId").toString();
String ids = map.get("ids").toString();
String[] idsArray = ids.split(",");
for (int i = 0; i < idsArray.length; i++) {
String ids = map.get("ids").toString();
String[] idsArray = ids.split(",");
for (int i = 0; i < idsArray.length; i++) {
//将参数存放在实体类中
Comment comment = new Comment();
comment.setCreator(creator);
comment.setId(idsArray[i]);
list.add(comment);
}
if(list.size()>0){
dynamicsDao.setRead(list);
}
}
Comment comment = new Comment();
comment.setCreator(creator);
comment.setId(idsArray[i]);
list.add(comment);
}
if(list.size()>0){
dynamicsDao.setRead(list);
}
}
对应的dao层中代码:
public void setRead(List<Comment> list) throws Exception;
对应的mapper文件中代码:
<update id="setRead">
UPDATE personal_dynamics_comment
SET isRead = 1
WHERE id IN(
<foreach item="item" index="key" collection="list" open="" separator="," close="">
#{item.id}
</foreach>
)
AND creator IN(
<foreach item="item" index="key" collection="list" open="" separator="," close="">
#{item.creator}
</foreach>
)
AND STATUS=1
</update>
UPDATE personal_dynamics_comment
SET isRead = 1
WHERE id IN(
<foreach item="item" index="key" collection="list" open="" separator="," close="">
#{item.id}
</foreach>
)
AND creator IN(
<foreach item="item" index="key" collection="list" open="" separator="," close="">
#{item.creator}
</foreach>
)
AND STATUS=1
</update>
最后
以上就是朴实西装为你收集整理的foreach用法,常用的全部内容,希望文章能够帮你解决foreach用法,常用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复