mybatis foreach用法

1、collection:与choose when混合使用

<foreach item="item" index="index" collection="listData">                    
                       <choose>
                           <when test="item.type=='field'">
                              and ${item.field}=#{item.val1}
                           </when>
                           <when test="item.type=='range'">
                              and ${item.field} between #{item.val1} and #{item.val2} 
                           </when>
                           <otherwise>
                             <!-- donothing -->
                           </otherwise>
                        </choose>
</foreach>

2、sql批量插入:

insert into wm_out_order_${tabelName} (WMOO_ID, WMOO_CODE )   
      values 

<foreach collection="list" item="item" index="index" separator="," > 

( #{item.wmooId,jdbcType=INTEGER},#{item.wmooCode,jdbcType=VARCHAR}
      )

    </foreach> 

3、查询条件: 其中如果collection size不能过大,会导致foreach查询失败

 <select id="listExcel" parameterType="com.wlyd.fmcgwms.util.CondOrderLimit" resultMap="BaseResultMap">

select  * from wm_out_order_${tabelName} 

where 1=1 

 <if test="conditions.wmooCodes != null and conditions.wmooCodes != ''" >
    and o.WMOO_CODE in
    <foreach collection="conditions.wmooCodes" index="index" item="item" open="(" separator="," close=")">
     #{item}  
    </foreach>

 </if>

  </select>


多个条件:

<select id="trackForBatchExport" resultMap="BaseResultMap" parameterType="com.wlyd.fmcgwms.util.CondOrderLimit">
    select DISTINCT o.*,op.WMOP_BATCHPICK_NO wmooBatchpickNo,ca.EB_CARRIER_NAME wmooCarrierName,c.CDST_CUSTOMER_CODE wmooCustomerCode
    from wm_out_order_${conditions.companyCode} o
    LEFT JOIN wm_batch_picking_${conditions.companyCode} bp on o.WMOO_BAT_PIC_ID=bp.WMBP_ID
left join eb_carrier_${conditions.companyCode} ca on ca.EB_CARRIER_CODE = o.WMOO_ACTUALLY_CARRIER_CODE
    left join cd_customer_${conditions.companyCode} c on c.CDST_CUSTOMER_NAME = o.WMOO_CUSTOMER_NAME
LEFT JOIN wm_out_packlist_${conditions.companyCode} op on op.WMOP_OUTORDER_CODE = o.WMOO_CODE
where 1=1
    and( 
    <foreach collection="conditions.wmOutOrdersSearch" index="index" item="item" open="(" separator="or" close=")">
       (
        (o.WMOO_CODE  = #{item.wmooCode} and op.WMOP_BATCHPICK_NO = #{item.wmooBatchpickNo} and op.WMOP_BATCHPICK_NO !='')
       or
        (o.WMOO_CODE  = #{item.wmooCode} and (#{item.wmooBatchpickNo} is null or #{item.wmooBatchpickNo}=''))
       ) 
    </foreach> 
    )
  </select>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值