foreach

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hao.mapper.UserMapper">
<select id="findByCondition" parameterType="user" resultType="user">
select * from user
<where>
<if test="id!=0">
and id=#{id}
</if>
<if test="username!=null">
and username=#{username}
</if>
<if test="password!=null">
and password=#{password}
</if>
</where>
</select>
<select id="findByIds" parameterType="list" resultType="user">
select * from user
<where>
<foreach collection="list" open="id in(" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
</mapper>

测试

 @Test
public void test1() throws IOException {
InputStream stream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSession sqlSession = new SqlSessionFactoryBuilder().build(stream).openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<Integer> ids=new ArrayList<>();
ids.add(1);
ids.add(2);
List<User> byIds = mapper.findByIds(ids);
System.out.println(byIds);
}

观看打印的日志

-collection 表示要做foreach循环的对象
- item 表示集合中每一个元素或者该集合的对象,支持对象点属性的方式获取属性。
- index 表示循环的下标,从0开始
- open 表示以什么开始
- separator 表示每次进行迭代之间以什么符号作为分隔符
- close表示以什么结束

最新文章

  1. 并查集 poj2236
  2. mysql load file
  3. 【Oralce】时间操作
  4. 在C#中使用C++编写的类
  5. 正则基础之——环视(Lookaround)
  6. WebConfig配置文件详解(转载自逆心的博客)
  7. Application+Handle+Task
  8. 代码高亮插件推荐——SyntaxHighlighter++
  9. js导航栏样式变换
  10. windows 2008 R2操作系统上使用iis服务运行php和mysql数据库的网站遇到的验证码不显示问题?
  11. tomcat 构建问题记录
  12. python - Random常用方法记录
  13. Python - IPython
  14. #incldue&lt;cctype&gt;函数系列
  15. AS语法
  16. com.sun.awt.AWTUtilities.setWindowOpacity相关说明
  17. Edit Control响应全选(Ctrl+A)
  18. Animator 设置动画效果
  19. fillna()
  20. Sqlserver 批量数据更改

热门文章

  1. 两个宝藏|关于我在github上冲浪时的一个小技巧。
  2. 5月4日 python学习总结 socketserver
  3. [SPDK/NVMe存储技术分析]009 - Introduction to RDMA Send | RDMA Send操作概论
  4. python监控cpu 内存实现邮件微信报警
  5. python 命令行参数学习(一)
  6. 实习项目1-串口IP升级调试
  7. tomcat启动 ssm项目出现乱码的解决
  8. 在Unity中用UGUI制作可输入下拉框
  9. python编程笔记--字符编码
  10. MybatisPlus 多租户的常见问题