Creating Pagination in Spring Data JPA

后端 未结 2 1498
-上瘾入骨i
-上瘾入骨i 2021-01-12 20:39

I am trying to implement pagination feature in Spring Data JPA. I am referring this Blog My Controller contains following code :

 @RequestMapping(value=\"/o         


        
相关标签:
2条回答
  • 2021-01-12 21:34

    I've seen similar problem last week, but can't find it so I'll answer directly.

    Your problem is that you specify the parameters too late. Pageable works the following way: you create Pageable object with certain properties. You can at least specify:

    1. Page size,
    2. Page number,
    3. Sorting.

    So let's assume that we have:

    PageRequest p = new PageRequest(2, 20);
    

    the above passed to the query will filter the results so only results from 21th to 40th will be returned.

    You don't apply Pageable on result. You pass it with the query.

    Edit:

    Constructors of PageRequest are deprecated. Use Pageable pageable = PageRequest.of(2, 20);

    0 讨论(0)
  • 2021-01-12 21:41

    The constructors of Pageable are deprecated, use of() instead:

    Pageable pageable = PageRequest.of(0, 20);
    
    0 讨论(0)
提交回复
热议问题