foreach-遍历集合 -笔记要点

 <!--foreach 遍历标签
collection : 指定要遍历的集合;
list类型的参数会特殊处理封装在Map中,map的key就叫做list;
item ; 将当前遍历的元素复制给指定的变量;
separator : 每个元素之间的分隔符!
open : 遍历出所有的结果拼接到一个开始的字符!
close: 遍历出所有的结果拼接一个 结束的字符!
index : 索引,遍历list的时候是索引,
遍历map的时候index表示的就是map的key,item就是map的值;
#{变量名},就能取出变量的值,也就是当前遍历出的元素
-->

出错分析与总结
1.定义接口

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

2.定义XML映射文件

    <!--public List<Employee> getEmpsByConditionForeach(Employee e);-->
<select id="getEmpsByConditionForeach" resultType="com.bean.Employee">
select * from tbl_employee
-- where id in (1,5,6)
<foreach collection="ids" item="item_id" separator=","
open="where id in (" close=")" index="">
#{item_id}
</foreach>
</select>

3.编写测试代码

 public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
} @Test
public void test11() throws Exception { SqlSession openSession = getSqlSessionFactory().openSession();
try {
System.out.println("++++++++++---- 1.测试 动态SQL元素:foreach!");
EmployeeMapper_DynamicSQL mapper = openSession.getMapper(EmployeeMapper_DynamicSQL.class); List<Employee> list = mapper.getEmpsByConditionForeach(Arrays.asList(1, 2, 3, 4));
for(Employee e:list)
System.out.println(e); openSession.commit();
} finally {
openSession.close();
} }

测试结果:

++++++++++---- 1.测试 动态SQL元素:foreach!
DEBUG 12-05 16:25:49,085 ==> Preparing: select * from tbl_employee -- where id in (1,5,6) where id in ( ? , ? , ? , ? ) (BaseJdbcLogger.java:145)
DEBUG 12-05 16:25:49,104 ==> Parameters: 1(Integer), 2(Integer), 3(Integer), 4(Integer) (BaseJdbcLogger.java:145)
DEBUG 12-05 16:25:49,106 <== Total: 2 (BaseJdbcLogger.java:145)
Employee{id=1, lastName='Jerry2333', email='233@...', gender='1', dept=null}
Employee{id=4, lastName='葫芦娃', email='葫芦娃@163.com', gender='0', dept=null}

最新文章

  1. java数组的常用函数
  2. MyBatis3学习--来源自用户指南
  3. Mac 系统下的环境变量
  4. px4flow通过iic读取具体寄存器数据程序
  5. SVN服务器从Windows迁移到Linux
  6. 【行为型】TemplateMethod模式
  7. PL/SQL破解方法(不需要注册码)
  8. talos项目记录
  9. Silverlight学习(二)
  10. daemon 启动system V init 和 systemd 配置
  11. vue 图片切换动态绑定
  12. 《杜增强讲Unity之Tanks坦克大战》9-发射子弹时蓄力
  13. 【Spring源码分析系列】搭建Spring实现容器的基本实现
  14. 2018 青岛ICPC区域赛E ZOJ 4062 Plants vs. Zombie(二分答案)
  15. 内存泄漏检测工具VLD在VS2010中的使用举例
  16. Java 字符串 String
  17. [label][转载][paypal]paypal在线支付接口的WEB语言设置
  18. 【MySql】mysql 慢日志查询工具之mysqldumpslow
  19. can be found for element &#39;tx:annotation-driven&#39;
  20. PHP Regex

热门文章

  1. robot:根据条件主动判定用例失败或者通过
  2. 如何提高程序员的键盘使用效率(搬砖工人的自我修养(tucao))
  3. android基础----&gt;传感器的使用
  4. SQL优化——select
  5. [CF1209F]Koala and Notebook_堆优化dij
  6. 关于Python编码这一篇文章就够了
  7. @Scheduled注解各参数详解
  8. 更改 MATLAB 默认工作路径
  9. NOIP2017[提高组] 宝藏 题解
  10. PB笔记之窗口之间传递多参数的方法