我陷入了这个问题。 这是我的代码。
public Mono<Pagination<T>> getAll(Pageable pageable) {
return db.select()
.from(entityClazz)
.page(pageable)
.fetch()
.all()
.map(this::convertToDTO)
.collectList()
.map(responses -> new Pagination<>(responses, pageable.getPageNumber(), pageable.getPageSize(),
total rows in db))
;
}
这是分页类:
public final class Pagination<T> {
@JsonProperty("total")
private final long total;
@JsonProperty("page")
private final int page;
@JsonProperty("size")
private final int size;
@JsonProperty("items")
private final List<T> items;
我想使用数据库客户端获取数据库中的总行数。我是怎么做到的?
我有一个解决方案来获取总数,但它的性能很差:
total = getAll().count();
public Flux<T> getAll() {
Flux<E> entities = getRepository().findAll();
return convertToDTO(entities);
}
根据响应式规范,根本没有分页。
但是,如果要实现它,则应:
- 更改查询以应用
.limit()
和.offset()
- 使用
.count()
获取总页数,而不是从数据库中获取整个数据
参见: https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/reactive/ReactiveCrudRepository.html#count--