接下來兩節(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"
數(shù)據(jù)庫插入結(jié)果:
但是第二種僅僅用了2秒多就ok了。可見這種效率很高。
數(shù)據(jù)庫結(jié)果
這里寫了兩個(gè),其實(shí)第一種僅僅是做對(duì)比效率用。
批量新增數(shù)據(jù)記錄完畢。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/xu1916659422/article/details/77971867