The static of
method defined in PageRequest
class is used to create a new PageRequest
. PageRequest#of
method provides multiple overloaded methods in order to provide multiple Pagination strategy. overloaded of
methods defined in in PageRequest
are.
- of(int page, int size)
- of(int page, int size, Sort sort)
- of(int page, int size, Direction direction, String... properties)
of(int page, int size)
The static of(int page, int size)
method defined in PageRequest
class is used to create a new unsorted PageRequest
. of method takes 2 input parameter, page
and size
. the size will divide the entire record in the underlying database into sets and the page will query the particular set chunk, starting from 0. Data will be unsorted using Sort#UNSORTED
.
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;
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.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(0, 3);
Page<Apple> apples = appleRepository.findAll(page);
apples.forEach(System.out::println);
return apples.getContent();
}
}
package com.sujan.example.jpa.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Data
@ToString
@Entity
@NoArgsConstructor
public class Apple {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long appleId;
private String appleName;
public Apple(String appleName) {
this.appleName = 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_ limit ?
Hibernate: select count(apple0_.apple_id) as col_0_0_ from apple apple0_
Apple(appleId=1, appleName=Macintosh)
Apple(appleId=2, appleName=Fuji)
Apple(appleId=3, appleName=Gala)
of(int page, int size, Sort sort)
The static method of(int page, int size, Sort sort)
defined in PageRequest
class is used to create a new sorted PageRequest
. of method takes 3 input parameter, page
and size
and Sort
class. the size will divide the entire record in the underlying database into sets and the page will query the particular set chunk, starting from 0, Sort
will sort the retriving records using sorting mechanism provided.
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;
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(0, 5, Sort.by("appleName"));
Page<Apple> apples = appleRepository.findAll(page);
apples.forEach(System.out::println);
return apples.getContent();
}
}
package com.sujan.example.jpa.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Data
@ToString
@Entity
@NoArgsConstructor
public class Apple {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long appleId;
private String appleName;
public Apple(String appleName) {
this.appleName = 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 asc limit ?
Hibernate: select count(apple0_.apple_id) as col_0_0_ from apple apple0_
Apple(appleId=8, appleName=Fireside)
Apple(appleId=2, appleName=Fuji)
Apple(appleId=3, appleName=Gala)
Apple(appleId=7, appleName=Golden Delicious)
Apple(appleId=5, appleName=GrannySmith)
of(int page, int size, Direction direction, String... properties)
The static method of(int page, int size, Direction direction, String... properties)
defined in PageRequest
class is used to create a new sorted PageRequest
. PageRequest#of
method takes 4 input parameter, page
, size
, enum Direction
and Sort
class. the size will divide the entire record in the underlying database into sets and the page will query the particular set chunk, starting from 0, Sort
will sort the retriving records using sorting property provided based on the Direction
.
the Direction
enum is defined in Sort
and provides ASC
and DESC
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;
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(0, 5, Sort.Direction.DESC, new String[]{"appleName"});
Page<Apple> apples = appleRepository.findAll(page);
apples.forEach(System.out::println);
return apples.getContent();
}
}
package com.sujan.example.jpa.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Data
@ToString
@Entity
@NoArgsConstructor
public class Apple {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long appleId;
private String appleName;
public Apple(String appleName) {
this.appleName = 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_
Apple(appleId=11, appleName=Pinova)
Apple(appleId=6, appleName=PinkLady)
Apple(appleId=10, appleName=Mutsu)
Apple(appleId=1, appleName=Macintosh)
Apple(appleId=4, appleName=Jonagold)