mybatis批量更新(where的条件越少,最好是主键,效率越高)

复制代码
    <update id="updateBatch" databaseId="sqlserver">
        update T_RISK_TASK_SERVICE

        <trim prefix="set" suffixOverrides=",">
            <trim prefix=" TASK_REALITY_START_TIME = case " suffix=" end, ">
                <foreach collection="list" item="item">
                    <if test="item.taskRealityStartTime!= null">
                        when ID = #{item.id} then #{item.taskRealityStartTime}
                    </if>
                </foreach>
            </trim>
            <trim prefix=" TASK_REALITY_END_TIME = case " suffix=" end, ">
                <foreach collection="list" item="item">
                    <if test="item.taskRealityEndTime!= null">
                        when ID = #{item.id} then #{item.taskRealityEndTime}
                    </if>
                </foreach>
            </trim>
            <trim prefix=" TASK_NOTE = case " suffix=" end, ">
                <foreach collection="list" item="item">
                    <if test="item.taskNote!= null">
                        when ID = #{item.id} then #{item.taskNote}
                    </if>
                </foreach>
            </trim>
            <trim prefix=" TASK_FILE = case " suffix=" end, ">
                <foreach collection="list" item="item">
                    <if test="item.taskFile!= null">
                        when ID = #{item.id} then #{item.taskFile}
                    </if>
                </foreach>
            </trim>
            <trim prefix=" IS_SUBMIT = case " suffix=" end, ">
                <foreach collection="list" item="item">
                    <if test="item.isSubmit!= null">
                        when ID = #{item.id} then #{item.isSubmit}
                    </if>
                </foreach>
            </trim>
        </trim>

        where
        ID in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item.id}
        </foreach>
    </update>
复制代码

效果:

复制代码
 update T_RISK_TASK_SERVICE
set TASK_REALITY_START_TIME = case 
    when ID = 1 then 2024-06-12T00:00
    when ID = 2 then 2024-06-12T00:00
    when ID = 3 then 2024-06-15T00:00
end,TASK_REALITY_END_TIME = case 
    when ID = 1 then 2024-06-13T00:00
    when ID = 2 then 2024-06-13T00:00
    when ID = 3 then 2024-06-12T00:00
end,TASK_NOTE = case 
    when ID = 1 then '备注1'
    when ID = 2 then '备注1'
    when ID = 3 then '备注1'
end, TASK_FILE = case 
    when ID = 1 then '[{"id":"6668fd1531fb738df0b52465","name":"1718156566757jpg58523.jpg","new_id":"6668fd1531fb738df0b52465","url":"/ali/getcontent/6668fd1531fb738df0b52465.jpg","order_id":1},{"id":"666fad99c9e1c9927ca24688","name":"1718594969043jpg58523.jpg","new_id":"666fad99c9e1c9927ca24688","url":"/ali/getcontent/666fad99c9e1c9927ca24688.jpg","order_id":2},{"id":"6670fcef070c973610004005","name":"1718680815365png2103.png","new_id":"6670fcef070c973610004005","url":"/ali/getcontent/6670fcef070c973610004005.png","order_id":3}]'
    when ID = 2 then '[{"id":"666901a431fb738df0b52472","name":"1718157734253png2103.png","new_id":"666901a431fb738df0b52472","url":"/ali/getcontent/666901a431fb738df0b52472.png","order_id":1},{"id":"66695e9be9ae9c2f68db3cc3","name":"1718181532550jpg58523.jpg","new_id":"66695e9be9ae9c2f68db3cc3","url":"/ali/getcontent/66695e9be9ae9c2f68db3cc3.jpg","order_id":2}]'
    when ID = 3 then '[{"id":"666901b631fb738df0b52476","name":"1718157752156png1094.png","new_id":"666901b631fb738df0b52476","url":"/ali/getcontent/666901b631fb738df0b52476.png","order_id":1},{"id":"666fb0fdc9e1c9927ca247e9","name":"1718595837429jpg58523.jpg","new_id":"666fb0fdc9e1c9927ca247e9","url":"/ali/getcontent/666fb0fdc9e1c9927ca247e9.jpg","order_id":2}]'
end
where ID in (1, 2, 3)
复制代码

 

posted on   五官一体即忢  阅读(39)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示