当前位置:网站首页>数据库批量插入数据
数据库批量插入数据
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));
}
}
边栏推荐
- 【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘
- Global and Chinese market of cell culture freezers 2022-2028: Research Report on technology, participants, trends, market size and share
- Mouse events in JS
- JS interview collection test question 1
- Fabric.js 将本地图像上传到画布背景
- Mapping settings in elk (8) es
- How to make an RPM file
- 在{{}}中拼接字符
- 案例分享|智慧化的西部机场
- Go GC garbage collection notes (three color mark)
猜你喜欢
Virtual machine installation deepin system
Dark horse notes -- map set system
Learn BeanShell before you dare to say you know JMeter
【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘
解析少儿编程中的动手搭建教程
Pyechats 1.19 generate a web version of Baidu map
Lay the foundation for children's programming to become a basic discipline
Fabric.js 将本地图像上传到画布背景
Creation and destruction of function stack frames
[bus interface] Axi interface
随机推荐
Leetcode basic programming: array
Application d'un robot intelligent dans le domaine de l'agroécologie
ansible安装与使用
Feign realizes file uploading and downloading
7.1 Résumé du concours de simulation
農業生態領域智能機器人的應用
el form 表单validate成功后没有执行逻辑
Fabric.js 背景不受视口变换影响
数学问题(数论)试除法做质数的判断、分解质因数,筛质数
Leetcode 18 problem [sum of four numbers] recursive solution
Rhcsa --- work on the fourth day
Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share
Application of intelligent robot in agricultural ecology
The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
Paddlepaddle project source code
Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
Fabric.js 自由绘制矩形
Differential identities (help find mean, variance, and other moments)
函数栈帧的创建和销毁
从数组中找出和为目标的下标