在項(xiàng)目中遇到需要批量更新的功能,原本想的是在Java中用循環(huán)訪問數(shù)據(jù)庫去更新,但是心里總覺得這樣做會(huì)不會(huì)太頻繁了,太耗費(fèi)資源了,效率也很低,查了下mybatis的批量操作,原來確實(shí)有<foreach>標(biāo)簽可以做到。
dao 層接口:
1
2
3
4
5
|
public class Demo{ private int id; private String name; private String sex; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<pre name= "code" class = "html" > public int update( @Param ( "list" ) List<Demo> list);</pre><br> <br> <p></p> <pre></pre> <br> xml 文件: <p></p> <p><update id= "update" parameterType= "java.util.List" ><br> </p> <p>update bpm_info set message_id= 1 where id in <br> <span style= "white-space:pre" ></span><foreach collection= "list" index= "index" item= "item" open= "(" separator= "," close= ")" ><br> <span style= "white-space:pre" ></span>#{item.id}<br> <span style= "white-space:pre" ></span></foreach><br> </update><br> </p> <p><br> </p> <p>以上這種做法適用情況是:根據(jù)傳入的List參數(shù)集合中的每一個(gè)id遍歷去更新指定字段。。</p> <p><br> </p> <p>其中:</p> <p> 1 .collection 中要對(duì)應(yīng)接口中集合的名稱</p> <p> 2 .item 是集合的別名</p> <p><br> </p> |
以上所述是小編給大家介紹的Mybatis 中的sql批量修改方法實(shí)現(xiàn),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://blog.csdn.net/sinat_34864196/article/details/54703210