반응형
참고 URL
- 공식문서 Spring Doc
- PageRequest Custom PageRequestCustom
Test Repository
- [GithubRepo] (https://github.com/lso5507/demo-pageable)
필요이유
- 페이징 기능이 필요할때 Page관련 모델을 생성 후에 사용자가 관리해줘야 함.
장점
- page,Size,sort 등의 Parameter정보를 제공하면 봉투패턴으로 page정보와 Content정보를 전달해줌
단점
- 데이터에 대한 추가가공이 필요할 경우에 pageImpl 클래스를 상속받아 작업해야함
DataInit
Controller
package com.example.demopageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/customers")
public class CustomerController {
@Autowired
CustomerService service;
@GetMapping("/search")
public Page<Customer> search(
@RequestParam("searchTerm") String searchTerm,
@RequestParam(value = "page", required = false, defaultValue = "0") int page,
@RequestParam(value = "size", required = false, defaultValue = "10") int size)
{
return service.search(searchTerm, page, size);
}
@GetMapping
public Page<Customer> getAll() {
return service.findAll();
}
@GetMapping("/page")
public Page<Customer> getPageParam(@RequestParam int page, @RequestParam int size) {
return service.findAll(page,size);
}
}
- /Customers : 전체 유저를 조회
- /Customers/page : 사용자가 지정한 page, size에 알맞는 유저를 조회
- /Customers/search : 사용자 이름을 키워드로 검색 ( List 형식 리턴 )
Service
package com.example.demopageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
@Service
public class CustomerService {
@Autowired
CustomerRepository repository;
public Page<Customer> search(
String searchTerm,
int page,
int size) {
PageRequest pageRequest = PageRequest.of(
page,
size,
Sort.Direction.ASC,
"name");
return repository.search(searchTerm.toLowerCase(), pageRequest);
}
public Page<Customer> findAll() {
int page = 0;
int size = 10;
PageRequest pageRequest = PageRequest.of(
page,
size,
Sort.Direction.ASC,
"name");
return new PageImpl<>(repository.findAll(), pageRequest, size);
}
public Page<Customer> findAll(int page,int size) {
PageRequest pageRequest = PageRequest.of(
page,
size,
Sort.Direction.ASC,
"name");
return repository.findAll(pageRequest);
//return new PageImpl<>(repository.findAll(pageRequest).getContent(), pageRequest, size);
}
}
Repository
package com.example.demopageable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long> {
@Query("FROM Customer c WHERE LOWER(c.name) like %:searchTerm% OR LOWER(c.email) like %:searchTerm%")
Page<Customer> search(@Param("searchTerm") String searchTerm, Pageable pageable);
}
반응형
'Java' 카테고리의 다른 글
Java는 왜 Call By Value인가? (2) | 2024.01.04 |
---|---|
[Spring]외장톰캣 특정 war 미로드 (0) | 2023.10.22 |
[QueryDSL]Q Class Import 불가 (0) | 2023.06.02 |
[Spring]SecurityContextHolder란? (1) | 2023.05.04 |
[SpringBoot]SpringBoot 3.x.x War 배포에러 (0) | 2023.03.01 |