首页 技术 正文
技术 2022年11月14日
0 收藏 559 点赞 3,634 浏览 2106 个字

原文地址:https://blog.csdn.net/jason5186/article/details/40896043

foreach属性
属性    描述
item    循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。
具体说明:在list和数组中是其中的对象,在map中是value。
该参数为必选。
collection    要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象用map代替作为键。
当然在作为入参时可以使用@Param(“keyName”)来设置键,设置keyName后,list,array,map将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:
如果User有属性List ids。入参是User对象,那么这个collection = “ids”
如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = “ids.id”
上面只是举例,具体collection等于什么,就看你想对那个元素做循环。
该参数为必选。
separator    元素之间的分隔符,例如在in()的时候,separator=”,”会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。
open    foreach代码的开始符号,一般是(和close=”)”合用。常用在in(),values()时。该参数可选。
close    foreach代码的关闭符号,一般是)和open=”(“合用。常用在in(),values()时。该参数可选。
index    在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。

<select id=”countByUserList” resultType=”_int” parameterType=”list”>  
select count(*) from users  
  <where>  
    id in  
    <foreach item=”item” collection=”list” separator=”,” open=”(” close=”)” index=””>  
      #{item.id, jdbcType=NUMERIC}  
    </foreach>  
  </where>  
</select>

::select count(*) from users WHERE id in ( ? , ? )
 <insert id=”addList”>
        
        INSERT INTO DELIVER
            (
                 <include refid=”selectAllColumnsSql”/>
             )
         
          <foreach collection=”deliverList” item=”item” separator=”UNION ALL”>
                SELECT
                     #{item.id, jdbcType=NUMERIC},
                     #{item.name, jdbcType=VARCHAR}
                FROM DUAL
          </foreach>
    </insert>

::insert into deliver select ?,? from dual union all select ?,? from dual
<insert id=”ins_string_string”>  
        insert into string_string (key, value) values  
        <foreach item=”item” index=”key” collection=”map”  
            open=”” separator=”,” close=””>(#{key}, #{item})</foreach>  
    </insert>

::insert into string_string (key, value) values (?, ?) , (?, ?)  — mysql
<select id=”sel_key_cols” resultType=”int”>  
        select count(*) from key_cols where  
        <foreach item=”item” index=”key” collection=”map”  
            open=”” separator=”AND” close=””>${key} = #{item}</foreach>  
    </select>

::select count(*) from key_cols where col_a = ? AND col_b = ?  (一定要注意到$和#的区别,$的参数直接输出,#的参数会被替换为?,然后传入参数值执行。)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849