Search code examples
springspring-bootcrudspring-jdbcspring-repositories

Crud Repository get from position until position


So I have a table of tags. Tag has an id and a name.

As a first step I wanted to sort all the IDs by descending order

List<Tag> findAllByOrderByIdDesc()

Next I wanted just to get first three tags and got it done by doing

List<Tag> findTop3ByOrderByIdDesc()

Now I want to get all tags in descending order from position x until position x+3 but I can't seem to find or figure out what to do here.


Solution

  • You can pass Pageable parameter.

    Example:

    List<Tag> findTop3ByOrderByIdDesc(Pageable page);

    In the Pageable parameter you need to pass page number and offset.

    Consider if you want to get values range from id 20 to 30.

    PageRequest.of(2,10); 
    

    pass this as your Pageable parameter.