1
votes

In My Spring MVC application, I am using spring-data-jpa with MySql Data Base. For a data table trying to implement pagination.

Like this enter image description here

On the table there are 191 records,and 'id' is the primary key - the value of starting from 10, 11, 12 ---- 199, 200.

My each page contains suppose 10 records.

following is the code I am using to fetch only 10 records from the table.

Pageable pageable = new PageRequest(firstResult, maxResults);   
Page<User> findAll2 = repository.findAll(pageable);

For Page 1 -> firstResult = 1 and maxResults = 10 Pageable pageable = new PageRequest(1, 10);
Here I am getting 10 records, the id 20 to 29 But I am expecting from 10 to 19

For Page 2 -> firstResult = 11 and maxResults = 10 Pageable pageable = new PageRequest(11, 10);
Here I am getting 10 records, the id 120 to 129 what about other records.

Now For Page 3 ->firstResult = 21 and maxResults = 10 Pageable pageable = new PageRequest(21, 10);
Here I am NOT getting any record. enter image description here

          All values are null.Not sure what I am missing.please help me on this.
1

1 Answers

1
votes

The first number is the page not the firstResult. So you should start with

Pageable pageable = new PageRequest(0, 10);   

Then:

Pageable pageable = new PageRequest(1, 10);

And so on.

When you say

Pageable pageable = new PageRequest(21, 10);   

You're asking for page 21, which you don't have, so it returns null.