MyBatis 批量資料插入的兩種方式

2021-09-28 14:04:30 字數 1342 閱讀 3355

public

intaddpersons

(@param

("persons"

) list

persons)

;

根據mysql的insert語句規則結合foreach去寫sql語句,collection中的是person物件,person。xx便可以取得屬性值

"addpersons"

>

insert into person(username,email,gender) values

collection

="persons"

item

="person"

separator

=","

>

(#,#,#)

foreach

>

insert

>

"addpersons"

>

collection

="persons"

item

="person"

separator

=";"

>

insert into person(username,email,gender) values

(#,#,#)

foreach

>

insert

>

因為利用foreach去拼接批量插入語句的時候,批量插入的資料量越多,拼接的語句越長,資料庫引擎解析的時候會負擔較大,所以資料量較大的時候可以利用executor type

通過檢視api可知可以在setting中設定,但是setting是全域性配置,如果全部語句都採用批處理就不合理的,所以需要在單個sql語句中實現批處理,則接觸單個sqlsession

mybatis3api:settings配置

"addperson"

parametertype

="person"

>

insert into person(username,email,gender) values (#,#,#)

insert

>

2.2 test,在opensession中傳入引數executortype.batch
sqlsession sqlsession =

this

.getsqlsessionfactory()

.opensession

(executortype.batch)

;

MyBatis批量插入資料

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

mybatis批量插入資料

有三種方式 裡面for迴圈 略mybatis batch模式插入 略對映檔案foreach方式插入 在對映檔案中使用 標籤 傳入引數為該物件的list集合 insert into auth role resource id,roleid,resourceid,createtime,updatetim...

Mybatis批量更新插入資料

熊大最近發現乙個批量更新時不用迴圈欄位的更新,跟各位撰碼人分享分享。同為碼農深知碼農不易,勿入坑。好了咱們來說正事兒,來看看這條sql update mydata table set status when then where id in 這無非就是根據id批量修改了mydata table這張表...