接下來兩節要探討的是批量插入和批量更新,因為這兩種操作在企業中也經常用到。
mysql新增語句
insert into 表名(字段,字段。。。) values ( 值,值 。。。);此種適合單條插入。
批量插入,一種可以在代碼中循環著執行上面的語句,但是這種效率太差,下面會有對比,看看它有多差。
另一種,可以用mysql支持的批量插入語句,
insert into 表名(字段,字段。。。) values ( 值,值 。。。),( 值,值 。。。),( 值,值 。。。)....
這種方式相比起來,更高效。
下面開始來實現。
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沒有什么不同的地方 ,主要用來跟下面的批量插入做對比。--> <insert id= "insert" parametertype= "com.soft.mybatis.model.customer" > <!-- 跟自增主鍵方式相比,這里的不同之處只有兩點 1 insert語句需要寫id字段了,并且 values里面也不能省略 2 selectkey 的order屬性需要寫成before 因為這樣才能將生成的uuid主鍵放入到model中, 這樣后面的insert的values里面的id才不會獲取為空 跟自增主鍵相比就這點區別,當然了這里的獲取主鍵id的方式為 select uuid() 當然也可以另寫別生成函數。--> <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" > <!-- 這里只做演示用,真正項目中不會寫的這么簡單。 --> insert into t_customer (id,c_name,c_sex,c_cerono,c_cerotype,c_age) values <!-- foreach mybatis循環集合用的 collection= "list" 接收的map集合中的key 用以循環key對應的屬性 separator= "," 表示每次循環完畢,在sql后面放一個逗號 item= "cus" 每次循環的實體對象 名稱隨意--> <foreach collection= "list" separator= "," item= "cus" > <!-- 組裝values對象,因為這張表的主鍵為非自增主鍵,所以這里 (select uuid()) 用于生成id的值--> ((select uuid()),#{cus.name},#{cus.sex},#{cus.cerono},#{cus.cerotype},#{cus.age}) </foreach> </insert> |
實體model對象
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); |
實現
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
|
/** * 新增數據 * @param customer * @return */ public int add(customer customer) { return insert( "customer.insert" , customer); } /** * 批量插入數據 * @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 ; } |
測試類
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( "耗時 : " +(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( "耗時 : " +(system.currenttimemillis() - start)); } |
兩種都進行插入1000條測試
由于我沒有用連接池等等原因,在插入了700多條的時候 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"
數據庫插入結果:
但是第二種僅僅用了2秒多就ok了。可見這種效率很高。
數據庫結果
這里寫了兩個,其實第一種僅僅是做對比效率用。
批量新增數據記錄完畢。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/xu1916659422/article/details/77971867