Mybatis 批量添加,批量更新


此篇適合有一定的mybatis使用經驗的人閱讀.

一.批量更新

  為了提升操作數據的效率,第一想到的是做批量操作,直接上批量更新代碼:

 1       <update id="updateBatchMembers" parameterType="list">
 2                 update crm_member
 3                 <trim prefix="set" suffixOverrides=",">
 4                     <trim prefix="dept_id =case" suffix="end,">
 5                         <foreach collection="list" item="i" index="index">
 6                             <if test="i.deptId!=null">
 7                                 when id=#{i.id} then #{i.deptId}
 8                             </if>
 9                         </foreach>
10                     </trim>
11                     <trim prefix=" sys_user_id =case" suffix="end,">
12                         <foreach collection="list" item="i" index="index">
13                             <if test="i.sysUserId!=null">
14                                 when id=#{i.id} then #{i.sysUserId}
15                             </if>
16                         </foreach>
17                     </trim>
18 
19                     <trim prefix="public_area_id =case" suffix="end," >
20                         <foreach collection="list" item="i" index="index">
21                             <if test="i.publicAreaId!=null">
22                                 when id=#{i.id} then #{i.publicAreaId}
23                             </if>
24                         </foreach>
25                     </trim>
26                 </trim>
27                 where
28                 <foreach collection="list" separator="or" item="i" index="index" >
29                     id=#{i.id}
30                 </foreach>
31             </update>

  生成的sql語句

 1 update 
 2   crm_member 
 3 set 
 4 dept_id =case 
 5   when id=? then ? 
 6   when id=? then ? 
 7   when id=? then ? 
 8   when id=? then ? 
 9 end, 
10 sys_user_id =case 
11   when id=? then ? 
12   when id=? then ? 
13   when id=? then ? 
14   when id=? then ? 
15 end, 
16 public_area_id =case 
17   when id=? then ? 
18   when id=? then ? 
19   when id=? then ? 
20   when id=? then ?  
21 end 
22 where 
23   id=? 
24   or id=? 
25   or id=? 
26   or id=? 
27   or id=? 

二.批量添加

  批量添加在做大量數據插入到mysql時,效率相對單條遍歷插入大大提高;

  但是數據是基於數據庫層面做的約束的話,在插入的數據中有一個數據有誤,整個批量操作全部回滾;

  適用場景:數據遷移時使用.

  

 1    <insert id="batchInsertMember" parameterType="java.util.List">
 2            insert into crm_member(
 3                 id,
 4                 name,
 5                 type,
 6                 phone,
 7                 link,
 8 )
32             values
33         <foreach collection="list" item="item" index="index" separator=",">
34                 (
35                 #{item.id},
36                 #{item.name},
37                 #{item.type},
38                 #{item.phone},
39                 #{item.link}
40 )
64         </foreach>
65     </insert>

生成對應的sql

1 insert into 
2   crm_member3   (
    id,
    name,
    type,
    phone,
    link
    )
4 values 5   (?,?,?,?,?), 6   (?,?,?,?,?), 7   (?,?,?,?,?), 8   (?,?,?,?,?)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM