方式一:
1
2
3
4
5
6
7
8
9
10
|
<update id= "updatebatch" parametertype= "java.util.list" > <foreach collection= "list" item= "item" index= "index" open= "" close= "" separator= ";" > update tablename <set> name=${item.name}, name2=${item.name2} </set> where id = ${item.id} </foreach> </update> |
但mybatis映射文件中的sql語(yǔ)句默認(rèn)是不支持以" ; " 結(jié)尾的,也就是不支持多條sql語(yǔ)句的執(zhí)行。所以需要在連接mysql的url上加 &allowmultiqueries=true 這個(gè)才可以執(zhí)行。
方式二:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<update id= "updatebatch" parametertype= "java.util.list" > update tablename <trim prefix= "set" suffixoverrides= "," > <trim prefix= "c_name =case" suffix= "end," > <foreach collection= "list" item= "cus" > < if test= "cus.name!=null" > when id=#{cus.id} then #{cus.name} </ if > </foreach> </trim> <trim prefix= "c_age =case" suffix= "end," > <foreach collection= "list" item= "cus" > < if test= "cus.age!=null" > when id=#{cus.id} then #{cus.age} </ if > </foreach> </trim> </trim> <where> <foreach collection= "list" separator= "or" item= "cus" > id = #{cus.id} </foreach> </where> </update> |
這種方式貌似效率不高,但是可以實(shí)現(xiàn),而且不用改動(dòng)mysql連接
效率參考文章:http://www.zmynmublwnt.cn/article/176279.html
方式三:
臨時(shí)改表sqlsessionfactory的屬性,實(shí)現(xiàn)批量提交的java,但無(wú)法返回受影響數(shù)量。
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
26
27
28
29
|
public int updatebatch(list<object> list){ if (list == null || list.size() <= 0 ){ return - 1 ; } sqlsessionfactory sqlsessionfactory = springcontextutil.getbean( "sqlsessionfactory" ); sqlsession sqlsession = null ; try { sqlsession = sqlsessionfactory.opensession(executortype.batch, false ); mapper mapper = sqlsession.getmapper(mapper. class ); int batchcount = 1000 ; //提交數(shù)量,到達(dá)這個(gè)數(shù)量就提交 for ( int index = 0 ; index < list.size(); index++) { object obj = list.get(index); mapper.updateinfo(obj); if (index != 0 && index%batchcount == 0 ){ sqlsession.commit(); } } sqlsession.commit(); return 0 ; } catch (exception e){ sqlsession.rollback(); return - 2 ; } finally { if (sqlsession != null ){ sqlsession.close(); } } } |
其中 springcontextutil 是自己定義的工具類 用來(lái)獲取spring加載的bean對(duì)象,其中g(shù)etbean() 獲得的是想要得到的sqlsessionfactory。mapper 是自己的更具業(yè)務(wù)需求的mapper接口類,object是對(duì)象。
總結(jié)
- 方式一 需要修改mysql的連接url,讓全局支持多sql執(zhí)行,不太安全
- 方式二 當(dāng)數(shù)據(jù)量大的時(shí)候 ,效率明顯降低
- 方式三 需要自己控制,自己處理,一些隱藏的問(wèn)題無(wú)法發(fā)現(xiàn)。
附件:springcontextutil.java
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
26
27
28
29
30
|
@component public class springcontextutil implements applicationcontextaware{ private static applicationcontext applicationcontext; @override public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception { springcontextutil.applicationcontext = applicationcontext; } public static applicationcontext getapplicationcontext(){ return applicationcontext; } public static object getbean( class t){ try { return applicationcontext.getbean(t); } catch (beansexception e){ return null ; } } public static object getbean(string name){ try { return applicationcontext.getbean(name); } catch (beansexception e){ return null ; } } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://segmentfault.com/a/1190000018084851