日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

c mysql批量添加数据类型_mybatis学习之路----mysql批量新增数据

發布時間:2024/10/8 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c mysql批量添加数据类型_mybatis学习之路----mysql批量新增数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

接下來兩節要探討的是批量插入和批量更新,因為這兩種操作在企業中也經常用到。

mysql新增語句

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

批量插入,

一種可以在代碼中循環著執行上面的語句,但是這種效率太差,下面會有對比,看看它有多差。

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

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

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

下面開始來實現。

select uuid()

insert into t_customer (id,c_name,c_sex,c_ceroNo,c_ceroType,c_age)

values (#{id},#{name},#{sex},#{ceroNo},#{ceroType},#{age})

insert into

t_customer (id,c_name,c_sex,c_ceroNo,c_ceroType,c_age)

values

((select uuid()),#{cus.name},#{cus.sex},#{cus.ceroNo},#{cus.ceroType},#{cus.age})

實體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 param);

實現

/**

* 新增數據

* @param customer

* @return

*/

public int add(Customer customer) {

return insert("customer.insert", customer);

}

/**

* 批量插入數據

* @param param

* @return

*/

public int batchInsert(Map 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 param = new HashMap();

List list = new ArrayList();

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了??梢娺@種效率很高。

數據庫結果

這里寫了兩個,其實第一種僅僅是做對比效率用。

批量新增數據記錄完畢。

---------------------

作者:第一小菜鳥

來源:CSDN

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

版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

總結

以上是生活随笔為你收集整理的c mysql批量添加数据类型_mybatis学习之路----mysql批量新增数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。