The Pageable
is interface defined in Spring Data JPA. Pageable
is an abstract interface which provides pagination information.
first
The Pageable#first
Returns the Pageable
requesting the first page.
next
The Pageable#next
Returns the Pageable
requesting the next page.
getOffset
The Pageable#getOffset
returns the offset to be taken according to the underlying page and page size.
previousOrFirst
The Pageable#previousOrFirst
returns the previous Pageable
link or the first Pageable
link if the current one already in the first one.
isPaged
The Pageable#isPaged
returns whether the current Pageable
contains pagination information.
isUnpaged
The Pageable#isUnpaged
returns whether the current Pageable
does not contain pagination information.
hasPrevious
The Pageable#hasPrevious
returns whether there's a previous Pageable
we can access from the current one. Will return false
in case the current Pageable
already refers to the first page.
getPageNumber
The Pageable#getPageNumber
returns the page to be returned.
getPageSize
The Pageable#getPageSize
returns the number of items to be returned.
getSort
The Pageable#getSort
returns the sorting parameters.
getSortOr
The Pageable#getSortOr
returns the current Sort
direction, if the current one is unsorted then will return the provided.
toOptional
The Pageable#toOptional
returns an Optional so that it can easily be mapped on.
schema.sql
drop database if exists AppleDb;
create database AppleDb;
use AppleDb;
CREATE TABLE apple ( apple_id BIGINT NOT NULL, apple_name VARCHAR(255) DEFAULT NULL, PRIMARY KEY (apple_id)) ENGINE=INNODB;
CREATE TABLE hibernate_sequence ( next_val BIGINT) ENGINE=INNODB;
data.sql
insert into hibernate_sequence values ( 1 );
insert into apple (apple_id, apple_name ) values (1, "Macintosh");
insert into apple (apple_id, apple_name ) values (2, "Fuji");
insert into apple (apple_id, apple_name ) values (3, "Gala");
insert into apple (apple_id, apple_name ) values (4, "Jonagold");
insert into apple (apple_id, apple_name ) values (5, "GrannySmith");
insert into apple (apple_id, apple_name ) values (6, "PinkLady");
insert into apple (apple_id, apple_name ) values (7, "Golden Delicious");
insert into apple (apple_id, apple_name ) values (8, "Fireside" );
insert into apple (apple_id, apple_name ) values (9, "Jazz" );
insert into apple (apple_id, apple_name ) values (10, "Mutsu" );
insert into apple (apple_id, apple_name ) values (11, "Pinova" );
package com.sujan.example.jpa.controller;
import com.sujan.example.jpa.entity.Apple;
import com.sujan.example.jpa.repository.AppleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class AppleController {
@Autowired
private AppleRepository appleRepository;
@GetMapping
List<Apple> PageRequestOf() {
Pageable page = PageRequest.of(2, 3, Sort.Direction.DESC, new String[]{"appleName"});
Page<Apple> apples = appleRepository.findAll(page);
System.out.println("first:- " + page.first());
System.out.println("next:- " + page.next());
System.out.println("getOffset:- " + page.getOffset());
System.out.println("previousOrFirst:- " + page.previousOrFirst());
System.out.println("isPaged:- " + page.isPaged());
System.out.println("isUnpaged:- " + page.isUnpaged());
System.out.println("hasPrevious:- " + page.hasPrevious());
System.out.println("getPageNumber:- " + page.getPageNumber());
System.out.println("getPageSize:- " + page.getPageSize());
System.out.println("getSort:- " + page.getSort());
System.out.println("getSortOr:- " + page.getSortOr(Sort.by("apple")));
System.out.println("toOptional:- " + page.toOptional());
apples.forEach(System.out::println);
return apples.getContent();
}
}
package com.sujan.example.jpa.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Data
@Entity
@NoArgsConstructor
public class Apple {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long appleId;
private String appleName;
}
package com.sujan.example.jpa.repository;
import com.sujan.example.jpa.entity.Apple;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface AppleRepository extends JpaRepository<Apple, Long> {
}
package com.sujan.example.jpa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JpaApplication {
public static void main(String[] args) {
SpringApplication.run(JpaApplication.class, args);
}
}
spring.datasource.url=jdbc:mysql://localhost:3306/AppleDb
spring.datasource.username=root
spring.datasource.password=root
plugins {
id 'org.springframework.boot' version '2.3.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'com.sujan'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
annotationProcessor 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
curl --location --request GET 'http://localhost:8080/'
Hibernate: select apple0_.apple_id as apple_id1_0_, apple0_.apple_name as apple_na2_0_ from apple apple0_ order by apple0_.apple_name desc limit ?, ?
Hibernate: select count(apple0_.apple_id) as col_0_0_ from apple apple0_
first:- Page request [number: 0, size 3, sort: appleName: DESC]
next:- Page request [number: 3, size 3, sort: appleName: DESC]
getOffset:- 6
previousOrFirst:- Page request [number: 1, size 3, sort: appleName: DESC]
isPaged:- true
isUnpaged:- false
hasPrevious:- true
getPageNumber:- 2
getPageSize:- 3
getSort:- appleName: DESC
getSortOr:- appleName: DESC
toOptional:- Optional[Page request [number: 2, size 3, sort: appleName: DESC]]
Apple(appleId=5, appleName=GrannySmith)
Apple(appleId=7, appleName=Golden Delicious)
Apple(appleId=3, appleName=Gala)