星空网 > 软件开发 > ASP.net

mybatis3批量更新 批量插入

在公司ERP项目开发中,遇到批量数据插入或者更新,因为每次连接数据库比较耗时,所以决定改为批量操作,提升效率。库存盘点导入时,需要大量数据批量操作。

1:数据库连接代码中必须开启批量操作。加上这句,&allowMultiQueries=true,完整的如下:

jdbc:mysql://localhost:3306/jeesite2016?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true

 

2:批量更新 ,注意update的separator是;,和批量插入的不一样。

 

Sql代码下载    mybatis3批量更新 批量插入

  1. <update id="batchUpdateQuantity" parameterType="java.util.List">  
  2.                   <foreach collection="list" item="item" index="index"  open="" close="" separator=";">  
  3.                   update erp_store   
  4.                   <set>   
  5.                   quantity=#{item.quantity},  
  6.                   update_date=#{item.updateDate}  
  7.                   </set>   
  8.                   where id = #{item.id}  
  9.                  </foreach>  
  10.     </update>  
  11.       

 

经测试,一共1662条数据,批量插入用时466ms,循环单独插入用时1898ms。可以批量操作效率高很多。

 

3、批量插入  

 

Java代码下载   mybatis3批量更新 批量插入

  1. <insert id="batchInsert">  
  2.         INSERT INTO erp_store_detail(  
  3.             id,  
  4.             store_id,  
  5.             type,  
  6.             change_quantity,  
  7.             after_quantity,  
  8.             update_date,  
  9.             remarks,  
  10.             link_id  
  11.         ) VALUES   
  12.      <foreach collection="list" item="item" index="index" separator="," >    
  13.         (  
  14.             #{item.id},  
  15.             #{item.store.id},  
  16.             #{item.type},  
  17.             #{item.changeQuantity},  
  18.             #{item.afterQuantity},  
  19.             #{item.updateDate},  
  20.             #{item.remarks},  
  21.             #{item.linkId}  
  22.         )  
  23.      </foreach>    
  24.       
  25.     </insert>  

 




原标题:mybatis3批量更新 批量插入

关键词:mybatis

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

找客服还要收钱?某亚马逊卖家惨损22万美刀!:https://www.ikjzd.com/articles/108155
离岸账户关停潮升级,11月底前或将大规模清退!:https://www.ikjzd.com/articles/108156
卖家如何通过差异化运营,攻克亚马逊运营难点?:https://www.ikjzd.com/articles/108157
Amazon Giveaway将于10月10日暂停使用!:https://www.ikjzd.com/articles/108159
成功创建速卖通活动的技巧和秘诀分享大全!:https://www.ikjzd.com/articles/10816
跨境电商发展新趋势:订单碎片化,贸易在线化!:https://www.ikjzd.com/articles/108161
TikTok 将推出先买后付服务 :https://www.kjdsnews.com/a/1836651.html
TikTok 将推出先买后付服务 :https://www.goluckyvip.com/news/188219.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流