mybatis的批量插入在開發中應該比較常見,在這裡直接引入乙個簡單的例子便可一目了然,mybatis批量插入的效率不言而喻,比使用for迴圈不是簡單的效率問題,加入說向10w級使用者量的小應用推送訊息,同時儲存每個使用者的推送訊息記錄,如果使用for迴圈,可能出現的場景可能是伺服器掛掉,100w次對乙個表插入會把該錶鎖死,最終掛掉.
insert into zx_client
(,phone
,real_name
,age
,***
,id_no
,id_addr
) values (,#
,#,#
,#,#,#)
通過
listlist =new arraylist<>();
想list加入一定數量的物件
clientservice.insertbatch(list)
collection:指定要遍歷的集合:如果數量較大防止記憶體溢位也可以把乙個集合拆分為幾個集合list型別的引數會特殊處理封裝在map中,map的key就叫list
item:將當前遍歷出的元素賦值給指定的變數
separator:每個元素之間的分隔符
open:遍歷出所有結果拼接乙個開始的字元
close:遍歷出所有結果拼接乙個結束的字元
index:索引。遍歷list的時候是index就是索引,item就是當前值
遍歷map的時候index表示的就是map的key,item就是map的值
#就能取出變數的值也就是當前遍歷出的元素
/**
* 將指定集合分拆成幾個集合
* @param list 即將拆分的集合
* @param num 拆成幾份
*/public static mapsublist(list list,integer num)
bigdecimal ratio = new bigdecimal("1").divide(new bigdecimal(num),ratio_num, roundingmode.half_down);
bigdecimal size = new bigdecimal(list.size()).multiply(ratio);
int begin = 0;
int end = new bigdecimal(list.size()).multiply(ratio).intvalue();
for (int i = 0;i也可以使用以下方法
public void insertbatch(mapparammap, listlist) throws exception }}
} catch (exception e) finally
}
MyBatis 批量插入
1.遇到問題 insert at least 1 column 報錯說 至少插入一列 錯誤的語句 select studentcode,roomcode,n x,n y from把括號去掉 select studentcode,roomcode,n x,n y from 2.column count...
mybatis 批量插入
單字段的批量更新如 list存的只有id 將前端接收的id集合拼接的字串解析 listidlist new arraylist idlist.add 1 idlist.add 2 idlist.add 3 要修改的資訊 sql語句類似 update role set modifier modify ...
mybatis批量插入,批量更新
insert into t ingco trade lithium electric product product no,li e product no,transpor report number,msds,transpor report number path,msds path,un tes...