代码展示:
xml<if test="request.sendOutStatus !=null and request.sendOutStatus.size()>0">
AND b.send_out_status IN
<foreach collection="request.sendOutStatus" index="index" item="item" open="(" separator="," close=")" >
#{item}
</foreach>
</if>
先在if里判断长度和是不是满足自己需要的数据,然后遍历列表,把符合条件的进行组装,最后大概相当于
AND b.send_out_status IN (1,2,3)
xml<if test="request.sendOutStatus !=null and request.sendOutStatus.size()>0">
AND ( b.send_out_status IN
<foreach collection="request.sendOutStatus" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
<foreach collection="request.sendOutStatus" index="index" item="item" >
<choose>
<when test='item == "0"'>
or b.send_out_status is null
</when>
</choose>
</foreach>
)
</if>
第二种比第一种稍微进阶一点点,首先就是普通的遍历,然后再执行一次遍历,然后再拼接起来,因为有时候单次遍历不能满足条件,choose就是选择的意思,当遍历中有满足条件的就是执行一下 如果状态为零就相当于
AND b.send_out_status IN (1,2,3) or b.send_out_status is null
不为零则还是普通的方式
AND b.send_out_status IN (1,2,3)
foreach中除了choose以为还有otherwise等选择判断的方式
代码展示:
xml <insert id="insertForeach" parameterType="java.util.List" useGeneratedKeys="false">
insert into fund
( id,fund_name,fund_code,date_x,data_y,create_by,create_date,update_by,update_date,remarks,del_flag)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.id},
#{item.fundName},
#{item.fundCode},
#{item.dateX},
#{item.dataY},
#{item.createBy},
#{item.createDate},
#{item.updateBy},
#{item.updateDate},
#{item.remarks},
#{item.delFlag}
)
</foreach>
</insert>
代码展示:
xml<update id="updateNewStock" parameterType="java.util.List">
<foreach collection="list" item="bean" index="index" open="" close="" separator=";">
UPDATE green_beans
<set>
stock=#{bean.stock}
</set>
<where>
beanUid = #{bean.beanUid}
</where>
</foreach>
</update>
xml <if test="request.column!=null and request.column!=''">
order by ${request.column}
<choose>
<when test="request.dir!=null and request.dir!='' and request.dir=='asc'" >
asc
</when>
<when test="request.dir!=null and request.dir!='' and request.dir=='desc'" >
desc
</when>
</choose>
</if>
<if test="request.column==null and request.column==''">
order by t.discharge_time desc,t.PATIENT_NAME desc
</if>
本文作者:Weee
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!