Mybatis中updateBatch实现批量更新

 更新时间:2022年03月11日 09:30:46   作者:未月廿三  
本文主要介绍了Mybatis中updateBatch实现批量更新,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一.更新多条数据,每条数据都不一样

背景描述:通常如果需要一次更新多条数据有两个方式,(1)在业务代码中循环遍历逐条更新。(2)一次性更新所有数据(更准确的说是一条sql语句来更新所有数据,逐条更新的操作放到数据库端,在业务代码端展现的就是一次性更新所有数据)。两种方式各有利弊,下面将会对两种方式的利弊做简要分析,主要介绍第二种方式在mybatis中的实现。

1.逐条更新(java实现)

这种方式显然是最简单,也最不容易出错的,即便出错也只是影响到当条出错的数据,而且可以对每条数据都比较可控,更新失败或成功,从什么内容更新到什么内容,都可以在逻辑代码中获取。代码可能像下面这个样子:

updateBatch(List<MyData> datas){
    for(MyData data : datas){
        try{
            myDataDao.update(data);//更新一条数据,mybatis中如下面的xml文件的update
        }
        catch(Exception e){
            ...//如果更新失败可以做一些其他的操作,比如说打印出错日志等
        }
    }
}

//mybatis中update操作的实现
<update>
    update mydata
    set   ...
    where ...
</update>

这种方式最大的问题就是效率问题,逐条更新,每次都会连接数据库,然后更新,再释放连接资源(虽然通过连接池可以将频繁连接数据的效率大大提高,抗不住数据量大),这中损耗在数据量较大的时候便会体现出效率问题。这也是在满足业务需求的时候,通常会使用上述提到的第二种批量更新的实现(当然这种方式也有数据规模的限制,后面会提到)。

2.逐条更新(mybatis实现)

通过循环,依次执行多条update的sql

前提条件:
要实现批量更新,首先得设置mysql支持批量操作,在jdbc链接中需要附加&allowMultiQueries=true属性才行 

例如: 

jdbc:mysql://localhost:3306/dbname?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
<update id="updateBatch"  parameterType="java.util.List">  
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
        update course
        <set>
            name=${item.name}
        </set>
        where id = ${item.id}
    </foreach>      
</update>

一条记录update一次,性能比较差,容易造成阻塞。

3.sql批量更新(主力实现)

(1)、实际实践(传入的是List<Map<String, Object>>)
务必注意:一定要加where条件,里面的id为需要更新的数据的id;如果不加where条件,则会全部更新,但是需要更新且有数据的更新为传递的数据,没有数据的则更新为null,此时更新出错

<update id="updateChartParamByAccountAndChartid" parameterType="list">
        update followme_parameters
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="signal_source =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.signalSource!=null">
                        when account=#{item.account} and chart_id=#{item.chartId}
                         then #{item.signalSource}
                    </if>
                </foreach>
            </trim>
            <trim prefix="rate =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.rate!=null">
                        when account=#{item.account} and chart_id=#{item.chartId}
                        then #{item.rate}
                    </if>
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
            #{item.id}
        </foreach>
    </update>

另外文章的样板

<update id="updateBatch" parameterType="list">
     update course
      <trim prefix="set" suffixOverrides=",">
       <trim prefix="peopleId =case" suffix="end,">
           <foreach collection="list" item="i" index="index">
                   <if test="i.peopleId!=null">
                    when id=#{i.id} then #{i.peopleId}
                   </if>
           </foreach>
        </trim>
        <trim prefix=" roadgridid =case" suffix="end,">
           <foreach collection="list" item="i" index="index">
                   <if test="i.roadgridid!=null">
                    when id=#{i.id} then #{i.roadgridid}
                   </if>
           </foreach>
        </trim>

        <trim prefix="type =case" suffix="end," >
           <foreach collection="list" item="i" index="index">
                   <if test="i.type!=null">
                    when id=#{i.id} then #{i.type}
                   </if>
           </foreach>
        </trim>
        <trim prefix="unitsid =case" suffix="end," >
            <foreach collection="list" item="i" index="index">
                    <if test="i.unitsid!=null">
                     when id=#{i.id} then #{i.unitsid}
                    </if>
            </foreach>
     </trim>
    </trim>
    where
    <foreach collection="list" separator="or" item="i" index="index" >
        id=#{i.id}
    </foreach>
</update>

(2)、下面逐步讲解

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

<update id="updateBatch" parameterType="java.util.List">
    update mydata_table 
    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>

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

复制拷贝多次,更改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>

4.批量更新(单个字段,传参list),实际是sql批量更新的简化版本而已

(1)、单个字段方法1

<update id="updateByBatch" parameterType="java.util.List">
    update t_goods
    set NODE_ID=
    <foreach collection="list" item="item" index="index"
             separator=" " open="case" close="end">
      when GOODS_ID=#{item.goodsId} then #{item.nodeId}
    </foreach>
    where GOODS_ID in
    <foreach collection="list" index="index" item="item"
             separator="," open="(" close=")">
      #{item.goodsId,jdbcType=BIGINT}
    </foreach>
  </update>

(2)、单个字段方法2

<update id="updateByBatch" parameterType="java.util.List">
    UPDATE
    t_goods
    SET NODE_ID = CASE
    <foreach collection="list" item="item" index="index">
      WHEN GOODS_ID = #{item.goodsId} THEN #{item.nodeId}
    </foreach>
    END
    WHERE GOODS_ID IN
    <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
      #{item.goodsId}
    </foreach>
  </update>

以上单字段更新实际执行:

UPDATE t_goods SET NODE_ID = CASE WHEN GOODS_ID = ? THEN ? END WHERE GOODS_ID IN ( ? )

5.sql批量更新(通过insert实现)

传入的是List<Map<String,Object>>

直接运行插入,如果有插入的数据转为更新该条数据

<insert id="updateChartParamByAccountAndChartid">
    insert into followme_parameters
    (account,chart_id,signal_source,rate)
    values
    <foreach collection="list" separator="," index="index" item="item">
        (#{item.account},#{item.chartId},#{item.signalSource},#{item.rate})
    </foreach>
    ON duplicate KEY UPDATE
    signal_source=values(signal_source),rate=values(rate) 
</insert>

二.更新多条数据,更新的内容一样.

1.传map/传String

NODE_ID从map中取出来,goodsIdList是字符串拼接好的(如下面的"1,2,5")

<update id="updateByBatchPrimaryKey" parameterType="java.util.Map">
    UPDATE t_goods
    SET NODE_ID = #{nodeId}
    WHERE GOODS_ID IN (${goodsIdList})
  </update>

实际的sql

UPDATE t_goods SET NODE_ID = ? WHERE GOODS_ID IN (1,2,5);

2.传map/传list

NODE_ID从map中取出来,goodsIdList是用list拼接出来的

<update id="updateByBatchPrimaryKey" parameterType="java.util.Map">
    UPDATE t_goods
    SET NODE_ID = #{nodeId}
    WHERE GOODS_ID IN 
    <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
      #{item.goodsId}
    </foreach>
</update>

实际的sql

UPDATE t_goods SET NODE_ID = ? WHERE GOODS_ID IN (1,2,5);

参考文章:
主力:https://blog.csdn.net/xyjawq1/article/details/74129316/
辅助:https://www.jianshu.com/p/041bec8ae6d3

到此这篇关于Mybatis中updateBatch实现批量更新 的文章就介绍到这了,更多相关Mybatis 批量更新内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解Java抛出和声明异常的代码实现

    详解Java抛出和声明异常的代码实现

    我们在编写代码时,有时候因为某些原因,并不想在这个方法中立即处理产生的异常,也就是说并不想进行异常的捕获,接下来小编就来教会大家该如何进行异常的抛出,需要的朋友可以参考下
    2023-08-08
  • Java中的HashMap和Hashtable区别解析

    Java中的HashMap和Hashtable区别解析

    这篇文章主要介绍了Java中的HashMap和Hashtable区别解析,HashMap和Hashtable都实现了Map接口,但决定用哪一个之前先要弄清楚它们之间的区别,主要的区别有线程安全性、同步和速度,需要的朋友可以参考下
    2023-11-11
  • mybatis plus自动生成器解析(及遇到的坑)

    mybatis plus自动生成器解析(及遇到的坑)

    这篇文章主要介绍了mybatis-plus自动生成器及遇到的坑,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Netty中最简单的粘包解析方法分享

    Netty中最简单的粘包解析方法分享

    黏包 是指网络上有多条数据发送给服务端, 但是由于某种原因这些数据在被接受的时候进行了重新组合,本文分享了一种最简单的黏包解析方法, 非常适用于初初初级选手
    2023-05-05
  • java中treemap和treeset实现红黑树

    java中treemap和treeset实现红黑树

    这篇文章主要为大家详细介绍了java中treemap和treeset实现红黑树,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • java调用mysql存储过程实例分析

    java调用mysql存储过程实例分析

    这篇文章主要介绍了java调用mysql存储过程的方法,以实例形式较为详细的分析了mysql数据库的建立和存储过程的实现方法,需要的朋友可以参考下
    2015-06-06
  • java实现统计字符串中字符及子字符串个数的方法示例

    java实现统计字符串中字符及子字符串个数的方法示例

    这篇文章主要介绍了java实现统计字符串中字符及子字符串个数的方法,涉及java针对字符串的遍历、判断及运算相关操作技巧,需要的朋友可以参考下
    2017-01-01
  • 有关Java中的BeanInfo介绍

    有关Java中的BeanInfo介绍

    Java的BeanInfo在工作中并不怎么用到,我也是在学习spring源码的时候,发现SpringBoot启动时候会设置一个属叫"spring.beaninfo.ignore",网上一些地方说这个配置的意思是是否跳过java BeanInfo的搜索,但是BeanInfo又是什么呢?本文我们将对此做一个详细介绍
    2021-09-09
  • 深入浅出重构Mybatis与Spring集成的SqlSessionFactoryBean(上)

    深入浅出重构Mybatis与Spring集成的SqlSessionFactoryBean(上)

    通常来讲,重构是指不改变功能的情况下优化代码,但本文所说的重构也包括了添加功能。这篇文章主要介绍了重构Mybatis与Spring集成的SqlSessionFactoryBean(上)的相关资料,需要的朋友可以参考下
    2016-11-11
  • Java super关键字的使用方法详解

    Java super关键字的使用方法详解

    这篇文章主要介绍了Java super关键字的使用方法详解的相关资料,希望通过本文能帮助到大家,让大家对super关键字彻底掌握,需要的朋友可以参考下
    2017-10-10

最新评论