• 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   (?,?,?,?,?)
  • 相关阅读:
    PetaPOCO 一对多 多对一 多对多
    PetaPoco使用要点
    MySQL_杭州北仓 12.3-12.7需求活动期间累计下单达到3天及以上的客户_20161212
    Python 2.7_Second_try_爬取阳光电影网_获取电影下载地址并写入文件 20161207
    Python 2.7_First_try_爬取阳光电影网_20161206
    MySQL计算销售员昨日各指标综合得分_20161206
    MySQL_关于用嵌套表计算的可以不用 20161205
    MySQL_财务统计各产品品类各城市上周收入毛利表_20161202
    借助取色工具ColorPix对Pycharm编辑器设定自己喜欢的代码颜色_20161202
    python2.7 爬虫_爬取小说盗墓笔记章节及URL并导入MySQL数据库_20161201
  • 原文地址:https://www.cnblogs.com/djq-jone/p/10754742.html
Copyright © 2020-2023  润新知