mybatis动态sql中foreach标签的使用

foreach标签主要用于构建in条件,他可以在sql中对集合进行迭代。如下:

  <delete id="deleteBatch"> 

    delete from user where id in

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

      #{id}

    </foreach>

  </delete>

  我们假如说参数为----  int[] ids = {1,2,3,4,5}  ----那么打印之后的SQL如下:

  delete form user where id in (1,2,3,4,5)

  释义:

    collection :collection属性的值有三个分别是list、array、map三种,分别对应的参数类型为:List、数组、map集合,我在上面传的参数为数组,所以值为array

    item : 表示在迭代过程中每一个元素的别名

    index :表示在迭代过程中每次迭代到的位置(下标)

    open :前缀

    close :后缀

    separator :分隔符,表示迭代时每个元素之间以什么分隔

我们通常可以将之用到批量删除、添加等操作中。

 

再比如

查询:

<!-- 获取商户对账单的fileId -->
<select id="getBillInfoList" resultType="com.wondersgroup.soa.dto.bill.BillInfoEntity">
select *
from t_mer_file_mer_info
where batchNo in
<foreach collection="list" item="batchNo" index="index"
open="(" close=")" separator=",">
#{batchNo}
</foreach>
</select>

 

 

插入:

<insert id="batchinsertSelective" parameterType="tPortalAdjunctEntity" >
insert into t_portal_adjunct (merno, type,type_id, file_id, file_name)
values
<foreach collection ="list" item="info" index= "index" separator =",">
(#{info.merno}, #{info.type},
#{info.type_id}, #{info.file_id}, #{info.file_name})
</foreach >
</insert>

转载于:https://www.cnblogs.com/jianzhixuan/p/6434589.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值