激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - 詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對比

詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對比

2021-07-14 15:31第一小菜鳥 Java教程

這篇文章主要介紹了詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對比,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

上節(jié)探討了批量新增數(shù)據(jù),這節(jié)探討批量更新數(shù)據(jù)兩種寫法的效率問題。

實現(xiàn)方式有兩種,

一種用for循環(huán)通過循環(huán)傳過來的參數(shù)集合,循環(huán)出n條sql,

另一種 用mysql的case when 條件判斷變相的進行批量更新  

下面進行實現(xiàn)。

注意第一種方法要想成功,需要在db鏈接url后面帶一個參數(shù)  &allowmultiqueries=true

即:  jdbc:mysql://localhost:3306/mysqltest?characterencoding=utf-8&allowmultiqueries=true

其實這種東西寫過來寫過去就是差不多一樣的代碼,不做重復的贅述,直接上代碼。

?
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!-- 這次用resultmap接收輸出結果 -->
 <select id="findbyname" parametertype="string" resultmap="customermap">
   select * from t_customer where c_name like concat('%', #{name},'%') order by c_cerono limit 0,100
 </select>
 
 
 <!-- 批量更新第一種方法,通過接收傳進來的參數(shù)list進行循環(huán)著組裝sql -->
 <update id="batchupdate" parametertype="java.util.map">
   <!-- 接收list參數(shù),循環(huán)著組裝sql語句,注意for循環(huán)的寫法
      separator=";" 代表著每次循環(huán)完,在sql后面放一個分號
      item="cus" 循環(huán)list的每條的結果集
      collection="list" list 即為 map傳過來的參數(shù)key -->
   <foreach collection="list" separator=";" item="cus">
     update t_customer set
     c_name = #{cus.name},
     c_age = #{cus.age},
     c_sex = #{cus.sex},
     c_cerono = #{cus.cerono},
     c_cerotype = #{cus.cerotype}
     where id = #{cus.id}
   </foreach>
 </update>
 
 <!-- 批量更新第二種方法,通過 case when語句變相的進行批量更新 -->
 <update id="batchupdatecasewhen" parametertype="java.util.map">
   update t_customer
   <trim prefix="set" suffixoverrides=",">
     <!-- 拼接case when 這是一種寫法 -->
     <!--<foreach collection="list" separator="" item="cus" open="c_age = case id" close="end, ">-->
     <!--when #{cus.id} then #{cus.age}-->
     <!--</foreach>-->
 
     <!-- 拼接case when 這是另一種寫法,這種寫著更專業(yè)的感覺 -->
     <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 prefix="c_sex =case" suffix="end,">
       <foreach collection="list" item="cus">
         <if test="cus.sex!=null">
           when id=#{cus.id} then #{cus.sex}
         </if>
       </foreach>
     </trim>
     <trim prefix="c_cerono =case" suffix="end,">
       <foreach collection="list" item="cus">
         <if test="cus.cerono!=null">
           when id=#{cus.id} then #{cus.cerono}
         </if>
       </foreach>
     </trim>
     <trim prefix="c_cerotype =case" suffix="end,">
       <foreach collection="list" item="cus">
         <if test="cus.cerotype!=null">
           when id=#{cus.id} then #{cus.cerotype}
         </if>
       </foreach>
     </trim>
   </trim>
   <where>
     <foreach collection="list" separator="or" item="cus">
       id = #{cus.id}
     </foreach>
   </where>
 </update>

接口

?
1
2
3
4
5
list<customer> findbyname(string name);
 
 int batchupdate(map<string,object> param);
 
 int batchupdatecasewhen(map<string,object> param);

實現(xiàn)類

?
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
  * 用于更新時,獲取更新數(shù)據(jù)
  * @param name
  * @return
  */
 public list<customer> findbyname(string name) {
   sqlsession sqlsession = null;
   try {
     sqlsession = sqlsessionutil.getsqlsession();
     return sqlsession.selectlist("customer.findbyname", name);
   } catch (exception e) {
     e.printstacktrace();
   } finally {
     sqlsessionutil.closesession(sqlsession);
   }
   return new arraylist<customer>();
 }
 
 
 /**
  * 批量更新第一種方式
  * @param param
  * @return
  */
 public int batchupdate(map<string,object> param) {
   return bathupdate("customer.batchupdate",param);
 }
 
 /**
  * 批量更新第二種方式
  * @param param
  * @return
  */
 public int batchupdatecasewhen(map<string,object> param) {
   return bathupdate("customer.batchupdatecasewhen",param);
 }
 
 /**
  * 公共部分提出
  * @param statementid
  * @param param
  * @return
  */
 private int bathupdate(string statementid,map param){
   sqlsession sqlsession = null;
   try {
     sqlsession = sqlsessionutil.getsqlsession();
     int key = sqlsession.update(statementid, param);
     // commit
     sqlsession.commit();
     return key;
   } catch (exception e) {
     sqlsession.rollback();
     e.printstacktrace();
   } finally {
     sqlsessionutil.closesession(sqlsession);
   }
   return 0;
 }

測試前準備   首先用上節(jié)的 批量插入,插入10000條數(shù)據(jù)以備下面的批量更新用。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@test
  public void batchinsert() throws exception {
    map<string,object> param = new hashmap<string,object>();
    list<customer> list = new arraylist<customer>();
    for(int i=0;i<10000;i++){
      customer customer = new customer();
      customer.setname("準備數(shù)據(jù)" + i);
      customer.setage(15);
      customer.setcerono("111111111111"+i);
      customer.setcerotype(2);
      customer.setsex(1);
      list.add(customer);
    }
    param.put("list",list);
    long start = system.currenttimemillis();
    int result = customerdao.batchinsert(param);
    system.out.println("耗時 : "+(system.currenttimemillis() - start));
  }

開始進行測試效率問題。

首先進行的是測試十條數(shù)據(jù)。調(diào)整查詢數(shù)據(jù)為查詢十條

?
1
2
3
4
<!-- 這次用resultmap接收輸出結果 -->
<select id="findbyname" parametertype="string" resultmap="customermap">
  select * from t_customer where c_name like concat('%', #{name},'%') order by c_cerono limit 0,10
</select>

測試類

?
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
@test
public void batchudpate() throws exception {
  map<string,object> param = new hashmap<string,object>();
 
  param.put("list",getfindbyname("準備數(shù)據(jù)","批量更新01"));
  long start = system.currenttimemillis();
  customerdao.batchupdate(param);
  system.out.println("耗時 : "+(system.currenttimemillis() - start));
}
 
@test
public void batchudpatecasewhen() throws exception {
  map<string,object> param = new hashmap<string,object>();
  param.put("list",getfindbyname("批量更新01","準備數(shù)據(jù)"));
  long start = system.currenttimemillis();
  customerdao.batchupdatecasewhen(param);
  system.out.println("耗時 : "+(system.currenttimemillis() - start));
}
 
private list<customer> getfindbyname(string name, string change){
  list<customer> list = customerdao.findbyname(name);
  system.out.println("查詢出來的條數(shù) : " + list.size());
  if(null != change && !"".equals(change)){
    for(customer customer : list){
      customer.setname(change);
    }
  }
 
  return list;
}

第一種拼完整sql的方式耗時:

詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對比

第二種case when 耗時情況:

詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對比

結果可以看出,其實case when 耗時比較多。

下面來加大數(shù)據(jù)量到100條;

第一種拼完整sql的方式耗時:

詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對比

第二種case when 耗時情況:

詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對比

結果可以看出,其實case when 耗時仍然比第一種多。

繼續(xù)加大數(shù)據(jù)量到1000條

第一種拼完整sql的方式耗時:

詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對比

第二種case when 耗時情況:

詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對比

結果可以看出,其實case when 耗時仍然比第一種多。

繼續(xù)加大數(shù)據(jù)量到10000條

第一種拼完整sql的方式耗時:

詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對比

第二種case when 耗時情況:

詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對比

結果可以看出,兩種方式進行批量更新,效率已經(jīng)不在一個數(shù)量級了。case when明顯的慢的多。

看網(wǎng)上有人說第一種的效率跟用代碼循環(huán)著一條一條的循環(huán)著插入的效率差不多,通過測試我就有疑問了,他是怎么做到的。難道我的代碼有問題?明明第一種的效率很高嘛。

第一種效率其實相當高的,因為它僅僅有一個循環(huán)體,只不過最后update語句比較多,量大了就有可能造成sql阻塞。

第二種雖然最后只會有一條更新語句,但是xml中的循環(huán)體有點多,每一個case when 都要循環(huán)一遍list集合,所以大批量拼sql的時候會比較慢,所以效率問題嚴重。使用的時候建議分批插入。

根據(jù)效率,安全方面綜合考慮,選擇適合的很重要。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/xu1916659422/article/details/77971696/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久成人综合网 | 一级黄色免费观看 | 成人爱情偷拍视频在线观看 | 国产一级午夜 | 一区二区三区欧美在线观看 | 欧美 国产 亚洲 卡通 综合 | 国产在线a| 免费a级片视频 | 日韩在线视频免费观看 | 国产亚洲精品视频中文字幕 | 久久亚洲精品久久国产一区二区 | 久久久久免费精品国产小说色大师 | 亚洲成人夜色 | 91久久夜色精品国产网站 | 日本一区二区不卡高清 | 国产一级二级视频 | 久久成人午夜视频 | 国产一级毛片高清视频完整版 | 十级毛片| 俄罗斯hdxxx 日夜操天天干 | 日韩精品久久久久久久九岛 | 黄色大片高清 | 国产亚洲精品成人 | 久久骚 | 91丨九色丨国产在线观看 | 欧美久久久一区二区三区 | av日韩一区二区 | 欧美淫视频 | 亚洲射逼 | 国产亚洲精品yxsp | hd日本xxxx| 国产精品久久久乱弄 | 成熟女人特级毛片www免费 | 性爱视频在线免费 | 午夜视频在线观 | 日韩理论电影网 | 免费黄色大片在线观看 | 亚洲一区二区三区在线免费观看 | 亚洲网站在线观看 | 久久久久久久一区二区三区 | 日韩一级免费毛片 |