本文介绍了如何在响应式 Spring Data 中应用分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Spring Data 中,我们有 PagingAndSortingRepository,它继承自 CrudRepository.在响应式 Spring Data 中,我们只有ReactiveSortingRepository 继承自 ReactiveCrudRepository.我们如何才能以反应方式进行分页?例如,我们将来能否使用 ReactivePagingAndSortingRepository 来实现这一点?

In Spring Data, we have PagingAndSortingRepository which inherits from CrudRepository. In reactive Spring Data, we only have ReactiveSortingRepository which inherits from ReactiveCrudRepository.How could we make pagination in a reactive way ?Will we able to make this in future with ReactivePagingAndSortingRepository for instance?

推荐答案

Reactive Spring Data MongoDB 存储库不提供分页,因为它是为命令式存储库设计的.命令式分页在获取页面时需要其他详细信息.特别是:

Reactive Spring Data MongoDB repositories do not provide paging in the sense of paging how it's designed for imperative repositories. Imperative paging requires additional details while fetching a page. In particular:

  • 分页查询返回的记录数
  • 可选地,如果返回的记录数为零或与页面大小匹配,则查询产生的记录总数以计算总页数

这两个方面都不符合高效、非阻塞资源使用的概念.等到接收到所有记录(以确定第一个分页详细信息)将消除您通过反应式数据访问获得的大部分好处.此外,执行计数查询的成本相当高,并且会增加延迟,直到您能够处理数据.

Both aspects do not fit to the notion of efficient, non-blocking resource usage. Waiting until all records are received (to determine the first chunk of paging details) would remove a huge part of the benefits you get by reactive data access. Additionally, executing a count query is rather expensive, and increases the lag until you're able to process data.

您仍然可以通过将 Pageable (PageRequest) 传递给存储库查询方法来自己获取数据块:

You can still fetch chunks of data yourself by passing a Pageable (PageRequest) to repository query methods:

interface ReactivePersonRepository extends Repository<Person, Long> {

  Flux<Person> findByFirstnameOrderByLastname(String firstname, Pageable pageable);
}

Spring Data 将通过将 Pageable 转换为 LIMITOFFSET 将分页应用于查询.

Spring Data will apply pagination to the query by translating Pageable to LIMIT and OFFSET.

参考文献:

这篇关于如何在响应式 Spring Data 中应用分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 09:04