Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring data mongo pagination

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>{}

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

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?

like image 824
phuong Avatar asked Dec 04 '13 04:12

phuong


1 Answers

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

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

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);
like image 87
Simon Avatar answered Oct 05 '22 10:10

Simon