mybatis學(xué)習(xí)之路mysql批量新增數(shù)據(jù)的方法
接下來兩節(jié)要探討的是批量插入和批量更新,因為這兩種操作在企業(yè)中也經(jīng)常用到。
mysql新增語句
insert into 表名(字段,字段。。。) values ( 值,值 。。。);此種適合單條插入。
批量插入,一種可以在代碼中循環(huán)著執(zhí)行上面的語句,但是這種效率太差,下面會有對比,看看它有多差。
另一種,可以用mysql支持的批量插入語句,
insert into 表名(字段,字段。。。) values ( 值,值 。。。),( 值,值 。。。),( 值,值 。。。)....
這種方式相比起來,更高效。
下面開始來實現(xiàn)。
<!-- 跟普通的insert沒有什么不同的地方 ,主要用來跟下面的批量插入做對比。-->
<insert id="insert" parameterType="com.soft.mybatis.model.Customer">
<!-- 跟自增主鍵方式相比,這里的不同之處只有兩點
1 insert語句需要寫id字段了,并且 values里面也不能省略
2 selectKey 的order屬性需要寫成BEFORE 因為這樣才能將生成的uuid主鍵放入到model中,
這樣后面的insert的values里面的id才不會獲取為空
跟自增主鍵相比就這點區(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">
<!-- 這里只做演示用,真正項目中不會寫的這么簡單。 -->
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對應(yīng)的屬性
separator="," 表示每次循環(huán)完畢,在sql后面放一個逗號
item="cus" 每次循環(huán)的實體對象 名稱隨意-->
<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對象
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 + '\'' +
'}';
}
}
接口
int add(Customer customer); int batchInsert(Map<String,Object> param);
實現(xiàn)
/**
* 新增數(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;
}
測試類
@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));
}
兩種都進(jìn)行插入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"

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

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

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

這里寫了兩個,其實第一種僅僅是做對比效率用。
批量新增數(shù)據(jù)記錄完畢。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用RestTemplate發(fā)送http請求的實操演示
RestTemplate是Spring 框架提供的 ,可用于在應(yīng)用中調(diào)用 rest 服務(wù),它簡化了與 http 服務(wù)的通信方式,統(tǒng)一了 RESTful 的標(biāo)準(zhǔn),封裝了 http 鏈接,本文給大家介紹了SpringBoot使用RestTemplate發(fā)送http請求的實操演示,需要的朋友可以參考下2024-11-11
rabbitmq的消息持久化處理開啟,再關(guān)閉后,消費者啟動報錯問題
這篇文章主要介紹了rabbitmq的消息持久化處理開啟,再關(guān)閉后,消費者啟動報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
深入了解Java中String、Char和Int之間的相互轉(zhuǎn)換
這篇文章主要介紹了深入了解Java中String、Char和Int之間的相互轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,,需要的朋友可以參考下2019-06-06
Java多線程中ReentrantLock與Condition詳解
這篇文章主要介紹了Java多線程中ReentrantLock與Condition詳解,需要的朋友可以參考下2017-11-11

