<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
server:
port: 8081
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mysql?useSSL=false&characterEncoding=utf-8&useUnicode=true&serverTimezone=GMT%2B8&rewriteBatchedStatements=true
##rewriteBatchedStatements=true 开启批处理模式
package com.cy.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private Integer id;
private String username;
private String password;
}
@RequestMapping("/test3") //单个插入(慢)
public String test3(){
long startTime=System.currentTimeMillis();
for(int i=0;i<100000;i++){
userService.save(new User(i+1,"张三","123456"));
}
long endTime=System.currentTimeMillis();
long result=endTime-startTime;
return "总耗时:"+(result/1000);
}
@RequestMapping("/test2") //批量插入saveBatch(分片处理batchSize=1000,分批提交事务) (次快)
public String test2(){
List<User> userList=new ArrayList<>();
long startTime=System.currentTimeMillis();
for(int i=0;i<100000;i++){
userList.add(new User(i+1,"张三","123456"));
}
userService.saveBatch(userList);
long endTime=System.currentTimeMillis();
long result=endTime-startTime;
return "总耗时:"+(result/1000);
}
@Resource
private SqlSessionFactory sqlSessionFactory;
@RequestMapping("/test1") //批量插入(最快)---循环插入+批处理模式 ( 开启批处理模式 rewriteBatchedStatements=true )
public String test1(){
//开启批处理模式,关闭自动提交事务
SqlSession sqlSession= sqlSessionFactory.openSession(ExecutorType.BATCH,false);
long startTime=System.currentTimeMillis();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
for(int i=0;i<100000;i++){
userMapper.insert(new User(i+1,"张三","123456"));
}
sqlSession.commit();//一次性提交事务
sqlSession.close();//关闭资源
long endTime=System.currentTimeMillis();
long result=endTime-startTime;
return "总耗时:"+(result/1000);
}
免责申明:
本文系转载,版权归原作者所有,如若侵权请联系我们进行删除!
《数据治理行业实践白皮书》下载地址:https://fs80.cn/4w2atu
《数栈V6.0产品白皮书》下载地址:https://fs80.cn/cw0iw1
想了解或咨询更多有关袋鼠云大数据产品、行业解决方案、客户案例的朋友,浏览袋鼠云官网:https://www.dtstack.com/?src=bbs
同时,欢迎对大数据开源项目有兴趣的同学加入「袋鼠云开源框架钉钉技术群」,交流最新开源技术信息,群号码:30537511,项目地址:https://github.com/DTStack