JPA로 리스트를 뿌려줄때 더보기 버튼을 활용하여 10개씩 뿌려주기 위해 page를 사용하였습니다.
public interface NotiRepository extends JpaRepository<Noti, Long>{
List<Noti> findByPeople_idAndGubunCd(Pageable pageable,long id,char cd);}
JPA Reposiotry에 page<>대신 List<>를 통해 활용할 메서드를 작성하고 파라미터는 Pageable빼고 원하는 조건을 넣어준다. List<>사용시 count가 포함되지 않습니다.
@Override
public List<Noti> getMoreNoti(int pageNum, long id, char cd) {
// TODO Auto-generated method stub
int page = (pageNum == 0) ? 0 : (pageNum - 1); // page는 index 처럼 0부터 시작
Pageable pageable = PageRequest.of(page, 9, Sort.Direction.DESC, "id");// 내림차순으로 정렬한다
return notifyRepository.findByPeople_idAndGubunCd(pageable, id, cd);
}
service 에 pageNum을 파라미터로 하여 id값을 기준으로 최근 9개씩 조회하도록 하는 메소드를 작성합니다.
@ResponseBody
@GetMapping("/getMyNotiList/{pageNum}")
public Map<String,Object> getMyNotiList(HttpSession session,@PathVariable("pageNume")int pageNum) {
long peopleId = (long) session.getAttribute("peopleId");
List<Noti> notiList=notiService.getMoreNoti(pageNum, peopleId, 'P');
Map<String,Object> data=new HashMap<String,Object>();
data.put("notiList",notiList);
return data;
}
조회할 page번호를 pathvariable로 가져와 조회하여주면됩니다.