mybatis批量添加,批量更新前怎么判断是否已经存在

发布时间:2022-08-27 09:18:24 作者:iii
来源:亿速云 阅读:525

这篇文章主要介绍“mybatis批量添加,批量更新前怎么判断是否已经存在”,在日常操作中,相信很多人在mybatis批量添加,批量更新前怎么判断是否已经存在问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”mybatis批量添加,批量更新前怎么判断是否已经存在”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

批量添加,批量更新之前判断是否已经存在

批量添加之前判断是否已经存在,foreach  separator用UNION ALL。

mybatis批量添加,批量更新前怎么判断是否已经存在

批量修改

mybatis批量添加,批量更新前怎么判断是否已经存在

批量更新update详解文档

1 更新单条记录

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

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

UPDATE course SET name=‘course1' WHERE 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中的配置则如下:

 <update id="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>

属性说明

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&hellip;then&hellip;是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>

其结构如下:

    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更新.其他的将使用默认值更新,而不会保持原数据不变.如果要保持原数据不变呢?

即满足条件的更新,不满足条件的保持原数据不变,简单的来做就是再加一个,因为mybatis中没有if&hellip;else&hellip;语法,但可以通过多个实现同样的效果,如下:

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

1.组装多个update语句,这种方式需要设置jdbc连接 allowMultiQueries=true

   <update id="updateEquementWaterTest"   parameterType="java.util.List">
         <foreach collection="list" item="item" index="index">
                              update rent_hl_room l
                SET l.water_meter_id=#{item.equipmentCode},
                l.water_meter_source_type=#{item.equipmentSource}
                WHERE 
                    l.room_id=#{item.roomId};
             </foreach>
     </update>

2.case when

UPDATE 
  rent_hl_room l 
SET
  electricity_meter_id = 
  CASE
    WHEN room_id = 1942
    THEN 180524348 
    WHEN room_id = 1945
    THEN 180524480 
  END,
  electricity_meter_source_type = 
  CASE
    WHEN room_id = 1942 
    THEN ym 
    WHEN room_id = 1945 
    THEN ym 
  END 
WHERE room_id = 1942 
  OR room_id = 1945
          <update id="updateEquementWater"   parameterType="java.util.List">
              update rent_hl_room l
            <trim prefix="set" suffixOverrides=",">
             <trim prefix="water_meter_id =case" suffix="end,">
                 <foreach collection="list" item="i" index="index">
                         <if test="i.equipmentCode!=null">
                          when room_id=#{i.roomId} then #{i.equipmentCode}
                         </if>
                 </foreach>
              </trim>
              <trim prefix=" water_meter_source_type =case" suffix="end,">
                 <foreach collection="list" item="i" index="index">
                         <if test="i.equipmentSource!=null">
                          when room_id=#{i.roomId} then #{i.equipmentSource}
                         </if>
                 </foreach>
              </trim>
             </trim>
            where
            <foreach collection="list" separator="or" item="i" index="index" >
              room_id=#{i.roomId}
          </foreach>
        
       </update>

经测试 100条数据的时候第一种方式的效率比第二种差不多高1倍。

到此,关于“mybatis批量添加,批量更新前怎么判断是否已经存在”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 判断一个元素是否已经存在
  2. redis是否存在判断值

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis

上一篇:JavaScript函数式编程Thunk原理是什么

下一篇:Mybatis中怎么使用in()查询

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》