MyBatis批量处理Oracle数据库数据

[html]  view plain  copy
  1. <insert id="insertAll" parameterType="java.util.List">  
  2.     INSERT INTO TABLE_NAME   
  3.     (ID,NAME,VALUE)  
  4.     SELECT SEQUENCE.NEXTVAL ID,A.* FROM (  
  5.         <foreach collection="list" item="item" index="index" separator="UNION ALL">  
  6.             SELECT   
  7.             #{item.name,jdbcType=VARCHAR},  
  8.             #{item.value,jdbcType=DOUBLE}  
  9.             FROM DUAL  
  10.         </foreach>  
  11.     ) A  
  12. </insert>  


注:insert语句中不写values关键字,不然会报错。

如果不用序列自增主键,如下:

[html]  view plain  copy
  1. <insert id="insertAll" parameterType="java.util.List">  
  2.     INSERT INTO TABLE_NAME   
  3.     (ID,NAME,VALUE)  
  4.     <foreach collection="list" index="index" item="item" open="(" close=")" separator="union all">  
  5.         SELECT  
  6.         #{item.id,jdbcType=VARCHAR},  
  7.         #{item.name,jdbcType=VARCHAR},  
  8.         #{item.value,jdbcType=DOUBLE}  
  9.         FROM DUAL  
  10.     </foreach>  
  11. </insert>  


批量更新操作:

[html]  view plain  copy
  1. <update id="updateAll" parameterType="com.ab.bean.MainDataBean">  
  2.     <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">  
  3.         update table_name set  
  4.         value = #{item.value,jdbcType=DOUBLE}  
  5.         where id = #{item.id,jdbcType=VARCHAR}  
  6.     </foreach>  
  7. </update>  

批量删除操作:

[html]  view plain  copy
  1. <delete id="deleteAll" parameterType="com.ab.bean.MainDataBean">  
  2.     <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">  
  3.         delete from table_name  
  4.         where id = #{item.id} and name = #{item.name}  
  5.     </foreach>  
  6. </delete>  


注:

jdbcType正常情况下可以不写,但是如果数据中有null,就必须声明jdbcType,否则会报错。


重点:批量insert的效率问题!

后来在项目中,因为数据稍大一些,比如有一万多条的时候,批量insert的效率会变得异常差,要二十多分钟甚至更久才能完成。可以说还不如在java中用for循环每次单条数据插入。后来查了查,可以通过在java中对要插入的list通过subList方法进行分割,多次处理。代码如下:(这段代码在网上也能找到,但是我发现他们的逻辑有问题,会漏掉部分数据,我做了修改)

Service实现类的主要代码:

[java]  view plain  copy
  1. ArrayList<MainDataBean> list = new ArrayList<>();//这里忽略list封装值的过程  
  2. int count = 0;      //用于累计结果  
  3. if (list != null && list.size() > 0) {  
  4.     int batchCount = 500;               //每次批量插入的数据量大小  
  5.     int batchLastIndex = batchCount;    //声明每次截取list的终止索引大小  
  6.     for (int index = 0; index < endowList.size(); ) {    //声明index为截取的起始索引  
  7.         if (batchLastIndex >= endowList.size()) {  
  8.             //当结尾索引大于list的size时,将终止索引赋值为list的size,subList方法含头不含尾  
  9.             batchLastIndex = endowList.size();  
  10.             count += testDao.insertAll(list.subList(index, batchLastIndex),table_name);  
  11.             break;      //最后一次执行后,终止循环  
  12.         } else {  
  13.             count += testDao.insertAll(list.subList(index, batchLastIndex),table_name);  
  14.             //每次执行完insert之后,将起始索引赋值为终止索引  
  15.             index = batchLastIndex;   
  16.             //将终止索引赋值为原先的终止索引+每次批量处理的数据大小  
  17.             batchLastIndex = index + batchCount;  
  18.         }  
  19.     }  
  20. }  
  21. return count;  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值