MyBatis 批量插入大批量資料 資料過多問題

2021-10-10 15:39:28 字數 1514 閱讀 6325

mybatis 批量插入大批量資料

場景:匯入excel**,讀取資料,再批量插入資料庫

service關鍵**如下

傳入list集合,直接插入資料

batchinserthzjjbindcodelog

(list)

;xml

"batchinserthzjjbindcodelog"

>

insert into hzjj_batch_bind_code_log (id_card,yt_no,redeem_code,card_no,password,name,batch_id,ip,course_id) values

collection

="list"

item

="log"

separator

=","

>

(#,#,#,#,#,#,#,#,#)

foreach

>

insert

>

剛開始呼叫介面沒有什麼問題一切正常,後來一次匯入300條資料直接報錯:

傳入的**格式資料流(tds)遠端過程呼叫(rpc)協議流不正確。此 rpc 請求中提供了過多的引數。最多應為 2100

查閱資料才發現原來mybatis呼叫sql server的時候,不管插入還有查詢修改等操作,引數是有限制的**,當引數長度超過2100就會報此錯誤。**

解決辦法:

多個執行緒批量插入資料,本來1000條資料,啟動10個執行緒,每個執行緒插入100條就ok。

解決以後**如下:

//因為我這邊資料不會很多,所以執行緒池就直接使用cachedthreadpool,沒有自定義乙個執行緒池。

executorservice executorservice = executors.

newcachedthreadpool()

;//num 資料被分為多少批可以傳送完

//100 每個執行緒批量插入100個

int num=list.

size()

/100;if

(list.

size()

%100

>=0)

// lastindex 擷取結束座標

int lastindex=0;

for(

int i=

0;i)else

//多執行緒批量新增

executorservice.

submit

(new

batchinserthzjjcodethread

(list.

sublist

(i*100

,lastindex));

}//關閉執行緒池,拒絕接收任務

executorservice.

shutdown()

;while

(true

)}

然後再次匯入**就可以正常新增

MySQL大批量插入資料

1.對於myisam型別的表,可以通過以下方式快速的匯入大量的資料。alter table tblname disable keys loading the data alter table tblname enable keys 這兩個命令用來開啟或者關閉myisam表非唯一索引的更新。在匯入大量...

mysql大批量資料插入優化

目前在專案中發現一張700萬的表,插入10萬條資料通過mybatis的批量插入大概需要3分鐘左右,耗時太長。現在通過mysql的load data local infile 命令進行優化插入。最後測試10萬條資料批量插入大概在3秒左右的時間。定義命令執行語句 public inte ce fastb...

oracle中插入大批量資料

方法一 建立乙個表,並同時新增1000000條資料,create table testtable as select rownum as id,to char sysdate rownum 24 3600,yyyy mm dd hh24 mi ss as inc datetime,trunc dbm...