当前位置: 代码网 > it编程>编程语言>Java > mybatis使用case when按照条件进行更新方式

mybatis使用case when按照条件进行更新方式

2025年08月23日 Java 我要评论
mybatis使用case when按照条件更新示例代码<update id="updatenrpurchaseorderdetailbygoodsbarcode"> update

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标签,感觉更为复杂。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com