mybatis循环修改,when多个字段,where的in多个字段

本文介绍使用MyBatis通过foreach循环实现对多个记录的批量更新操作。针对不同的字段设置条件进行更新,并通过in子句指定要更新的记录范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mybatis–foreach循环update修改
判断时双主键或者多个判断 需要when多个字段 和 where的in多个字段判断

    <update id="updateAppSetDatas" parameterType="list">
        update app_set_data
        <set>
            <trim prefix="appName=case" suffix="end,">
                <foreach collection="appSetDatas" item="item" index="index">
                    <if test="item.app_name != null and item.app_name != ''">
                        when area_id=#{item.areaId} and app_id =#{item.appId} then #{item.app_name}
                    </if>
                    <if test="item.app_name == null and item.app_name == ''">
                        when area_id=#{item.areaId} and app_id =#{item.appId} then app_set_data.appName
                    </if>
                </foreach>
            </trim>
            <trim prefix="pass_reader1=case" suffix="end,">
                <foreach collection="appSetDatas" item="item" index="index">
                    <if test="item.passReader1 != null">
                        when area_id=#{item.areaId} and app_id =#{item.appId} then #{item.passReader1}
                    </if>
                    <if test="item.passReader1 == null">
                        when area_id=#{item.areaId} and app_id =#{item.appId} then app_set_data.pass_reader1
                    </if>
                </foreach>
            </trim>
            <trim prefix="update_time=case" suffix="end,">
                <foreach collection="appSetDatas" item="item" index="index">
                    <if test="item.updateTime != null">
                        when area_id=#{item.areaId} and app_id =#{item.appId} then #{item.updateTime}
                    </if>
                    <if test="item.updateTime == null">
                        when area_id=#{item.areaId} and app_id =#{item.appId} then app_set_data.update_time
                    </if>
                </foreach>
            </trim>
        </set>
        where (area_id,app_id) in
        <foreach collection="appSetDatas" item="item" index="index" separator="," open="(" close=")">
            (#{item.areaId},#{item.appId})
        </foreach>
    </update>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tan.]der

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值