mybatis批量處理

2021-09-02 01:27:39 字數 1683 閱讀 2592

在專案當中總會一次查詢滿足不了頁面所需展示的字段。這時候需要根據查出來的list迴圈去查詢另外的字段,有人會在迴圈中執行資料庫操作,這樣會建立多次資料庫連線,不但耗費效能而且會導致連線數滿。尤其是查詢大資料量的時候,效能測試的時差體現的很明顯。我們應當避免這樣的操作,去用批量處理。

說明:item集合或陣列裡的元素(物件)

collection集合型別(陣列或集合)

open以什麼開始

close以什麼結束

separator中間以什麼相連

1.批查詢 select

select

date_format(acc.update_time,'%y-%m-%d %h:%i:%s') as updatetime,

date_format(acc.redemptiontime,'%y-%m-%d %h:%i:%s') as redemptiontime,

acc.auditsu***ceid as auditsu***ceid

from auditjournalminute acc

where

acc.auditsu***ceid in

#

(select

date_format(acc.update_time,'%y-%m-%d %h:%i:%s') as updatetime,

date_format(acc.redemptiontime,'%y-%m-%d %h:%i:%s') as redemptiontime,

acc.auditsu***ceid as auditsu***ceid

from auditjournalminute acc

where

acc.auditsu***ceid=#

and acc.`status`=#

order by acc.update_time asc

limit 1)

2.批量插入 insert

insert into audit_history

(backgrounduserid,

show_time,

commit_num,

lend_num)

values

(#,#,#,#)

3.批量刪除 delete

delete from 

artworkmasterphotoalbum

where

artworkmasterphotoalbumid in

#

4.批量更新

update role

set update_time=

when # then #

where roleid in

#

update role

update_time=#,

create_time=#

where roleid=#

注: insert 的時候 如果需要返回主鍵,在標籤中增加 usegeneratedkeys=「true」 keyproperty=「實體主鍵id欄位」

mybatis批量處理

逐條更新 這種方式顯然是最簡單,也最不容易出錯的,即便出錯也只是影響到當條出錯的資料,而且可以對每條資料都比較可控,更新失敗或成功,從什麼內容更新到什麼內容,都可以在邏輯 中獲取。可能像下面這個樣子 updatebatch listdatas catch exception e mybatis中up...

mybatis之批量處理

批量處理即對多條資料進行sql操作,如批量更新,插入,新增。之前採取過很low的方式,就是在dao層進行迴圈,對每條資料進行操作。這樣效果可以實現,但是頻繁連線資料庫,效能,效率上非常不好。mybatis支援引數為list的操作,這樣連線資料庫就一次,把迴圈的語句寫入到sql語句中,這樣效率會高很多...

mybatis批量修改,批量新增

mybatis批量修改 批量新增sql語句 1 單個新增 insert into t user user name,mobile values 2 新增並返回主鍵 keyproperty的屬性是要返回的主鍵欄位的名稱 insert into t user user name,mobile value...