Spring数据mongo分页 [英] Spring data mongo pagination

查看:27
本文介绍了Spring数据mongo分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

StoryRepo extends PagingAndSortingRepository<Story, String>{}

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

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

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

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

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?

推荐答案

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

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

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

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

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

Then in Spring, enable pagination using this:

@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);

这篇关于Spring数据mongo分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆