Pagination in Spring Data JPA (limit and offset)

后端 未结 7 844
眼角桃花
眼角桃花 2020-12-02 08:54

I want the user to be able to specify the limit (the size of the amount returned) and offset (the first record returned / index returned) in my query method.

Here a

相关标签:
7条回答
  • 2020-12-02 09:32

    You can do that by creating your own Pageable.

    Try out this basic sample. Works fine for me:

    public class ChunkRequest implements Pageable {
    
    private int limit = 0;
    private int offset = 0;
    
    public ChunkRequest(int skip, int offset) {
        if (skip < 0)
            throw new IllegalArgumentException("Skip must not be less than zero!");
    
        if (offset < 0)
            throw new IllegalArgumentException("Offset must not be less than zero!");
    
        this.limit = offset;
        this.offset = skip;
    }
    
    @Override
    public int getPageNumber() {
        return 0;
    }
    
    @Override
    public int getPageSize() {
        return limit;
    }
    
    @Override
    public int getOffset() {
        return offset;
    }
    
    @Override
    public Sort getSort() {
        return null;
    }
    
    @Override
    public Pageable next() {
        return null;
    }
    
    @Override
    public Pageable previousOrFirst() {
        return this;
    }
    
    @Override
    public Pageable first() {
        return this;
    }
    
    @Override
    public boolean hasPrevious() {
        return false;
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题