How can I choose ideal way for pagination in Spring boot?
How can I choose ideal way for pagination in Spring boot?
I need your advice for paginations.
My table data is very long.Therefor I use pagination and every pag action İ call data from database
I have a rest method in spring-boot application.
This method could get data from database and everthing is ok.
I need pag count(all count of my data/ perPage count).
I can find this count from db.
I must write two seperate method for allCount and the below method?
How can I use ideal way for pagination in Spring boot?
@CrossOrigin(maxAge = 3600)
@RequestMapping(value = "/payment/all", method = RequestMethod.POST)
public List<NotaryPaymentInformationEntity> getAllProduct(@RequestBody PaginationModel pag){
try {
System.out.println(pag.getPageno()+ " " + pag.getRecords());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Pageable pageable = new PageRequest(pag.getPageno(), pag.getRecords());
System.out.println("here"+notaryPaymentRepository.findAll(pageable).getContent().size());
return notaryPaymentRepository.findAll(pageable).getContent();
}
public interface NotaryPaymentRepository extends JpaRepository<NotaryPaymentInformationEntity,Integer>{
}
2 Answers
2
You can get the response of your query in Page class obj which extends the Slice class and has all the methods you would need.
Pageable pageable = new PageRequest(pageNo, PAGE_SIZE);
The pageNo and PAGE_SIZE needs to be sent from the client
@Query(value = "SELECT h FROM {NameOfYourModelClass} h")
Page<HorseWatch> getPaginatedModel(Pageable pageable);
Call this method from your service class and retrieve it in a Page object
You can get the page content, page number and total number of pages using (using getContent(),getNumber(),getTotalPages() from the object )
Save these values in your response class and send this class back to the client
public class PageResponse {
private List<{NameOfModelClass}> content;
private int currentPage;
private int totalPages;
}
You need pagination support then add the parameter Pageable pageable
Pageable pageable
@CrossOrigin(maxAge = 3600)
@RequestMapping(value = "/payment/all", method = RequestMethod.POST)
public List<NotaryPaymentInformationEntity> getAllProduct(Pageable pageable){
try {
System.out.println(pag.getPageno()+ " " + pag.getRecords());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//NO pageable object need to be manually constructed
return notaryPaymentRepository.findAll(pageable).getContent();
}
and URL can now have extra query params like /payment/all?page=2&size=10
If you use Spring data REST, then you will have HATEOAS support which will give you more information about how many total counts are there and what page you are in etc., But the response would be of HAL format.
Reference: https://docs.spring.io/spring-data/rest/docs/current/reference/html/#paging-and-sorting
I send http post to /payment/all. I can ctach limit and offset and get data from database for this items. I want to get additional count(all of them row from table). And I think isn't good way create another method wich this method will return totalCount of all my data.
– MehmanBashirov
Jun 29 at 9:22
@RequestBody PaginationModel page. this object can give me offset and limit.
– MehmanBashirov
Jun 29 at 9:24
page and size are substitues for your paginationmodel object. You wouldn't need it anymore. But for total count, you need to write query of your own and update it the response. No other way. Else you can use Spring data REST. see reference in my answer.
– Karthik R
Jun 29 at 9:34
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
great.I changed my method signature as the below.public Page<NotaryPaymentInformationEntity> getAllProduct(@RequestBody PaginationModel pag) - And I get all of them informations.
– MehmanBashirov
Jun 29 at 10:19