zoukankan      html  css  js  c++  java
  • mybatis的xml中sql语句中in的写法(迭代遍历)

    这里使用 foreach标签


    <foreach  item="item" collection="listTag" index="index"  open="(" separator="," close=")">

    #{item}

    </foreach>


    foreach元素的属性主要有 item,index,collection,open,separator,close。

    item表示集合中每一个元素进行迭代时的别名.

    index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置.

    open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符.

    close表示以什么结束.

    1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

    <select id="addList" resultType="map">

    select * from tp_trade where  id in 

    <foreach  item="item" collection="list" index="index"  open="(" separator="," close=")">#{item}</foreach>

    </select>

    传入参数的代码为:

    List<Object>  addList(List<Object> ids);

    2.如果传入的是单参数且参数类型是一个Array数组的时候,collection属性值为array


    <select id="addArray" resultType="map">

    select * from tp_trade where  tt_type in 

    <foreach  item="item" collection="array" index="index"  open="(" separator="," close=")">#{item}</foreach>

    </select>

    传入的参数代码为:
    List<Object> addArray(String[]  ids);

    3.如果多个参数,我们会封装成map类型,然后在把需要遍历的list或者array封装到map中。

    传入的参数代码为:

    String str = "1,2,3,4";

    Map  map = new HashMap();

    map.put("type",str.spit(","));

    再把封装好map传入到方法中。

    List<Object> addMap(Map<String,Object> map);


    <select id="addMap" resultType="map">

    select * from tp_trade where  type in 

    <foreach  item="item" collection="type" index="index"  open="(" separator="," close=")">#{item}</foreach>

    </select>

    type就是数组集合,使用item遍历即可。

  • 相关阅读:
    数组是个好东西
    排列(permutation) 用1,2,3,…,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要 求abc:def:ghi=1:2:3。按照“abc def ghi”的格式输出所有解,每行一个解。
    子序列的和
    韩信点兵
    水仙花数
    阶乘之和
    3n+1问题
    MongoDB 安装
    mysql中bigint在php中表示
    Android之NDK开发
  • 原文地址:https://www.cnblogs.com/SmallStrange/p/13168900.html
Copyright © 2011-2022 走看看