mongodb Spring数据mongo分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20366666/
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-09-09 13:31:42  来源:igfitidea点击:

Spring data mongo pagination

mongodbpaginationspring-data

提问by phuong

I want to implement pagination with Spring Data Mongo. There are many tutorials and docs suggest to use PagingAndSortingRepository, like this:

我想用 Spring Data Mongo 实现分页。有很多教程和文档建议使用 PagingAndSortingRepository,如下所示:

StoryRepo extends PagingAndSortingRepository<Story, String>{}

And so because PagingAndSortingRepository provides api for query with paging, I can use it like:

因此,因为 PagingAndSortingRepository 提供了用于分页查询的 api,我可以像这样使用它:

Page<Story> story = storyRepo.findAll(pageable);

My question is where actually is this findAll method here implemented? Do I need to write its implementation by myself? The StoryRepoImpl which implements StoryRepo needs to implement this method?

我的问题是这里的 findAll 方法实际上是在哪里实现的?我需要自己编写它的实现吗?实现StoryRepo的StoryRepoImpl需要实现这个方法吗?

回答by Simon

You do not need to implement the method as when you autowired the Spring object PagingAndSortingRepository, it automatically implements the method for you.

您不需要实现该方法,因为当您自动装配 Spring 对象 PagingAndSortingRepository 时,它会自动为您实现该方法。

Please note that since you are using Mongodb, you can extend MongoRepository instead.

请注意,由于您使用的是 Mongodb,因此您可以扩展 MongoRepository。

Then in Spring, enable pagination using this:

然后在 Spring 中,使用以下方法启用分页:

@RequestMapping(value="INSERT YOUR LINK", method=RequestMethod.GET)
  public List<Profile> getAll(int page) {
    Pageable pageable = new PageRequest(page, 5); //get 5 profiles on a page
    Page<Profile> page = repo.findAll(pageable);
    return Lists.newArrayList(page);

回答by phuong

I got it working by writing my own implementations, something like this:

我通过编写自己的实现来让它工作,如下所示:

List<Story> stories = null;

Query query = new Query();
query.with(pageable);

stories = getTemplate().find(query, Story.class);

long total = getTemplate().count(query, Story.class);
Page<Story> storyPage = new PageImpl<Story>(stories, pageable, total);

return storyPage;

I'm working with spring data & mongodb, using mongo template to query data.

我正在使用 spring 数据和 mongodb,使用 mongo 模板查询数据。

回答by Iqbal

To paginate a query, you can use something like below:

要对查询进行分页,您可以使用以下内容:

public interface PersonRepository extends MongoRepository<Person, String> {
    Page<Person> findByFirstname(String firstname, Pageable pageable);
}

For more details, please refer to the second query in Example 6.6 in https://docs.spring.io/spring-data/mongodb/docs/1.2.0.RELEASE/reference/html/mongo.repositories.html

更多细节请参考https://docs.spring.io/spring-data/mongodb/docs/1.2.0.RELEASE/reference/html/mongo.repositories.html中Example 6.6中的第二个查询

回答by Oliver Drotbohm

The method is implemented by a store-specific class. For the Spring Data JPA module, it's SimpleJpaRepository. You usually let a DI container create instances for these repository interfaces. With Spring you'd activate Spring Data repositories by either using @EnableJpaRepositoryon a JavaConfig class or

该方法由特定于商店的类实现。对于 Spring Data JPA 模块,它是SimpleJpaRepository. 您通常让 DI 容器为这些存储库接口创建实例。使用 Spring,您可以通过使用@EnableJpaRepositoryJavaConfig 类或

<jpa:repositories base-package="com.acme.repositories" />

This will create a proxy instance for the repo, so that you can get it injected into your clients:

这将为 repo 创建一个代理实例,以便您可以将其注入您的客户端:

class MyClient {

  @Inject
  public MyClient(PersonRepository repository) {
    …
  }
}

回答by Indian

Query query1 = new Query();
Integer startIndex = page * size;
Integer endIndex = (page * size) + size;

List<dto> totalRecord = mongoOperation.find(query1, dto.class);

query1.limit((endIndex > totalRecord.size() ? totalRecord.size() : endIndex));

List<dto> responseList = mongoOperation.find(query1, dto.class);

int end = (endIndex > (totalRecord.size() - 1) ? totalRecord.size() - 1 : endIndex);
if (totalRecord.size() > 0 && end == 0)
  end = 1;

if (totalRecord.size() > 0)
  responseList = responseList.subList(startIndex, end);

int totalPages = totalRecord.size() / size + (totalRecord.size() % size == 0 ? 0 : 1);