Spring/SpringBoot

Spring Boot으로 웹 출시까지 #8. JPA를 이용한 페이지 처리 및 검색

느리지만 꾸준하게 2022. 6. 2. 14:29

 

bootstrap를 이용해서 page-overview를 확인하는 ui를 변경해주자.

 

JPA를 이용해서 페이지를 가져오는 작업을 PagingAndSortingRepositorty interface를 검색해서 참고한다.

 

BoardController을 아래와 같이 해준다.

@GetMapping("/list")
    public String list(Model model) {
        Page<Board> boards = boardRepository.findAll(PageRequest.of(1, 20));
        model.addAttribute("boards", boards);
        return "board/list";
    }

 

pageable도 이용해서 코드를 작성해주자.

@GetMapping("/list")
    public String list(Model model, Pageable pageable) {
        Page<Board> boards = boardRepository.findAll(pageable);
        model.addAttribute("boards", boards);
        return "board/list";
    }

 

@GetMapping("/list")
    public String list(Model model, Pageable pageable) {
        Page<Board> boards = boardRepository.findAll(pageable);
        int startPage = Math.max(0, boards.getPageable().getPageNumber() - 4);
        int endPage = Math.min(boards.getTotalPages(), boards.getPageable().getPageNumber() + 4);
        model.addAttribute("startPage", startPage);
        model.addAttribute("endPage", endPage);
        model.addAttribute("boards", boards);
        return "board/list";
    }

 

thymeleaf의 numbers를 이용해보자. (thymeleaf number loop를 검색해서 자세한 예시를 보자.)

<th:block th:each="i: ${#numbers.sequence(0, response.count - 1)}">
    <button th:if="${response.page == i}" class="active">Dummy</button>
    <button th:unless="${response.page == i}">Dummy</button>
</th:block>

 

@PageableDefault 값도 넣어주자.

@GetMapping("/list")
    public String list(Model model, @PageableDefault(size = 2) Pageable pageable) {
        Page<Board> boards = boardRepository.findAll(pageable);
        int startPage = Math.max(1, boards.getPageable().getPageNumber() - 4);
        int endPage = Math.min(boards.getTotalPages(), boards.getPageable().getPageNumber() + 4);
        model.addAttribute("startPage", startPage);
        model.addAttribute("endPage", endPage);
        model.addAttribute("boards", boards);
        return "board/list";
    }

 

 

그리고 list.html에서 아래와 같이 코드를 넣어주자.

<li class="page-item" th:classappend="${i == boards.pageable.pageNumber + 1} ? 'disabled'" th:each="i : ${#numbers.sequence(startPage, endPage)}">
  <a class="page-link" href="#" th:href="@{/board/list(page=${i - 1},searchText=${param.searchText})}" th:text="${i}">1</a>
</li>

 

 

BoardRepository에서 메서드를 만들어준 걸 활용하여

package com.example.myhome.repository;

import com.example.myhome.model.Board;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface BoardRepository extends JpaRepository<Board, Long> {

    List<Board> findByTitle(String title);
    List<Board> findByTitleOrContent(String title, String content);

    Page<Board> findByFirstnameContainingOrContentContaining(String title, String content, Pageable pageable);
}

 

BoardController에서 아래와 같이 설정해준다.

@GetMapping("/list")
    public String list(Model model, @PageableDefault(size = 2) Pageable pageable, String searchText) {
//        Page<Board> boards = boardRepository.findAll(pageable);
        Page<Board> boards = boardRepository.findByFirstnameContainingOrContentContaining(searchText, searchText, pageable);
        int startPage = Math.max(1, boards.getPageable().getPageNumber() - 4);
        int endPage = Math.min(boards.getTotalPages(), boards.getPageable().getPageNumber() + 4);
        model.addAttribute("startPage", startPage);
        model.addAttribute("endPage", endPage);
        model.addAttribute("boards", boards);
        return "board/list";
    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<참고 :Spring Boot으로 웹 출시까지 #7. JPA 이용한 페이지 처리 및 검색>

https://www.youtube.com/watch?v=hmSPJHtZyp4&list=PLPtc9qD1979DG675XufGs0-gBeb2mrona&index=8