mybatis使用case when按照条件更新
示例代码
<update id="updatenrpurchaseorderdetailbygoodsbarcode">
update nr_purchase_order_detail
set stored_num =
<foreach collection="barcodelist" item="item" index="index"
separator=" " open="case goods_barcode" close="end">
when #{item.barcode} then stored_num + #{item.receivenum}
</foreach>
,unstored_num =
<foreach collection="barcodelist" item="item" index="index"
separator=" " open="case goods_barcode" close="end">
when #{item.barcode} then actual_goods_num - stored_num
</foreach>
where goods_barcode in
<foreach collection="barcodelist" item="item" index="index" separator="," open="(" close=")">
#{item.barcode}
</foreach>
and parent_id = #{id}
</update><!--casewhen形式更新-->
<update id="testcasewhen" parametertype="java.util.map">
update ${user}
<set>
<trim prefix="id= case id" suffix="end,">
<foreach collection="list" item="param">
<if test="param.id != null and id != ''">
when #{param.id} then #{param.id}
</if>
</foreach>
</trim>
<trim prefix="username = case id" suffix="end,">
<foreach collection="list" item="param">
<if test="param.username != null and param.username != ''">
when #{param.} then #{param.username}
</if>
</foreach>
</trim>
<trim prefix="password = case id" suffix="end,">
<foreach collection="list" item="param">
<if test="param.password != null and param.password != ''">
when #{param.id} then #{param.password}
</if>
</foreach>
</trim>
</set>
<where>
id in
<foreach collection="list" item="param" separator="," open="(" close=")">
#{param.id}
</foreach>
</where>
</update>
说明
上述第一个示例中,按照条码进行批量更新,将所有的条码对应的入库数量和剩余数量进行更新。
第二个示例中,又使用了set标签和trim标签,感觉更为复杂。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论