Mybatis批量更新 updateBatch

<update id="updateBatch" parameterType="java.util.List">
        update mydata_table
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="status =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                     when id=#{item.id} then #{item.status}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item.id,jdbcType=BIGINT}
        </foreach>
    </update>

<trim>属性说明 
1.prefix,suffix 表示在trim标签包裹的部分的前面或者后面添加内容 
2.如果同时有prefixOverrides,suffixOverrides 表示会用prefix,suffix覆盖Overrides中的内容。 
3.如果只有prefixOverrides,suffixOverrides 表示删除开头的或结尾的xxxOverides指定的内容。

上述代码转化成sql如下:

update mydata_table 
    set status = 
    case
        when id = #{item.id} then #{item.status}//此处应该是<foreach>展开值
        ...
    end
    where id in (...);

有时候可能需要更新多个字段,那就需要将

<trim prefix="status =case" suffix="end,">
     <foreach collection="list" item="item" index="index">
          when id=#{item.id} then #{item.status}
     </foreach>
</trim>

复制拷贝多次,更改prefixwhen...then...的内容即可

原文链接:https://blog.csdn.net/xyjawq1/article/details/74129316

猜你喜欢

转载自www.cnblogs.com/fswhq/p/updateBatch.html
今日推荐