Paging with Spring Framework

Asked

Viewed 3,824 times

1

I have a question, I need to make a pagination when I return all the records of a table, I don’t have much experience with Spring and I would like to know how it is done, I need to overwrite findAll()? Or is it possible to do it in the controller’s own way? Thanks in advance.

  • You are using Spring Data?

2 answers

2


If you are using Spring Data you can create a interface that extends the interface Pagingandsortingrepository containing additional methods for paging.

The method findAll receives an object of the type Pageable, which contains the criteria for paging, such as number of items, current page, which field you want to sort and whether the ordering is ascending or descending.

Take an example:

Interface: interface The Long parameter is the User Id type

Controller: controller

The tamanho is the number of items you should have on the page. The method findAll returns an object of type Page which contains the information on the current page: Content, Quantity, Total Elements, Number of Pages. You can check on documentation what is returned by the method

1

From what you said it seems to me that you use Spring Data, so Spring itself provides another interface that you can extend when you are declaring your repository, this being the Pagingandsortingrepository, with this your findAll receives a Pageable type object and the return of the findAll method automatically comes paginated, I’ll give you some reference links, anything I’m here and may ask.

Doc interface.
http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/PagingAndSortingRepository.html

Doc how to use.
http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.core-Concepts

  • While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed. - From Review

  • Hello Tiago, I’m starting now at stackoverflow, but I’ll do it from now on, thank you very much!

  • Hi Leonardo, welcome to the site. You can also [Dit] your posts anytime and improve them!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.