├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── javatechie
│ │ └── jpa
│ │ ├── PaginationSortingExampleApplication.java
│ │ ├── dto
│ │ └── APIResponse.java
│ │ ├── entity
│ │ └── Product.java
│ │ ├── repository
│ │ └── ProductRepository.java
│ │ └── service
│ │ └── ProductService.java
└── resources
│ └── application.properties
└── test
└── java
└── com
└── javatechie
└── jpa
└── PaginationSortingExampleApplicationTests.java
/README.md:
--------------------------------------------------------------------------------
1 | # spring-boot-pagination-sorting
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.5.4
9 |
10 |
11 | com.javatechie
12 | spring-data-jpa-pagination-sorting
13 | 0.0.1-SNAPSHOT
14 | pagination-sorting-example
15 | Demo project for Spring Boot
16 |
17 | 1.8
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-starter-data-jpa
23 |
24 |
25 | org.springframework.boot
26 | spring-boot-starter-web
27 |
28 |
29 |
30 | mysql
31 | mysql-connector-java
32 | runtime
33 |
34 |
35 | org.projectlombok
36 | lombok
37 | true
38 |
39 |
40 | org.springframework.boot
41 | spring-boot-starter-test
42 | test
43 |
44 |
45 |
46 |
47 |
48 |
49 | org.springframework.boot
50 | spring-boot-maven-plugin
51 |
52 |
53 |
54 | org.projectlombok
55 | lombok
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/jpa/PaginationSortingExampleApplication.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.jpa;
2 |
3 | import com.javatechie.jpa.dto.APIResponse;
4 | import com.javatechie.jpa.entity.Product;
5 | import com.javatechie.jpa.service.ProductService;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.boot.SpringApplication;
8 | import org.springframework.boot.autoconfigure.SpringBootApplication;
9 | import org.springframework.data.domain.Page;
10 | import org.springframework.data.domain.Sort;
11 | import org.springframework.web.bind.annotation.GetMapping;
12 | import org.springframework.web.bind.annotation.PathVariable;
13 | import org.springframework.web.bind.annotation.RequestMapping;
14 | import org.springframework.web.bind.annotation.RestController;
15 |
16 | import java.util.List;
17 |
18 | @SpringBootApplication
19 | @RestController
20 | @RequestMapping("/products")
21 | public class PaginationSortingExampleApplication {
22 |
23 | @Autowired
24 | private ProductService service;
25 |
26 | @GetMapping
27 | private APIResponse> getProducts() {
28 | List allProducts = service.findAllProducts();
29 | return new APIResponse<>(allProducts.size(), allProducts);
30 | }
31 |
32 | @GetMapping("/{field}")
33 | private APIResponse> getProductsWithSort(@PathVariable String field) {
34 | List allProducts = service.findProductsWithSorting(field);
35 | return new APIResponse<>(allProducts.size(), allProducts);
36 | }
37 |
38 | @GetMapping("/pagination/{offset}/{pageSize}")
39 | private APIResponse> getProductsWithPagination(@PathVariable int offset, @PathVariable int pageSize) {
40 | Page productsWithPagination = service.findProductsWithPagination(offset, pageSize);
41 | return new APIResponse<>(productsWithPagination.getSize(), productsWithPagination);
42 | }
43 |
44 | @GetMapping("/paginationAndSort/{offset}/{pageSize}/{field}")
45 | private APIResponse> getProductsWithPaginationAndSort(@PathVariable int offset, @PathVariable int pageSize,@PathVariable String field) {
46 | Page productsWithPagination = service.findProductsWithPaginationAndSorting(offset, pageSize, field);
47 | return new APIResponse<>(productsWithPagination.getSize(), productsWithPagination);
48 | }
49 |
50 |
51 |
52 | public static void main(String[] args) {
53 | SpringApplication.run(PaginationSortingExampleApplication.class, args);
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/jpa/dto/APIResponse.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.jpa.dto;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 | @Data
8 | @AllArgsConstructor
9 | @NoArgsConstructor
10 | public class APIResponse {
11 |
12 | int recordCount;
13 | T response;
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/jpa/entity/Product.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.jpa.entity;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 | import javax.persistence.Entity;
8 | import javax.persistence.GeneratedValue;
9 | import javax.persistence.Id;
10 | import javax.persistence.Table;
11 |
12 | @Entity
13 | @Table(name = "PRODUCT_TBL")
14 | @Data
15 | @NoArgsConstructor
16 | @AllArgsConstructor
17 | public class Product {
18 | @Id
19 | @GeneratedValue
20 | private int id;
21 | private String name;
22 | private int quantity;
23 | private long price;
24 |
25 | public Product(String name, int quantity, long price) {
26 | this.name = name;
27 | this.quantity = quantity;
28 | this.price = price;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/jpa/repository/ProductRepository.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.jpa.repository;
2 |
3 | import com.javatechie.jpa.entity.Product;
4 | import org.springframework.data.jpa.repository.JpaRepository;
5 |
6 | public interface ProductRepository extends JpaRepository {
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/jpa/service/ProductService.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.jpa.service;
2 |
3 | import com.javatechie.jpa.entity.Product;
4 | import com.javatechie.jpa.repository.ProductRepository;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.data.domain.Page;
7 | import org.springframework.data.domain.PageRequest;
8 | import org.springframework.data.domain.Sort;
9 | import org.springframework.stereotype.Service;
10 |
11 | import javax.annotation.PostConstruct;
12 | import java.security.PublicKey;
13 | import java.util.List;
14 | import java.util.Random;
15 | import java.util.stream.Collectors;
16 | import java.util.stream.IntStream;
17 |
18 | @Service
19 | public class ProductService {
20 |
21 | @Autowired
22 | private ProductRepository repository;
23 |
24 | // @PostConstruct
25 | // public void initDB() {
26 | // List products = IntStream.rangeClosed(1, 200)
27 | // .mapToObj(i -> new Product("product" + i, new Random().nextInt(100), new Random().nextInt(50000)))
28 | // .collect(Collectors.toList());
29 | // repository.saveAll(products);
30 | // }
31 |
32 |
33 | public List findAllProducts() {
34 | return repository.findAll();
35 | }
36 |
37 |
38 | public List findProductsWithSorting(String field){
39 | return repository.findAll(Sort.by(Sort.Direction.ASC,field));
40 | }
41 |
42 |
43 | public Page findProductsWithPagination(int offset,int pageSize){
44 | Page products = repository.findAll(PageRequest.of(offset, pageSize));
45 | return products;
46 | }
47 |
48 | public Page findProductsWithPaginationAndSorting(int offset,int pageSize,String field){
49 | Page products = repository.findAll(PageRequest.of(offset, pageSize).withSort(Sort.by(field)));
50 | return products;
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
2 | spring.datasource.url = jdbc:mysql://localhost:3306/javatechie
3 | spring.datasource.username = root
4 | spring.datasource.password = Password
5 | spring.jpa.show-sql = true
6 | spring.jpa.hibernate.ddl-auto = update
7 | spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
8 | server.port=9191
9 | spring.jpa.properties.hibernate.format_sql=true
--------------------------------------------------------------------------------
/src/test/java/com/javatechie/jpa/PaginationSortingExampleApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.jpa;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest
7 | class PaginationSortingExampleApplicationTests {
8 |
9 | @Test
10 | void contextLoads() {
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------