Now that we have a list of thousand customers, quite often, we don't want all the customers in the database. Because what if I have a million records of customers? Now, you don't want to send all million records at one shot, but you may want to send a chunk of them. For example, when you search anything in Google, Google may give you probably 20, or 50 records at the first page. And then if you want, you can go to the next page, and so on and so forth. Somewhere, I should be able to say that, hey, give me the page number one, something like this by typing question mark, underscore page equals to one.
So this way, I should get only the first page record, where the first page may consist of 10 records or 20 Records, something that we can assume as default. So to do that, I can go back over here to eclipse and then change our customer repository. Instead of extending credit repository. We can extend a subtype of crud repository called paging and sorting repository. So let's go add that here. Paging and sorting repository.
Save this. I have all the functions. But in addition, we also have a couple of extra features. So if I go back over here, refresh here, I still see my thousand records. Of course, now I have a way of processing this page number one. Now, the underscore page is a convention that you say that the page number is one.
Similarly, if you want to say the size of the pages 20. For example, you may say ampersand underscore limit equals to 20. So these are not any keywords. It's your choice. If you want to change this from underscore page to a page size on p, L, etc. It's totally up to you.
I'm going to stick with underscore page underscore limit for two parameters. Now, if I don't mention any one of them, it should be assumed. For example, if I don't mention any parameter, As at all, it should be assumed that I'm looking for page number one. So to do this, let's go back to our code. In our rest controller, we're going to say here, something like this. So we're going to add a couple of variables, let's say integer, page num.
And then I'll say integer page size. If page num and page size have been supplied, I can supply those two information over here. Now, if you look at the Find all parameters, there is a salt object that I can supply. There is an iterable IDs I can supply and there is also patchable. I can supply so payable is an interface that has couple of options. So let's see how we can create that.
So I'm going to say here, payable, P equals two, which means information about the page abilities such as Page number and page size is going to come from page request dot off, I can supply the page num and the size. So I'm going to say here, page num, and then it's a page size. And this page table needs to be imported from spring framework. So let's do that. And then I can say find all of p. So, we do have now a page table option, but only one problem is that we did not tell spring where to look for values for this page number and page size. Now, if you remember, we used or we thought of using underscore page underscore limit.
So to do that, we may have to say here something like at the rate request parameter. And the parameter name happens to be underscore page. And then I can say if it is not supplied, default value is One. And that's what actually we have to save for page num. And then similarly, I can give here also an option, saying that it is supposed to be underscore limit. And the default value happens to be 10.
So in case if you don't supply the limit, we'll assume that it's going to be default. Now one small thing here is that for us, the page number one means that we want to start from the record number one, but the page request treats it as page number two, so because the index of the page start from zero, so we may want to specify here that page num minus one, because we want to say when you want page number one, we want to say your zero. So as soon as we're done with this, let's save it. Let's go back here and not specify any additional parameters and then refresh. I should see a smaller chunk here now and you can see that it start from page number one. So hence, ID number one to the last One, which would be the ID number 10.
But we also get a lot of additional information. But I'm only interested in the array of data. So if that is the case, you might have to say here, find all dot get content. And that gives me a list of customers only. So when I go back here, now when I refresh that, you can see that we get a plain and simple array of data from one to 10. If you want the second page, you can always say question mark underscore page number equals to two.
And you can see we get it from 11 to 20, and so on and so forth. So if I say page number 33, we got 321 onwards up to 330. That's how we are getting the results. Of course, we can also check that hey, per page, I want 20 Records not 10. So I can say ampersand underscore limit equals to 20 or 25, whatever that you feel right. Now you can see I got 20 Records starting from 630 One