MyBatis批量插入和刪除中雙層迴圈的寫法

2021-08-18 13:46:59 字數 1458 閱讀 4719

本部落格主要用兩個例子來說明一下批量刪除和批量插入雙層迴圈的用法,順便自己記錄一下,方便以後使用。

1、批量刪除

(1):dao中的寫法:

public int batchdelprice(@param("deletelist")list> deletelist);
其中deletelist是乙個map的集合,map中的object是乙個list集合,deletelist拼接如下:

listdeletepriceid = getdelpriceid(oripriceid,nowpriceid);

mapdeletemap = new hashmap();

deletemap.put("usercode", usercode);

deletemap.put("delete", deletepriceid);

deletelist.add(deletemap);

(2):xml中的寫法:

delete from ***

where user_code = #

and product_id in

#

注意:批量刪除操作,每個sql間是以分號間隔的,即最外層分隔符是separator=";"。

2、批量插入:

(1):dao中的寫法:

public int batchaddprice(@param("addlist")list> newaddlist);
newaddlist中的資料拼接如下:

list> newaddlist = new arraylist>();

for(int i = 0; i < addlist.size(); i++)

usermap.put("pricelist", pricelist);

newaddlist.add(usermap);

}

(2):xml中的寫法:

insert into ***(

user_code,product_id,price,efftime,index_num,pubtime

)values

(#,#,#,now(),1,now()

)

(1):dao中的寫法:

@mapkey("product_id") 

public map> queryproductdefprice();

其中@mapkey中是你要當成查出的map的key值的欄位名稱,map中的字段為查詢出的字段,我這裡查出的map是:}。

(2):xml中的寫法:

select product_id,default_price from ***

希望對大家有所幫助。

mybatis之批量插入和批量刪除

在程式中封裝了乙個list集合物件,然後需要把該集合中的實體插入到資料庫中,專案使用了spring mvc mybatis的配置,使用mybatis批量插入。主要是用foreach標籤,它可以在sql語句中進行迭代乙個集合。foreach元素的屬性主要有 item,index,collection,...

MyBatis批量插入和批量新增。

曾在學習redis時,技術牛畫了乙個資料庫的發展圖,redis最終發展竟是關係型資料庫 你恨不得想說個oh 就像ibatis發展到mybatis,最後發展方向極可能是hibernate,這個.create table batch options test id varchar2 50 not nul...

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...