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 2024-06-21 10:36  五官一体即忢  阅读(15)  评论(0编辑  收藏  举报

导航