zoukankan      html  css  js  c++  java
  • collection:指定要遍历的集合

    //查询员工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();
    }
    }

  • 相关阅读:
    HDU 1160 dp中的路径问题
    zzuli 1907: 小火山的宝藏收益
    POJ 3414 dfs广搜直接应用
    http://acm.zzuli.edu.cn/zzuliacm/problem.php?cid=1158&pid=5 二分函数的间接应用
    LightOJ 1067 组合数取模
    九段美到极致的句子
    质数和分解
    codevs 1080 线段树练习
    codevs 2806 红与黑
    codevs 2152 滑雪
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/8567232.html
Copyright © 2011-2022 走看看