使用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()方法,也就是每次需要先查詢再做修改。在使用上方便,但是因每次涉及到查詢、新增,事務的關系,導致修改或者新增耗時得非常的久。
那么下面我們將結合EntityManager對批量新增,修改做出優(yōu)化。
jpa結合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: true
EntityManager
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> 表對應的實體類 */ 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> 表對應的實體類 */ 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("提交預校驗數(shù)據(jù)用時:"+(System.currentTimeMillis() - timeIdStart) / 1000.0+"秒"); return result; }
注意
使用batch批量新增的時候,表的主鍵不能使用自增的形式,需要使用uuid來才能使用批量的新增
運行時間對比
使用saveall()方法耗時(提交1000個號碼文件) 從sql監(jiān)控中可以看到,新增1000個號碼,執(zhí)行了7.962秒,執(zhí)行了1000次的事務,都是一個個提交的
使用batch批量新增(同為1000個號碼)此時執(zhí)行,提交了兩次,執(zhí)行了兩次事務。因為我設置了batch_size為1000。此時用是196毫秒。看一看出他是按999條數(shù)據(jù),然后統(tǒng)一提交的
總結
到此這篇關于使用SpringBoot和JPA實現(xiàn)批量處理新增、修改的文章就介紹到這了,更多相關SpringBoot JPA批量處理新增修改內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
探索分析Redis?AOF日志與數(shù)據(jù)持久性
這篇文章主要為大家介紹了探索分析Redis?AOF日志與數(shù)據(jù)持久性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12Springboot FeignClient調(diào)用Method has too m
本文主要介紹了Springboot FeignClient微服務間調(diào)用Method has too many Body parameters 解決,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12SpringBoot3整合Hutool-captcha實現(xiàn)圖形驗證碼
在整合技術框架的時候,想找一個圖形驗證碼相關的框架,看到很多驗證碼的maven庫不再更新了或中央倉庫下載不下來,還需要多引入依賴,后面看到了Hutool圖形驗證碼(Hutool-captcha)中對驗證碼的實現(xiàn),所以本文介紹了SpringBoot3整合Hutool-captcha實現(xiàn)圖形驗證碼2024-11-11