mybatis 批量更新update详解

转载:https://blog.csdn.net/lu1024188315/article/details/78758943点击打开链接

转载:https://blog.csdn.net/xyjawq1/article/details/74129316点击打开链接

1  更新单条记录 

UPDATE course SET name = 'course1' WHEREid = 'id1';

2  更新多条记录的同一个字段为同一个值

 UPDATE course SET name='course1WHERE id in('id1','id2','id3);

3  更新多条记录为多个字段为不同的值

比较普通的写法,是通过循环,依次执行update语句。

Mybatis写法如下: 

<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一次,性能比较差,容易造成阻塞

MySQL没有提供直接的方法来实现批量更新,但可以使用case when语法来实现这个功能。

 UPDATE course
    SET name = CASE id 
        WHEN 1 THEN 'name1'
        WHEN 2 THEN 'name2'
        WHEN 3 THEN 'name3'
    END, 
    title = CASE id 
        WHEN 1 THEN 'New Title 1'
        WHEN 2 THEN 'New Title 2'
        WHEN 3 THEN 'New Title 3'
    END
WHERE id IN (1,2,3)

这条sql的意思是,如果id为1,则name的值为name1,title的值为New Title1;依此类推。

在Mybatis中的配置则如下:

 <updateid="updateBatch"parameterType="list">

            update course
            <trim prefix="set" suffixOverrides=",">
             <trim prefix="name=case" suffix="end,">
                 <foreach collection="list" item="item" index="index">
                         <if test="item.name!=null">
                          when id=#{item.id} then #{item.name}
                         </if>
                 </foreach>
              </trim>
              <trim prefix="title =case" suffix="end,">
                 <foreach collection="list" item="item" index="index">
                         <if test="item.title!=null">
                          when id=#{item.id} then #{item.title}
                         </if>
                 </foreach>
              </trim>
             </trim>
            where
            <foreach collection="list" separator="or" item="item" index="index">
              id=#{item.id}
          </foreach>
</update>
<trim> 属性说明 
1. prefix,suffix  表示在 trim 标签包裹的部分的前面或者后面添加内容 
2.如果同时有 prefixOverrides,suffixOverrides  表示会用 prefix,suffix 覆盖 Overrides 中的内容。 
3.如果只有 prefixOverrides,suffixOverrides  表示删除开头的或结尾的 xxxOverides 指定的内容。
4  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 的 <foreach> 语法来拼凑成了批量更新的 sql ,上面的意思就是批量更新 id 在 updateBatch 参数所传递 List 中的数据的status 字段。还可以使用 <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">
                     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>
其结构如下:
    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">
         <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 更新.其他的将使用默认值更新,而 不会保持原数据不变 .如果要保持原数据不变呢?即满足条件的更新,不满足条件的保持原数据不变,简单的来做就是再加一个 <if> ,因为 mybatis 中没有 if...else... 语法,但可以通过多个 <if> 实现同样的效果,如下:
<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
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis批量更新update指的是在一次数据库操作中更新多条数据记录,这样可以减少与数据库的交互次数,提升数据更新的效率。在Mybatis中,批量更新update操作主要分为两种形式:静态SQL的批量更新和动态SQL的批量更新。 静态SQL的批量更新是指通过Mybatis的<foreach>标签实现的,它的基本步骤是:定义一个List集合,将需要更新的数据对象添加到集合中,使用foreach标签将集合元素遍历后拼接成一条完整的SQL语句,执行SQL语句进行批量更新操作。 动态SQL的批量更新是指根据不同的条件动态构建SQL语句,实现数据更新的操作。动态SQL的批量更新主要有两种方式:一种是使用Mybatis的<foreach>标签构建动态SQL语句,类似于静态SQL的批量更新;另一种是使用Mybatis的SQL Provider功能,即定义一个类,通过在类中定义方法,并使用@Provider注解,将方法关联到mapper.xml文件中,在程序中直接调用方法即可实现动态构建SQL语句的功能。 在实际项目中,选择何种方式进行批量更新update操作,应根据具体业务需求和数据更新的复杂度来进行选择。如果数据更新比较简单,不需要使用动态SQL,那么可以使用静态SQL的批量更新方式;如果数据更新比较复杂,需要使用动态SQL来进行处理,那么可以选择使用Mybatis的SQL Provider功能。通过掌握这些批量更新的技巧,可以大大提高数据更新的效率,提高程序的响应速度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值