Mybatis中进行批量更新

5 篇文章 0 订阅

Mybatis中进行批量更新

Mybatis中进行批量更新

  • Sql 批量更新

    一条sql语句来批量更新所有数据,下面直接看一下在mybatis中通常是怎么写的(去掉mybatis语法就是原生的sql语句了)。

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

    其中when…then…是sql中的"switch" 语法。这里借助mybatis的语法来拼凑成了批量更新的sql,上面的意思就是批量更新id在updateBatch参数所传递List中的数据的status字段。还可以使用实现同样的功能,代码如下:

    <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>
    

    属性说明
    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>
    

    复制拷贝多次,更改prefix和when…then…的内容即可.而如果当需要为某个字段设置默认值的时候可以使用else

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

    还有更常见的情况就是需要对要更新的数据进行判断,只有符合条件的数据才能进行更新,这种情况可以这么做:

    <trim prefix="status =case" suffix="end,">
     <foreach collection="list" item="item" index="index">
         <if test="item.status !=null and item.status != -1">
             when id=#{item.id} then #{item.status}
         </if>
     </foreach>
    </trim>
    

    这样的话只有要更新的list中status != null && status != -1的数据才能进行status更新.其他的将使用默认值更新,而不会保持原数据不变.如果要保持原数据不变呢?即满足条件的更新,不满足条件的保持原数据不变,简单的来做就是再加一个,因为mybatis中没有if…else…语法,但可以通过多个实现同样的效果,如下:

    <trim prefix="status =case" suffix="end,">
    <foreach collection="list" item="item" index="index">
        <if test="item.status !=null and item.status != -1">
            when id=#{item.id} then #{item.status}
        </if>
        <if test="item.status == null or item.status == -1">
            when id=#{item.id} then mydata_table.status      //这里就是原数据
        </if>
    </foreach>
    </trim>
    

    整体批量更新的写法如下:

    <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">
                     <if test="item.status !=null and item.status != -1">
                         when id=#{item.id} then #{item.status}
                     </if>
                     <if test="item.status == null or item.status == -1">
                         when id=#{item.id} then mydata_table.status//原数据
                     </if>
                 </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item.id,jdbcType=BIGINT}
        </foreach>
    </update>
    

    这种批量跟心数据库的方式可以在一次数据库连接中更新所有数据,避免了频繁数据库建立和断开连接的开销,可以很大程度的提高数据更新效率。但是这样的问题是如果这个过程中更新出错,将很难知道具体是哪个数据出错,如果使用数据自身的事务保证,那么一旦出错,所有的更新将自动回滚。而且通常这种方式也更容易出错。因此通常的使用的方案是进行折中,也就是一次批量更新一部分(分页进行更新,比如说一共有1000条数据,一次更新100条)。这样可以分担出错的概率,也更容易定位到出错的位置。
     当然如果数据量确实很大的时候,这种批量更新也一样会导致更新效率低下(比如说一次更新100条,那如果10亿条数据呢,一样要批量更新1000万次,建立和断开1000万次数据库,这个效率是无法承受的)。这时候也许只能考虑其他方案了,比如引入缓存机制等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值