package com.springdatejpa.springboot.repository;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import com.springdatejpa.springboot.entity.Product;
@SpringBootTest
public class PaginationAndSortingTest {
@Autowired
private ProductRepository productRepository;
@Test
void pagination() {
int pageNo = 0;
int pageSize = 5;
// create pageable object
Pageable pageable = PageRequest.of(pageNo, pageSize);
// findAll method and pass pageable instance
Page<Product> page = productRepository.findAll(pageable);
List<Product> products = page.getContent();
products.forEach((product) -> System.out.println(product));
// total pages
int totalPage = page.getTotalPages();
// total elements
long totalElements = page.getTotalElements();
// number of elements
int numberOfElements = page.getNumberOfElements();
// size
int size = page.getSize();
// last
boolean isLast = page.isLast();
// first
boolean isFirst = page.isFirst();
System.out.println("totalPage : " + totalPage);
System.out.println("totalElements : " + totalElements);
System.out.println("numberOfElements : " + numberOfElements);
System.out.println("size : " + size);
System.out.println("isLast : " + isLast);
System.out.println("isFirst : " + isFirst);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductRepository productRepository;
@GetMapping
public Page<Product> getProducts(Pageable pageable, @RequestParam(required = false) Long lastItemId) {
if (lastItemId != null) {
// 이전 페이지의 마지막 아이템을 기준으로 다음 페이지를 요청합니다.
return productRepository.findByIdGreaterThanOrderByPriceAsc(lastItemId, pageable);
} else {
// 첫 번째 페이지 요청입니다.
return productRepository.findAllByOrderByPriceAsc(pageable);
}
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductRepository productRepository;
@GetMapping
public Page<Product> getProducts(Pageable pageable, @RequestParam(name = "lastItemId", required = false) Long lastItemId) {
if (!pageable.isFirst()) {
// 이전 페이지가 아닌 경우, 이전 페이지의 마지막 아이템을 기준으로 다음 페이지를 요청합니다.
return productRepository.findByIdGreaterThanOrderByPriceAsc(lastItemId, pageable);
} else {
// 첫 번째 페이지 요청입니다.
return productRepository.findAllByOrderByPriceAsc(pageable);
}
}
}
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
public interface ProductRepository extends CrudRepository<Product, Long> {
Page<Product> findAllByOrderByPriceAsc(Pageable pageable);
Page<Product> findByIdGreaterThanOrderByPriceAsc(Long lastItemId, Pageable pageable);
}
SELECT * FROM product WHERE id > :lastItemId ORDER BY price ASC LIMIT :pageSize