zoukankan      html  css  js  c++  java
  • mybatis批量update

    更新单条记录

    UPDATE course SET name = 'course1' WHERE id = 'id1';
    

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

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

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

    比较普通的写法,是通过循环,依次执行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一次,性能比较差,容易造成阻塞。

    PostgreSQL没有提供直接的方法来实现批量更新,但可以使用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="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>
    
    <update id="updateUserLog" parameterType="java.util.List">
        update ps_user_log
        SET dept_id =
        <foreach collection="list" item="item" index="index"
                 separator=" " open="case id" close="end">
            when #{item.id} then #{item.deptId}
        </foreach>
        , type =
        <foreach collection="list" item="item" index="index"
                 separator=" " open="case id" close="end">
            when #{item.id} then #{item.type}
        </foreach>
        where id in (
        <foreach collection="list" item="item" index="index"
                 separator=",">
            #{item.id}
        </foreach>
        )
    </update>
    
  • 相关阅读:
    Java并发编程(二)线程任务的中断(interrupt)
    Java并发编程(一) 两种实现多线程的方法(Thread,Runnable)
    青蛙跳台阶(Fibonacci数列)
    旋转数组的最小值
    用两个栈实现队列
    重建二叉树
    二维数组中的查找
    Lab 3-1
    Lab 1-4
    Lab 1-3
  • 原文地址:https://www.cnblogs.com/cchilei/p/12851225.html
Copyright © 2011-2022 走看看