news 2026/5/9 18:07:45

分批导出大文件:JPA Slice 使用解决page查询做导出时无效count问题

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
分批导出大文件:JPA Slice 使用解决page查询做导出时无效count问题

1.要重写底层readPage方法,删除掉count的部分

package com.konka.api.bms.repository; import com.konka.api.voucher.domain.SnView; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.support.JpaEntityInformation; import org.springframework.data.jpa.repository.support.SimpleJpaRepository; import org.springframework.stereotype.Repository; import javax.persistence.EntityManager; import javax.persistence.TypedQuery; import java.io.Serializable; import java.util.List; public class CustomNotCountRepository<T,ID extends Serializable> extends SimpleJpaRepository<T,ID> { public CustomNotCountRepository(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) { super(entityInformation, entityManager); } public CustomNotCountRepository(Class<T> domainClass, EntityManager em) { super(domainClass, em); } @Override protected <S extends T> Page<S> readPage(TypedQuery<S> query, Class<S> domainClass, Pageable pageable, Specification<S> spec) { query.setFirstResult((int)pageable.getOffset()); query.setMaxResults(pageable.getPageSize()); List<S> content = query.getResultList(); return new PageImpl<>(content, pageable, content.size()); } }

2.封装slice查询方法

package com.konka.api.bms.repository; import com.konka.api.voucher.domain.SnView; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Repository; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @Repository public class HelperRepository<T>{ @PersistenceContext private EntityManager em; public <T> Slice<T> findAll(Specification<T> specification, Pageable pageable, Class<T> domainClass){ CustomNotCountRepository customNotCountRepository = new CustomNotCountRepository<>(domainClass, em); return customNotCountRepository.findAll(specification, pageable); } }

3.调用查询

spec = getSpec(searchData); pageable = Pageutil.getPageRequest(searchData); all = this.helperRepository.findAll(spec, pageable, SnView.class);

4.分批导出

@Async @Transactional(readOnly = true) public void pageForExportV2(PageParam<SnViewDTO> searchData, Authentication currentAuth) { SecurityUtils.buildAuthentication(currentAuth); Specification<SnView> spec; String entityName = "条码库存"; DownloadTaskCenterDTO taskCenterDTO = commonFeignService.saveDownloadTaskSimple("条码库存", entityName, entityName); Class<SnViewDTO> clazz = SnViewDTO.class; String fileName = entityName + "-" + DateUtil.umsNow() + ExcelTypeEnum.XLSX.getValue(); Path f = Paths.get(FileUtils.getCachePath(), fileName); ExcelWriter excelWriter = EasyExcel.write(f.toString(), clazz) .registerConverter(new LocalDateConverter()) .registerConverter(new LocalDateTimeConverter()) .registerConverter(new BooleanConverter()) // 注意:移除入参中的excelTemps,避免全量加载 .build(); WriteSheet sheet1 = EasyExcel.writerSheet("sheet1").build(); // 一次 EXPORT_BATCH_SIZE 条 searchData.setPageSize(EXPORT_BATCH_SIZE); int page = 1; Pageable pageable; Slice<SnView> all; try{ while (true) { spec = getSpec(searchData); pageable = Pageutil.getPageRequest(searchData); all = this.helperRepository.findAll(spec, pageable, SnView.class); if (!CollectionUtils.isEmpty(all.getContent())){ excelWriter.write(all.getContent(), sheet1); }else { break; } // 终止循环条件:当前批次无数据(说明已查询完所有数据) if (all.getContent().size() < EXPORT_BATCH_SIZE) { break; } searchData.setPageIndex(++page); } if (excelWriter != null) { excelWriter.finish(); log.info("Excel文件写入完成,已生成完整文件"); } FileInfoDTO fileInfo = commonFeignService.uploadFile(entityName, f); commonFeignService.updateDownloadTaskCenter(fileInfo.getId(), taskCenterDTO.getId(), null); }catch (Exception e){ // 全局异常捕获:所有步骤的异常均在此处理,更新任务为失败 log.error("条码库存导出失败,任务ID:{}", e.getMessage()); if (Objects.nonNull(taskCenterDTO)) { taskCenterDTO.setTaskArgs(e.getMessage()); commonFeignService.updateDownloadMsg(taskCenterDTO); } }finally { // ########################################################### // 关键修复2:finally块做**双重兜底**+资源释放,确保无遗漏 // ########################################################### try { // 兜底:若未主动执行finish,在此执行(防止异常导致主动finish未执行) if (excelWriter != null) { excelWriter.finish(); } } catch (Exception e) { log.error("ExcelWriter关闭失败", e); } try { // 删除临时Excel文件:无论成功/失败,都删除,避免临时文件堆积 if (f != null && Files.exists(f)) { Files.deleteIfExists(f); log.info("临时Excel文件已删除:{}", f); } } catch (IOException e) { log.error("删除临时Excel文件失败,文件路径:{}", f, e); } } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/9 9:50:25

实测对比后!千笔,最受欢迎的AI论文写作软件

你是否曾为论文选题发愁&#xff1f;是否在深夜面对空白文档无从下笔&#xff1f;是否反复修改却总觉得表达不够专业&#xff1f;论文写作不仅是对知识的考验&#xff0c;更是对耐心和效率的挑战。对于无数本科生来说&#xff0c;从开题到定稿&#xff0c;每一步都充满压力。而…

作者头像 李华
网站建设 2026/5/9 17:21:20

MySQL之InnoDB单表推荐2000W记录缘由

MySQL之InnoDB单表推荐2000W记录缘由 一、概述 在MySQL数据库设计实践中&#xff0c;业界普遍推荐InnoDB单表记录数控制在2000万&#xff08;20M&#xff09;左右。这个数值并非MySQL的硬性限制&#xff0c;而是基于性能、维护成本和架构设计等多个维度综合考量的经验值。本文将…

作者头像 李华
网站建设 2026/4/23 15:20:05

lvgl v8之label使用示例

void lvgl_test() {lv_obj_t* label1 = lv_label_create(lv_scr_act());

作者头像 李华
网站建设 2026/5/9 10:51:57

导师严选! AI论文软件 千笔ai写作 VS 文途AI,继续教育写作者必备!

随着人工智能技术的迅猛迭代与普及&#xff0c;AI辅助写作工具已逐步渗透到高校学术写作场景中&#xff0c;成为专科生、本科生、研究生完成毕业论文不可或缺的辅助手段。越来越多面临毕业论文压力的学生&#xff0c;开始依赖各类AI工具简化写作流程、提升创作效率。但与此同时…

作者头像 李华
网站建设 2026/5/9 12:17:38

智能温度计检测控制系统设计

智能温度计检测控制系统设计 第一章 系统设计目标与核心要求 智能温度计检测控制系统旨在突破传统温度计单一测量功能的局限&#xff0c;实现温度的精准采集、智能分析、异常控制与数据交互&#xff0c;适用于实验室、工业车间、智能家居等多场景温度管理。系统核心设计目标包括…

作者头像 李华