使用SpringBoot和JPA實現(xiàn)批量處理新增、修改
jpa的sava與saveAll
save()方法
@Transactional
public <S extends T> S save(S entity) {
if (this.entityInformation.isNew(entity)) {
this.em.persist(entity);
return entity;
} else {
return this.em.merge(entity);
}
}根據(jù)源碼我們可以看出來,save是先通過判斷這個對象是不是新的,新的便會新增,否則就是執(zhí)行的修改。整個是有分兩步進行的,先查詢再新增
saveAll()方法
@Transactional
public <S extends T> List<S> saveAll(Iterable<S> entities) {
Assert.notNull(entities, "The given Iterable of entities not be null!");
List<S> result = new ArrayList();
Iterator var3 = entities.iterator();
while(var3.hasNext()) {
S entity = var3.next();
result.add(this.save(entity));
}
return result;
}saveAll()方法是一種更新多條的一種方式,里面?zhèn)鞯拇鎸ο蟮募?。分析源碼我們可以看出saveAll()底層還是調(diào)用的save()方法,也就是每次需要先查詢再做修改。在使用上方便,但是因每次涉及到查詢、新增,事務(wù)的關(guān)系,導(dǎo)致修改或者新增耗時得非常的久。
那么下面我們將結(jié)合EntityManager對批量新增,修改做出優(yōu)化。
jpa結(jié)合Batch
配置文件
spring:
#配置 Jpa
jpa:
properties:
hibernate:
generate_statistics: false
order_insert: true //配置批量新增
order_update: true //配置批量修改
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
jdbc:
batch_size: 1000 //容器內(nèi)批量的大小
open-in-view: trueEntityManager
EntityManager其實就是一個容器,通過注解引入EntityManager對象 使用EntityManager對新增,修改進行批量處理
/**
* @author 程序員panda
* @date
* @desc 批量處理
*/
@Service
@Transactional
public class BatchService {
@PersistenceContext
private EntityManager entityManager;
//配置文件中每次批量提交的數(shù)量
@Value("${spring.jpa.properties.hibernate.jdbc.batch_size}")
private long batchSize;
/**
* 批量插入
*
* @param list 實體類集合
* @param <T> 表對應(yīng)的實體類
*/
public <T> void batchInsert(List<T> list) {
if (!ObjectUtils.isEmpty(list)){
for (int i = 0; i < list.size(); i++) {
entityManager.persist(list.get(i));
if (i % batchSize == 0) {
entityManager.flush();
entityManager.clear();
}
}
entityManager.flush();
entityManager.clear();
}
}
/**
* 批量更新
*
* @param list 實體類集合
* @param <T> 表對應(yīng)的實體類
*/
public <T> void batchUpdate(List<T> list) {
if (!ObjectUtils.isEmpty(list)){
for (int i = 0; i < list.size(); i++) {
entityManager.merge(list.get(i));
if (i % batchSize == 0) {
entityManager.flush();
entityManager.clear();
}
}
entityManager.flush();
entityManager.clear();
}
}
}實際運用
選擇一個需要新增的table,將原有的saveAll(),改為調(diào)用自定義的batchInsert()方法
public JSONObject sumbitPhone(MultipartFile phoneFile) {
long timeIdStart = System.currentTimeMillis();
JSONObject result=new JSONObject();
List<CheckPhone> phoneList=getPhoneListEn(phoneFile);
batchService.batchInsert(phoneList);
result.put("code",200);
result.put("msg","提交成功");
logger.info("提交預(yù)校驗數(shù)據(jù)用時:"+(System.currentTimeMillis() - timeIdStart) / 1000.0+"秒");
return result;
}注意
使用batch批量新增的時候,表的主鍵不能使用自增的形式,需要使用uuid來才能使用批量的新增
運行時間對比
使用saveall()方法耗時(提交1000個號碼文件) 從sql監(jiān)控中可以看到,新增1000個號碼,執(zhí)行了7.962秒,執(zhí)行了1000次的事務(wù),都是一個個提交的
使用batch批量新增(同為1000個號碼)此時執(zhí)行,提交了兩次,執(zhí)行了兩次事務(wù)。因為我設(shè)置了batch_size為1000。此時用是196毫秒??匆豢闯鏊前?99條數(shù)據(jù),然后統(tǒng)一提交的
總結(jié)
到此這篇關(guān)于使用SpringBoot和JPA實現(xiàn)批量處理新增、修改的文章就介紹到這了,更多相關(guān)SpringBoot JPA批量處理新增修改內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
5分鐘快速學會spring boot整合Mybatis的方法
這篇文章主要給大家介紹了如何通過5分鐘快速學會spring boot整合Mybatis的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12
利用SpringMVC和Ajax實現(xiàn)文件上傳功能
這篇文章主要為大家詳細介紹了利用SpringMVC和Ajax實現(xiàn)文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08
一篇文章帶你搞定 springsecurity基于數(shù)據(jù)庫的認證(springsecurity整合mybatis)
這篇文章主要介紹了一篇文章帶你搞定 springsecurity基于數(shù)據(jù)庫的認證(springsecurity整合mybatis),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10
Spring?MVC啟動之HandlerMapping作用及實現(xiàn)詳解
這篇文章主要為大家介紹了Spring?MVC啟動之HandlerMapping作用及實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03

