Spring Data Mongo分页 [英] Spring data mongo pagination

查看:83
本文介绍了Spring Data 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 Data Mongo分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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