轉:http://www.cnblogs.com/winkey4986/p/3915151.html
Mybatis批量更新
批量操作就不進行贅述了。減少服務器與數據庫之間的交互。網上有很多關於批量插入還有批量刪除的帖子。但是批量更新卻沒有詳細的解決方案。
實現目標
這里主要講的是1張table中。根據不同的id值,來update不同的property。
數據表:1張。Tblsupertitleresult。錯題結果統計。
表結構:

表中每一條數據必須通過兩個字段來確定:userHhCode+titleId
需要批量更新的字段是:correctDate,result,checkState。
1批量更新的sql語句
我用的數據庫是mysql。其他數據庫的sql語句也都大同小異。
用mybatis的mapper-xml進行組裝sql之前需要寫出批量操作的sql語句。
Sql:
update tblsupertitleresult set result =case
when (userHhCode=2001 and titleId=1)then 90
when (userHhCode=2001 and titleId=2)then 70
end
,checkState = case
when (userHhCode=2001 and titleId=1)then 80
when (userHhCode=2001 andtitleId=2)then 120
end
where (userHhCode=2001 and titleId=1) or(userHhCode=2001 and titleId=2)
關於這個批量更新的sql語句做一個簡單的解釋。
要更新userHhCode=2001,titleId=1和userHhCode=2001 ,titleId=2的兩條數據。
當userHhCode=2001,titleId=1時,將result設置為90,checkState設置為80
當userHhCode=2001,titleId=2時,將result設置為80,checkState設置為120.
這是mysql語句。運行沒有問題。接下來就是mybatis的mapper-xml
Mybatis中mapper-xml
這里,首先介紹實體類。
public classWrongTitle {
//manipulatetable of tblsupertitleresult
private String titleId;
private String titleIdNew;
private String result;
private String checkState;
private String isCollect;
private String times;
private String wrongDate;
private String wrongNum;
private String collectDate;
private String userHhCode;
private String correctDate;
private String tid;// teacher who will review this wrong title
private String paperTitleId;
getter和set方法省略。
好了現在開始介紹mybatis里面的幾個標簽。由於一些原因,mybatis的技術文檔和用戶指南所介紹得並不詳細。
<foreach>標簽:foreach元素的屬性主要有item,index,collection,open,separator,close。item表示集合中每一個元素進行迭代時的別名,
index指定一個名字,用於表示在迭代過程中,每次迭代到的位置,
open表示該語句以什么開始,
separator表示在每次進行迭代之間以什么符號作為分隔符,
close表示以什么結束,
在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,但是在不同情況下,該屬性的值是不一樣的,主要有一下3種情況:
1.如果傳入的是單參數且參數類型是一個List的時候,collection屬性值為list;
2.如果傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值為array;
3.如果傳入的參數是多個的時候,我們就需要把它們封裝成一個Map了,當然單參數也可以封裝成map,實際上如果你在傳入參數的時候,在MyBatis里面也是會把它封裝成一個Map的,map的key就是參數名,所以這個時候collection屬性值就是傳入的List或array對象在自己封裝的map里面的key;
關於以上三種collection的用法。百度上有很多帖子。這里不進行贅述。
<trim>標簽:有四個屬性:
Prefix: 指的是<trim></trim>所包含的部分(body)以什么開頭。
prefixOverrides:指<trim>中如果有內容時可忽略(body)前的匹配字符。
suffix: 指的是<trim></trim>所包含的部分(body)以什么結尾。
suffixOverrides:指<trim>中如果有內容時可忽略(body)后的匹配字符。
接下來直接上:
Mapper-xml
<update id="batchUpdate">
update tblsupertitleresult
<trim prefix="set" suffixOverrides=",">
<trim prefix="checkState =case" suffix="end,">
<foreach collection="list"item="i" index="index">
<if test="i.checkState!=null">
when (userHhCode=#{i.userHhCode} and titleId=#{i.titleId}) then #{i.checkState}
</if>
</foreach>
</trim>
<trim prefix=" correctDate =case" suffix="end,">
<foreach collection="list"item="i" index="index">
<if test="i.correctDate!=null">
when (userHhCode=#{i.userHhCode} and titleId=#{i.titleId}) then #{i.correctDate}
</if>
</foreach>
</trim>
<trim prefix="result =case" suffix="end," >
<foreach collection="list"item="i" index="index">
<if test="i.result!=null">
when (userHhCode=#{i.userHhCode} and titleId=#{i.titleId}) then #{i.result}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index">
(userHhCode =#{i.userHhCode} andtitleId=#{i.titleId})
</foreach>
</update>
接下來就是dao:
public interface DatacenterDAO{
// batch update super title_result_view
public intbatchUpdate(List<WrongTitle> list );
Test類
public classTestBatch {
/**
* @param args
*/
public static voidmain(String[] args) {
ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");
DatacenterDAO dao = context.getBean(DatacenterDAO.class);
ArrayList<WrongTitle> list = newArrayList<WrongTitle>();
WrongTitle t1=new WrongTitle();
WrongTitle t2=new WrongTitle();
WrongTitle t3=new WrongTitle();
WrongTitle t4=new WrongTitle();
t1.setTitleId(3+"");
t2.setTitleId(4+"");
t3.setTitleId(5+"");
t4.setTitleId(6+"");
t1.setUserHhCode(2001+"");
t2.setUserHhCode(2001+"");
t3.setUserHhCode(2001+"");
t4.setUserHhCode(2001+"");
t1.setCheckState(5+"");
t2.setCheckState(6+"");
t3.setCheckState(7+"");
t4.setCheckState(8+"");
t1.setResult(10+"");
t2.setResult(12+"");
t3.setResult(14+"");
t4.setResult(16+"");
list.add(t1);
list.add(t2);
list.add(t3);
list.add(t4);
int i=dao.batchUpdate(list);
System.out.println("操作了"+i+"行數據");
}
運行結果截圖:

希望能幫助到大家~。~
================
親測可用,但是不知道效率到底如何。

