当前位置:网站首页>数据库批量插入数据
数据库批量插入数据
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));
}
}
边栏推荐
- Rhcsa --- work on the fourth day
- Getting started with pytest ----- confitest Application of PY
- Johnson–Lindenstrauss Lemma(2)
- 解析少儿编程中的动手搭建教程
- LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)
- Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share
- [quick view opencv] familiar with CV matrix operation with image splicing examples (3)
- fastText文本分类
- Orthogonal test method and function diagram method for test case design
- Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
猜你喜欢
The underlying principle of go map (storage and capacity expansion)
Application d'un robot intelligent dans le domaine de l'agroécologie
[opencv] image binarization
06 装饰(Decorator)模式
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
No logic is executed after the El form is validated successfully
运维工作的“本手、妙手、俗手”
Fabric.js 右键菜单
Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
[common error] the DDR type of FPGA device is selected incorrectly
随机推荐
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
运维工作的“本手、妙手、俗手”
js面试收藏试题1
农业生态领域智能机器人的应用
The underlying principle of go map (storage and capacity expansion)
Here comes the chicken soup! Keep this quick guide for data analysts
Case sharing | intelligent Western Airport
Getting started with pytest ----- confitest Application of PY
Basic differences between Oracle and MySQL (entry level)
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
Typescript function details
数据库问题汇总
Fabric.js IText 手动设置斜体
Mouse events in JS
4. Flask cooperates with a tag to link internal routes
Pytest learning ----- pytest assertion of interface automation testing
農業生態領域智能機器人的應用
Video multiple effects production, fade in effect and border background are added at the same time