【MyBatis框架】mapper配置文件-foreach标签

小灰灰 2021-09-25 23:00 597阅读 0赞

foreach标签

下面介绍一下一个mapper配置文件中的foreach标签(注意,要跟着前面的总结来看,这里使用的例子是结合前面的工程写的,大部分代码没有再赘述)

foreach的作用是向sql传递数组或List,mybatis使用foreach解析

1.1需求
在用户查询列表和查询总数的statement中增加多个id输入查询。
sql语句如下:

两种方法:
SELECT * FROM USER WHERE id=1 OR id=3 OR id=5

SELECT * FROM USER WHERE id IN(1,3,5)

1.2在输入参数类型中添加List ids传入多个id
public class UserQueryVo {

//传入多个id
private List ids;

public List getIds() {
return ids;
}

public void setIds(List ids) {
this.ids = ids;
}
……
}

1.3修改mapper.xml

WHERE id=1 OR id=3 OR id=5
在查询条件中,查询条件定义成一个sql片段,需要修改sql片段。

[html] view plain copy

  1. <**pre name=”code” class=”html”>**<!— 定义sql片段
  2. id:sql片段的唯一标识
  3. 在sql片段中不要加入where
  4. 经验:一般我们定义sql片段是为了可重用性,是基于单表来定义sql片段,
  5. 这样的话这个sql片段可重用性才高—>
  6. <**sql id=”query_user_where”>**
  7. <**if test=”ids!=null”>**
  8. <!— 使用foreach遍历传入ids
  9. collection指定输入对象中集合属性
  10. item每次遍历生成的对象名
  11. open开始遍历时要拼接的串
  12. close开始遍历时要拼接的串
  13. separator遍历的两个对象中间需要拼接的串
  14. >
  15. <!— 使用实现下边的sql拼接
  16. WHERE (id=1 OR id=3 OR id=5)—>
  17. <**foreach collection=”ids” item=”user_id” open=”AND (“ close=”)” separator=”OR”>**
  18. id=#{user_id}
  19. </**foreach**>
  20. </**if**>
  21. </**sql**>
  22. <**select** id=”findUserList” parameterType=”cn.edu.hpu.mybatis.PO.UserQueryVo”
  23. resultType=”cn.edu.hpu.mybatis.PO.UserCustom”>
  24. select * from user
  25. <**where**>
  26. <**include refid=”query_user_where”></include**>
  27. </**where**>
  28. </**select**>

在mapper接口类中添加相应方法:

[java] view plain copy

  1. //用户管理的Dao接口
  2. public interface UserMapper {
  3. //用户信息综合查询
  4. public List findUserList(UserQueryVo userQueryVo) throws Exception;
  5. ……
  6. }

1.4测试代码

[java] view plain copy

  1. //用户信息综合查询
  2. @Test
  3. public void testFindUserList() throws Exception{
  4. SqlSession sqlSession=sqlSessionFactory.openSession();
  5. //创建UserMapper代理对象
  6. UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
  7. //创建包装对象,设置查询条件
  8. UserQueryVo userQueryVo=new UserQueryVo();
  9. //传入多个Id
  10. List ids=new ArrayList();
  11. ids.add(1);
  12. ids.add(3);
  13. ids.add(5);
  14. //将ids通过userQueryVo传入statement中
  15. userQueryVo.setIds(ids);
  16. //调用userMapper的方法
  17. List users=userMapper.findUserList(userQueryVo);
  18. for (int i = 0; i < users.size(); i++) {
  19. UserCustom user=(UserCustom)users.get(i);
  20. System.out.println(user.getId()+”:”+user.getUsername());
  21. }
  22. }

测试结果:
1:张三
3:刘莉莉
5:刘三姐

日志输出:

[plain] view plain copy

  1. DEBUG [main] - Opening JDBC Connection
  2. DEBUG [main] - Created connection 6867819.
  3. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@68cb6b]
  4. DEBUG [main] - ==> Preparing: select * from user WHERE ( id=? OR id=? OR id=? )
  5. DEBUG [main] - ==> Parameters: 1(Integer), 3(Integer), 5(Integer)
  6. DEBUG [main] - <== Total: 3

可以看到,sql语句select * from user WHERE ( id=? OR id=? OR id=? ) 通过foreach输出成功

1.5另外一个sql的实现:

[html] view plain copy

  1. <!— 使用实现下边的sql拼接
  2. AND ID IN(1,3,5)—>
  3. <**foreach collection=”ids” item=”user_id” open=”AND ID IN(“ close=”)” separator=”,”>**
  4. #{user_id}
  5. </**foreach**>

save_snippets.png

发表评论

表情:
评论列表 (有 0 条评论,597人围观)

还没有评论,来说两句吧...

相关阅读