jpa 批量插入


@Override
@Transactional
public <S extends E> List<S> save(Iterable<S> entities) {
List<S> result = new ArrayList<>();
if (entities == null) {
return result;
}
Iterator<S> iterator = entities.iterator();
int i = 0;
int count = 0;
//遍历循环 每20个(可以做成配置中读取) insert 批量插入一次库
while (iterator.hasNext()) {
S entity = iterator.next();
entityManager.persist(entity);
result.add(entity);
i++;
if (i % 20 == 0) {
entityManager.flush();
entityManager.clear();
count=0;
}else {
count++;
}
}
//判断 是否有剩余未flush的 最后flush
if (count>0){
entityManager.flush();
entityManager.clear();
}
return result;
}
https://github.com/Alexey-/spring-boot-batch/blob/master/src/main/resources/application.properties


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM