当前位置:网站首页>数据库批量插入数据
数据库批量插入数据
2022-07-02 05:11:00 【@淡 定】
application.properties 中配数据库连接信息进行修改:
spring.datasource.username=root
spring.datasource.password=root
//注意参数参数 rewriteBatchedStatements的配置
spring.datasource.url=jdbc:mysql:///db?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
//实现层
@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IUserService {
private static final Logger logger = LoggerFactory.getLogger(UserService.class);
@Autowired
UserMapper userMapper;
@Autowired
SqlSessionFactory sqlSessionFactory;
@Transactional(rollbackFor = Exception.class)
public void addUserOneByOne(List<User> users) {
//虽然是一条条插入,但是只用了一个sqlsession
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);
UserMapper um = session.getMapper(UserMapper.class);
long startTime = System.currentTimeMillis();
for (User user : users) {
um.addUserOneByOne(user);
}
session.commit();
long endTime = System.currentTimeMillis();
logger.info("一条条插入 SQL 耗费时间 {}", (endTime - startTime));
}
}
边栏推荐
- LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
- Fabric.js 自由绘制矩形
- 函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
- [high speed bus] Introduction to jesd204b
- 关于Steam 教育的知识整理
- Application d'un robot intelligent dans le domaine de l'agroécologie
- 解析少儿编程中的动手搭建教程
- 数学知识——快速幂的理解及例题
- LS1046nfs挂载文件系统
- Save the CDA from the disc to the computer
猜你喜欢
![[Yu Yue education] autumn 2021 reference materials of Tongji University](/img/50/5136359b89a5d047fe648637643ad0.jpg)
[Yu Yue education] autumn 2021 reference materials of Tongji University

Orthogonal test method and function diagram method for test case design

C# 基于MQTTNet的服务端与客户端通信案例

Differential identities (help find mean, variance, and other moments)

10 minute quick start UI automation ----- puppeter

Analyzing the hands-on building tutorial in children's programming

Fabric.js IText 手动设置斜体

农业生态领域智能机器人的应用

Rhcsa --- work on the fourth day

CubeMx DMA笔记
随机推荐
How to configure PostgreSQL 12.9 to allow remote connections
[bus interface] Axi interface
Go implements leetcode rotation array
js中的Map(含leetcode例题)
There are duplicate elements in leetcode. Go implementation
Collectors. Groupingby sort
Getting started with pytest -- description of fixture parameters
解决:代理抛出异常错误
Rhcsa --- work on the third day
No logic is executed after the El form is validated successfully
视差特效的原理和实现方法
奠定少儿编程成为基础学科的原理
Fabric.js 自由绘制矩形
数学问题(数论)试除法做质数的判断、分解质因数,筛质数
Fabric.js IText 手动设置斜体
10 minute quick start UI automation ----- puppeter
7.1 Résumé du concours de simulation
Mapping settings in elk (8) es
Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
Preparation for writing SAP ui5 applications using typescript