一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - Java教程 - mybatis學(xué)習(xí)之路mysql批量新增數(shù)據(jù)的方法

mybatis學(xué)習(xí)之路mysql批量新增數(shù)據(jù)的方法

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

這篇文章主要介紹了mybatis學(xué)習(xí)之路mysql批量新增數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

接下來兩節(jié)要探討的是批量插入和批量更新,因?yàn)檫@兩種操作在企業(yè)中也經(jīng)常用到。

mysql新增語句  

insert into 表名(字段,字段。。。) values ( 值,值 。。。);此種適合單條插入。

批量插入,一種可以在代碼中循環(huán)著執(zhí)行上面的語句,但是這種效率太差,下面會(huì)有對(duì)比,看看它有多差。

另一種,可以用mysql支持的批量插入語句,

insert into 表名(字段,字段。。。) values ( 值,值 。。。),( 值,值 。。。),( 值,值 。。。)....

這種方式相比起來,更高效。

下面開始來實(shí)現(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
<!-- 跟普通的insert沒有什么不同的地方 ,主要用來跟下面的批量插入做對(duì)比。-->
 <insert id="insert" parametertype="com.soft.mybatis.model.customer">
   <!-- 跟自增主鍵方式相比,這里的不同之處只有兩點(diǎn)
         1 insert語句需要寫id字段了,并且 values里面也不能省略
         2 selectkey 的order屬性需要寫成before 因?yàn)檫@樣才能將生成的uuid主鍵放入到model中,
         這樣后面的insert的values里面的id才不會(huì)獲取為空
      跟自增主鍵相比就這點(diǎn)區(qū)別,當(dāng)然了這里的獲取主鍵id的方式為 select uuid()
      當(dāng)然也可以另寫別生成函數(shù)。-->
   <selectkey keyproperty="id" order="before" resulttype="string">
     select uuid()
   </selectkey>
   insert into t_customer (id,c_name,c_sex,c_cerono,c_cerotype,c_age)
   values (#{id},#{name},#{sex},#{cerono},#{cerotype},#{age})
 </insert>
 
 <!-- 批量插入, -->
 <insert id="batchinsert" parametertype="java.util.map">
   <!-- 這里只做演示用,真正項(xiàng)目中不會(huì)寫的這么簡(jiǎn)單。 -->
   insert into
    t_customer (id,c_name,c_sex,c_cerono,c_cerotype,c_age)
   values
   <!-- foreach mybatis循環(huán)集合用的
      collection="list" 接收的map集合中的key 用以循環(huán)key對(duì)應(yīng)的屬性
        separator="," 表示每次循環(huán)完畢,在sql后面放一個(gè)逗號(hào)
        item="cus" 每次循環(huán)的實(shí)體對(duì)象 名稱隨意-->
   <foreach collection="list" separator="," item="cus">
     <!-- 組裝values對(duì)象,因?yàn)檫@張表的主鍵為非自增主鍵,所以這里 (select uuid()) 用于生成id的值-->
     ((select uuid()),#{cus.name},#{cus.sex},#{cus.cerono},#{cus.cerotype},#{cus.age})
   </foreach>
 </insert>

實(shí)體model對(duì)象

?
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
package com.soft.mybatis.model;
 
/**
 * created by xuweiwei on 2017/9/10.
 */
public class customer {
 
  private string id;
  private string name;
  private integer age;
  private integer sex;
  private string cerono;
  private integer cerotype;
 
  public string getid() {
    return id;
  }
 
  public void setid(string id) {
    this.id = id;
  }
 
  public string getname() {
    return name;
  }
 
  public void setname(string name) {
    this.name = name;
  }
 
  public integer getage() {
    return age;
  }
 
  public void setage(integer age) {
    this.age = age;
  }
 
  public integer getsex() {
    return sex;
  }
 
  public void setsex(integer sex) {
    this.sex = sex;
  }
 
  public string getcerono() {
    return cerono;
  }
 
  public void setcerono(string cerono) {
    this.cerono = cerono;
  }
 
  public integer getcerotype() {
    return cerotype;
  }
 
  public void setcerotype(integer cerotype) {
    this.cerotype = cerotype;
  }
 
  @override
  public string tostring() {
    return "customer{" +
        "id='" + id + '\'' +
        ", name='" + name + '\'' +
        ", age=" + age +
        ", sex=" + sex +
        ", cerono='" + cerono + '\'' +
        ", cerotype='" + cerotype + '\'' +
        '}';
  }
}

接口

?
1
2
int add(customer customer);
int batchinsert(map<string,object> param);

實(shí)現(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
/**
  * 新增數(shù)據(jù)
  * @param customer
  * @return
  */
 public int add(customer customer) {
   return insert("customer.insert", customer);
 }
 
 /**
  * 批量插入數(shù)據(jù)
  * @param param
  * @return
  */
 public int batchinsert(map<string,object> param) {
   return insert("customer.batchinsert", param);
 }
 
 /**
  * 公共部分
  * @param statementid
  * @param obj
  * @return
  */
 private int insert(string statementid, object obj){
   sqlsession sqlsession = null;
   try {
     sqlsession = sqlsessionutil.getsqlsession();
     int key = sqlsession.insert(statementid, obj);
     // commit
     sqlsession.commit();
     return key;
   } catch (exception e) {
     sqlsession.rollback();
     e.printstacktrace();
   } finally {
     sqlsessionutil.closesession(sqlsession);
   }
   return 0;
 }

測(cè)試類

?
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
@test
 public void add() throws exception {
   long start = system.currenttimemillis();
   for(int i=0;i<1000;i++){
     customer customer = new customer();
     customer.setname("普通一條條插入 "+ i);
     customer.setage(15);
     customer.setcerono("000000000000"+ i);
     customer.setcerotype(2);
     customer.setsex(1);
     int result = customerdao.add(customer);
   }
   system.out.println("耗時(shí) : "+(system.currenttimemillis() - start));
 }
 
 @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<1000;i++){
     customer customer = new customer();
     customer.setname("批量插入" + 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("耗時(shí) : "+(system.currenttimemillis() - start));
 }

兩種都進(jìn)行插入1000條測(cè)試

由于我沒有用連接池等等原因,在插入了700多條的時(shí)候 junit直接掛了,

cause: org.apache.ibatis.executor.executorexception: error selecting key or setting result to parameter object.

cause: com.mysql.jdbc.exceptions.jdbc4.mysqlnontransientconnectionexception:

data source rejected establishment of connection,  message from server: "too many connections"

mybatis學(xué)習(xí)之路mysql批量新增數(shù)據(jù)的方法

數(shù)據(jù)庫插入結(jié)果:

mybatis學(xué)習(xí)之路mysql批量新增數(shù)據(jù)的方法

但是第二種僅僅用了2秒多就ok了。可見這種效率很高。

mybatis學(xué)習(xí)之路mysql批量新增數(shù)據(jù)的方法

數(shù)據(jù)庫結(jié)果

mybatis學(xué)習(xí)之路mysql批量新增數(shù)據(jù)的方法

這里寫了兩個(gè),其實(shí)第一種僅僅是做對(duì)比效率用。

批量新增數(shù)據(jù)記錄完畢。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久这里只有精品无码3D | 国内精品视频免费观看 | 国产亚洲毛片在线 | 国产精品久久久久影院色老大 | 99国产精品免费观看视频 | 日本在线色 | 亚洲国产网址 | 亚洲欧美日韩久久一区 | 国产乱子伦一区二区三区 | 青草热久精品视频在线观看 | 卫生间被教官做好爽HH视频 | 欧美一区二区三 | 继攵催眠女乱h调教 | 嫩草影院永久在线播放 | 高清国产欧美一v精品 | 免费一级毛片在级播放 | 国产精品欧美亚洲韩国日本99 | 欧美日韩人成在线观看 | 好大好硬好深好爽gif图 | jizz 日本亚洲 | 日韩精品一区二区三区免费视频 | 91中文在线| 国产成人一区二区三区在线视频 | 99一区二区三区 | 免费超级乱淫播放手机版 | 日韩成人免费 | 恩不要好大好硬好爽3p | 丝袜兔女郎被啪在线观看91 | 亚洲视频在线一区二区三区 | 视频二区 素人 欧美 日韩 | 99在线观看免费视频 | 国产 日韩 欧美视频二区 | 精品亚洲视频在线观看 | 日本韩国一区二区三区 | 忘忧草研究院一二三 | 亚洲 综合 欧美在线 热 | 成人国产一区 | 色婷在线| 亚洲黑人巨大videos0 | 我将她侵犯1~6樱花动漫在线看 | 免费久久久久 |