mybatis批量insert到Oracle資料庫

2021-07-30 20:16:50 字數 1613 閱讀 1177

(1)使用mybatis的foreach語法:

private static void listinsert() 

} system.out.println("60萬條耗時 :"+(system.currenttimemillis()-start));

}

對應的 sql語句:

insert into aop_trade (

log_id,

bizkey ,

methodcode,

province,

city,

channel_id,

reqtimes,

update_time

) (select

#,#,

#,#,

#,#,

#,sysdate

from dual)

這時:批量在list中存入10條,100條,1000條耗時分別是 54917毫秒,27949毫秒,25446毫秒。

(2)使用事務批量提交:

private static void onebyone() 

} system.out.println("60萬條耗時 :"+(system.currenttimemillis()-start));

}

這時採用5000條,10000條,20000條,30000條插入提交一次,分別耗時5798毫秒,4526毫秒,4976毫秒,4707毫秒。

(3)採用上面兩者結合:

private static void combine() 

list=new arraylist();

}} system.out.println("60萬條耗時 :"+(system.currenttimemillis()-start));

}

實驗結果為:

list存10條,100次insert提交一次:9107毫秒

list存10條,1000次insert提交一次:9034毫秒

list存100條,50次insert提交一次:8776毫秒

綜上所述: 採用第二種大的方案較好。

但是涉及到批量資料提交,都要保證這些資料中沒有錯誤資料,都可以插入到資料庫中,否則根據事務原理,全都成功,或全部都不成功。這樣對於安全性不高的資料差如有很高的風險,這裡採用增加錯誤日誌表的方法:

1,建立對應的錯誤日誌表(sql語句):

begin                         -- 為 aop_business 建立乙個 名字為

dbms_errlog.create_error_log('aop_trade', 'aop_trade_error_log');

end;

2,在執行sql時加上:

insert into aop_trade values( a1,a2,a3,a4)   log errors into aop_trade_error_log

reject limit unlimited

後面的多出來的便可以解決事務中有錯誤資料的情況。

MyBatis批量插入 insert 資料操作

在程式中封裝了乙個list集合物件,然後需要把該集合中的實體插入到資料庫中,由於專案使用了spring mybatis的配置,所以打算使用mybatis批量插入,由於之前沒用過批量插入,在網上找了一些資料後最終實現了,把詳細過程貼出來。實體類trainrecord結構如下 1 2 3 4 5 6 7...

Mybatis基本對映 INSERT

本章學習mysql基本對映 insert相關知識點。insert的用法比select要簡單很多。本系列文章是基於mybatis 3.4.6 版本,資料庫使用的是mysql 5.7。insert標籤常用屬性 parametertype 該屬性的含義就是其字面意思,即傳入語句的引數型別,是類的全限定類名...

ORACLE 批量插入 Insert 詳解

假設有一張表student 學生表 create table student id varchar2 11 primary key,name varchar2 32 not null varchar2 3 not null age smallint,tel varchar 16 其中中代表可選 代表...