You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Pageable实现传递全部查询对象而非固定6条数据

解决方案

你当前代码限制仅返回6条数据的核心原因是PageRequest.of(pageable.getPageNumber(), 6)的第二个参数(分页大小)被硬编码为6,要返回所有符合条件的记录,可根据业务场景选择以下两种方案:

方案1:调整分页参数拉取全量(适配性高,无需修改Repository层)

如果符合条件的数据总量不大、无内存溢出风险,可以直接将分页大小设置为足够大的取值,拉取全量数据:

StudentDTO students = StudentDTO.builder()
            .fieldName("studentCity")
            .postDTOS(postRepository.findStudentByCity(
                    // 页码设为0+分页大小设为Integer.MAX_VALUE即可拉取全量符合条件的数据
                    PageRequest.of(0, Integer.MAX_VALUE))
                    .map(postMapper::toPostDTO).toSet()).build();

方案2:新增不分页的Repository查询方法(推荐,性能更优)

如果不需要分页能力,直接在Repository层新增无需Pageable参数的查询方法,避免分页逻辑带来的额外性能开销:

  1. 首先在PostRepository接口中新增查询方法:
// 无查询条件的写法
Set<Post> findStudentByCity();
// 如果需要按城市筛选,增加对应参数即可
// Set<Post> findStudentByCity(String city);
  1. 修改业务层代码调用新的查询方法:
StudentDTO students = StudentDTO.builder()
            .fieldName("studentCity")
            .postDTOS(postRepository.findStudentByCity()
                    .stream()
                    .map(postMapper::toPostDTO)
                    .collect(Collectors.toSet())).build();

注意事项:

  • 若符合条件的总数据量超过1万条,不建议直接拉取全量,存在OOM风险,建议保留分页逻辑分批处理
  • 若原有findStudentByCity方法必须保留Pageable入参,优先选择方案1

内容的提问来源于stack exchange,提问作者Jack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 14:57:00