当前位置:网站首页>数据库批量插入数据
数据库批量插入数据
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));
}
}
边栏推荐
- 黑马笔记---Set系列集合
- 农业生态领域智能机器人的应用
- Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
- How to configure PostgreSQL 12.9 to allow remote connections
- 运维工作的“本手、妙手、俗手”
- LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)
- 7.1 Résumé du concours de simulation
- Analyzing the hands-on building tutorial in children's programming
- Fabric.js 3个api设置画布宽高
- Fabric.js 自由绘制矩形
猜你喜欢

Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I

函数栈帧的创建和销毁

Learn BeanShell before you dare to say you know JMeter

Dark horse notes -- map set system

Video cover image setting, put cover images into multiple videos in the simplest way

Fabric.js 精简JSON

4. Flask cooperates with a tag to link internal routes

数据库问题汇总

About PROFIBUS: communication backbone network of production plant

Steam教育的实际问题解决能力
随机推荐
Case sharing | intelligent Western Airport
案例分享|智慧化的西部机场
LeetCode 1175. 质数排列(质数判断+组合数学)
Fabric.js 圆形笔刷
6.30年终小结,学生时代结束
How to make an RPM file
农业生态领域智能机器人的应用
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
Future trend of automated testing ----- self healing technology
Johnson–Lindenstrauss Lemma(2)
paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
JS interview collection test question 1
函数栈帧的创建和销毁
Implementation of leetcode two number addition go
Lm09 Fisher inverse transform inversion mesh strategy
Collectors.groupingBy 排序
Essence and physical meaning of convolution (deep and brief understanding)
Video cover image setting, put cover images into multiple videos in the simplest way
Feign realizes file uploading and downloading
The underlying principle of go map (storage and capacity expansion)