반응형
나는 항상 마이바티스를 통해서 페이징처리를 했다.
이떄 필요한게 mysql 같은겨우는 offset과 limit
오라클은 rownum이 들어간다.
하지만 JPA를 해보니 쿼리문 없이
어느정도 페이징 처리가 가능했다.
살펴보자.

아래에 위치한 대로 페이징 처리가 된다.
먼저 컨트롤러를 보자
@GetMapping("/board/list")
public String boardList(Model model,
@PageableDefault(page = 0, size = 10, sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
String searchKeyword) {
Page<Board> list = null;
if(searchKeyword == null) {
list = boardService.boardList(pageable);
}else {
list = boardService.boardSearchList(searchKeyword, pageable);
}
int nowPage = list.getPageable().getPageNumber() + 1;
int startPage = Math.max(nowPage - 4, 1);
int endPage = Math.min(nowPage + 5, list.getTotalPages());
model.addAttribute("list", list);
model.addAttribute("nowPage", nowPage);
model.addAttribute("startPage", startPage);
model.addAttribute("endPage", endPage);
return "boardlist";
}
이떄 가장 중요한것은
@PageableDefault(page = 0, size = 10, sort = "id", direction = Sort.Direction.DESC) Pageable pageable
이부분이다.
page = 현재 페이지
size = 보여줄 게시물수
sort = "페이징 조건"
direction = 오름정렬 등
이렇게 설정해준뒤
nowPage , StartPage, endPage 변수를 각각 적용해준다.
이제 서비스단으로 보자 .
public Page<Board> boardList(Pageable pageable){
return boardRepository.findAll(pageable);
}
findAll에 아까 추가한 pageable을 추가해준다.
그뒤 JPA레포토리를 이용하여 리턴한다.
타임리프를 이용하여
<th:block th:each="page : ${#numbers.sequence(startPage, endPage)}">
<a th:if="${page != nowPage}" th:href="@{/board/list(page = ${page - 1}, searchKeyword = ${param.searchKeyword})}" th:text="${page}"></a>
<strong th:if="${page == nowPage}" th:text="${page}" style="color : red"></strong>
</th:block>
th:each 반복문을 이용하여 페이지를 꺼내준다.
그뒤 스트롱태그를 이용하여 현재 해당하는 페이지에 연결해준다.

이런식으로 페이징처리를 하면된다.
select
board0_.id as id1_0_,
board0_.content as content2_0_,
board0_.title as title3_0_
from
board board0_
where
board0_.title like ? escape ?
order by
board0_.id desc limit ?,
?
콘솔창에 SQL문이 다음과 같이 나온다.
https://github.com/MoonSeokHyun
MoonSeokHyun - Overview
http://mls0000.dothome.co.kr/. MoonSeokHyun has 14 repositories available. Follow their code on GitHub.
github.com
반응형
'Spring Boot > 기초 게시판제작 + maria DB' 카테고리의 다른 글
JPA를 사용한 게시판 만들기(8) 검색기능 및 페이징 {타임리프,스프링부트} (0) | 2022.05.11 |
---|---|
JPA를 사용한 게시판 만들기(6) 메시지띄우기 {타임리프,스프링부트} (0) | 2022.05.10 |
JPA를 사용한 게시판 만들기(5) 수정처리 {타임리프,스프링부트} (0) | 2022.05.10 |
JPA를 사용한 게시판 만들기(4) 삭제처리 {타임리프,스프링부트} (0) | 2022.05.06 |
JPA를 사용한 게시판 만들기(3) 상세보기작업 {타임리프,스프링부트} (0) | 2022.05.05 |