//查询员工id'在给定集合中(1,6)的
public List<Employee> getEmpsByConditionForeach(@Param("ids")List<Integer> ids);

2.映射配置文件

<!--public List<Employee> getEmpsByConditionForeach(List<Integer> ids); -->
<select id="getEmpsByConditionForeach" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee
<!--
collection:指定要遍历的集合:
list类型的参数会特殊处理封装在map中,map的key就叫list
item:将当前遍历出的元素赋值给指定的变量
separator:每个元素之间的分隔符
open:遍历出所有结果拼接一个开始的字符
close:遍历出所有结果拼接一个结束的字符
index:索引。遍历list的时候是index就是索引,item就是当前值
遍历map的时候index表示的就是map的key,item就是map的值

#{变量名}就能取出变量的值也就是当前遍历出的元素
-->
<foreach collection="ids" item="item_id" separator=","
open="where id in(" close=")">
#{item_id}
</foreach>
</select>

3. 进行测试

@Test
public void testDynamicSql() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try{
List<Employee> list = mapper.getEmpsByConditionForeach(Arrays.asList(1,6));
for (Employee emp : list) {
System.out.println(emp);
}

}finally{
openSession.close();
}
}

collection:指定要遍历的集合的更多相关文章

  1. 集合—collection、iterator遍历集合

    一.collection接口 1.collection常用方法 点击查看代码 @Test public void test(){ //contains() Collection coll = new ...

  2. 第29天学习打卡(迭代器、泛型 、Collection工具类、set集合的特点及应用、Map集合的特点及应用)

    迭代器 对过程的重复,称为迭代. 迭代器是遍历Collection集合的通用方式,可以在对集合遍历的同时进行添加.删除等操作. 迭代器的常用方法 next():返回迭代的下一个元素对象 hasNext ...

  3. freemarker 直接使用List来遍历set集合,可能会报错

    转摘:http://www.javaweb1024.com/java/JavaWebzhongji/2015/04/08/528.html freemarker  直接使用List来遍历set集合,可 ...

  4. DOM操作中,遍历动态集合的注意事项。ex: elem.children

    elem.childNodes和elem.children返回的都是动态集合. 动态集合(live collection): 不实际存储元素和属性值 每次访问集合都重新查找DOM树 遍历动态集合:   ...

  5. (2)集合 遍历set集合

    set集合的一些方法 Set<String> set1=new HashSet<String>(); set1.add("a"); set1.add(&qu ...

  6. 用来遍历map集合的方法

    map集合是以键值对进行存储值的,所以遍历map集合无非就是获取键和值,根据实际需求,进行获取键和值. 1.无非就是通过map.keySet()获取到值,然后根据键获取到值. for(String s ...

  7. 遍历List集合,删除符合条件的元素

    List集合的遍历有三种方式:增强for循环,普通for循环,Iterator迭代器遍历 如果只是对集合进行遍历,以上三种循环都可正常遍历: (1)增强For循环遍历List集合 List<St ...

  8. 用<forEach>遍历list集合时,提示我找不到对象的属性

    <c:forEach items="${list}" var="item"> <tr> <td>${item.UserId} ...

  9. 遍历Map集合:java.util.Map.Entry、KeySet两种方式

    遍历Map集合的两种方式: 1.用KeySet Map.keySet(),返回一个存放所有key的set集合,通过遍历集合,根据key值取出所有的value值. Map<String,Strin ...

随机推荐

  1. git 笔记总结

    一 创建版本库 (1) $ mkdir learngit $ cd learngit $ pwd /Users/michael/learngit (2) git init 二 提交文件 git sta ...

  2. Solr IK分词器配置

    下载地址:https://search.maven.org/search?q=com.github.magese 分词器配置: 参考:https://www.cnblogs.com/mengjinlu ...

  3. (转)linux chattr lsattr 命令

    转http://www.ha97.com/5172.html PS:有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chattr命令的作用很大,其中一些功 ...

  4. python-装饰器2

    python-装饰器2 1.函数既“变量 def bar(): print("in the bar") def foo(): print("in the foo" ...

  5. SQLite 参数化查询

    SQLite参数化查询 首先给出两个参考博客: Sqlite DB sqlite3使用简介 贴出一段自己用的代码: #include <stdio.h> #include <stdl ...

  6. Linux系统nmtui/nmcli绑定双网卡为team

    今天给大家带来图形化界面网络配置工具—nmtui的使用方法,可以省去敲命令的繁琐,较少误操作,结果更加直观. 小知识: nmtui:Network Manager Text User Interfac ...

  7. Kendo UI for jQuery使用教程:入门指南

    [Kendo UI for jQuery最新试用版下载] Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support ...

  8. Rsync参数介绍

    Rsync参数介绍   一.Rsync Rsync是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据镜像同步备份的优秀工具.rsync适用于unix/linux/windows等多种操作 ...

  9. 误删除mysql的root账号

    误删除mysql的root账号 mysql 测试通过 # mysql 误删除root最高权限用户 操作步骤: .停止mysql服务 (如果你删除了root用户,但没有退出操作对话框,恭喜节省了些麻烦, ...

  10. shiro框架学习-3- Shiro内置realm

    1. shiro默认自带的realm和常见使用方法 realm作用:Shiro 从 Realm 获取安全数据 默认自带的realm:idae查看realm继承关系,有默认实现和自定义继承的realm ...