【发布时间】:2021-11-29 20:04:05
【问题描述】:
我的应用中有一个图表,需要通过分页获取数据,但问题是图表上的 X 轴是一个时间。我需要通过升序(从旧事件到新事件)对数据进行排序,首先我需要返回最后一页(倒回分页到结束)。
为此,我获取页数并将其发送到Page<T> findAll(@Nullable Specification<T> s, Pageable p);,并附上最后一个页码。
Pageable pagination = PageRequest.of(getLastPageNumber(), // count(*) where ...
filter.getPageSize(),
Sort.by("time").ascending());
// specification not empty it has 5 independent where conditions wired to user interface
repository.findAll(Specification.where(specification), pagination);
问题:
- 最新一页没有填满。如果页面上有 500 个元素,数据库中有 501 个元素,则这样的实现会返回 1 个元素。因为我们得到了残留物。
- 我们总是需要知道表格中有多少元素。从
Specification对象中具有束条件的大表(≃1_000_000 行)中选择,而不是轻操作本身,+ 包含相同where条件的count的添加查询以及目标Specification,不会对性能产生积极影响。
我能以某种方式将最后一页作为第一页吗?它类似于findTop,具有使用spring数据分页的保存能力。我不能直接使用findTop,因为如果可能的话过滤器太多了,我想继续使用基于规范的解决方案。
有什么想法吗?
【问题讨论】:
标签: hibernate spring-data-jpa spring-data