mybatis mysql 批量_mybatis学习之路mysql批量新增数据的方法

接下来两节要探讨的是批量插入和批量更新,因为这两种操作在企业中也经常用到。

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"

9dd6e242fc5fe8ce94b7840d72bd8beb.png

数据库插入结果:

3d2e1435f5a9f000aa02557e79b9d160.png

但是第二种仅仅用了2秒多就ok了。可见这种效率很高。

295ffe88da5660231e575783a9309c1b.png

数据库结果

48c3ed49587a3d7315e5b136e4f2ded7.png

这里写了两个,其实第一种仅仅是做对比效率用。

批量新增数据记录完毕。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值