Java 使用 mongoTemplate 进行分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29030542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:13:44  来源:igfitidea点击:

Pagination with mongoTemplate

javaspring-dataspring-data-mongodb

提问by Marcin Wisnicki

I have a Query with Pageable:

我有一个带有 Pageable 的查询:

Query query = new Query().with(new PageRequests(page, size))

How can I execute it with MongoTemplate ? I don't see a single method returning Page<T>.

如何使用 MongoTemplate 执行它?我没有看到一个方法返回Page<T>

采纳答案by Ori Dar

MongoTemplatedoes not have methods to return Page. The find()methods return an ordinary List.

MongoTemplate没有方法返回Page。这些find()方法返回一个普通的List.

with(new PageRequests(page, size)is used internally to adjust skipand limitwith a MongoDB query (proceeded by a count query I think)

with(new PageRequests(page, size)在内部用于调整skiplimit使用 MongoDB 查询(我认为由计数查询进行)

Pagecan be used in conjunction with MongoDB repositorieswhich is a specialized case of Spring data repositories.

Page可以与MongoDB 存储库结合使用,MongoDB 存储库是 Spring 数据存储库的一个特例。

Thus, you'll have to use MongoRepository's Page findAll(Pageable pageable)for paginated results (actually inherited from PagingAndSortingRepository).

因此,您必须将MongoRepository'sPage findAll(Pageable pageable)用于分页结果(实际上是从 继承的PagingAndSortingRepository)。

回答by d0x

It's true that the MongoTemplatedoesn't have findXXXwith Pageables.

的确,PageablesMongoTemplate没有findXXX

But you can use the Spring Repository PageableExecutionUtilsfor that.

但是您可以PageableExecutionUtils为此使用 Spring Repository 。

In your example it would look like this:

在您的示例中,它看起来像这样:

Pageable pageable = new PageRequests(page, size);
Query query = new Query().with(pageable);
List<XXX> list = mongoTemplate.find(query, XXX.class);
return PageableExecutionUtils.getPage(
                       list, 
                       pageable, 
                       () -> mongoTemplate.count(Query.of(query).limit(-1).skip(-1), XXX.class));

Like in the original Spring Data Repository, the PageableExecutionUtilswill do a count request and wrap it into a nice Pagefor you.

就像在最初的 Spring Data Repository 中一样,它PageableExecutionUtils会做一个计数请求并将它包装成一个Page对你来说很好的。

Hereyou can see that spring is doing the same.

在这里你可以看到 spring 也在做同样的事情。

回答by Razzlero

Based on d0x's answer and looking at the spring code. I'm using this variation which works off the spring-boot-starter-data-mongodb dependency without needing to add spring data commons.

基于 d0x 的回答并查看spring 代码。我正在使用这种变体,它可以消除 spring-boot-starter-data-mongodb 依赖项,而无需添加 spring 数据公共。

@Autowired
private MongoOperations mongoOperations;

@Override
public Page<YourObjectType> searchCustom(Pageable pageable) {
    Query query = new Query().with(pageable);
    // Build your query here

    List<YourObjectType> list = mongoOperations.find(query, YourObjectType.class);
    long count = mongoOperations.count(query, YourObjectType.class);
    Page<YourObjectType> resultPage = new PageImpl<YourObjectType>(list , pageable, count);
    return resultPage;
}

回答by velastiqui

return type Mono<Page<Myobject>>...

return this.myobjectRepository.count()
        .flatMap(ptiCount -> {
          return this.myobjectRepository.findAll(pageable.getSort())
            .buffer(pageable.getPageSize(),(pageable.getPageNumber() + 1))
            .elementAt(pageable.getPageNumber(), new ArrayList<>())
            .map(ptis -> new PageImpl<Myobject>(ptis, pageable, ptiCount));
        });