



package com.study.board.repository;
import com.study.board.entity.Board;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BoardRepository extends JpaRepository<Board, Integer> {
Page<Board> findByTitleContaining(String searchKeyword, Pageable pageable);
}
package com.study.board.service;
import com.study.board.entity.Board;
import com.study.board.repository.BoardRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.UUID;
@Service
public class BoardService {
@Autowired
private BoardRepository boardRepository;
public Page<Board> boardSearchList(String searchKeyword, Pageable pageable) {
return boardRepository.findByTitleContaining(searchKeyword, pageable);
}
}
package com.study.board.controller;
import com.study.board.entity.Board;
import com.study.board.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class BoardController {
@Autowired
private BoardService boardService;
@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);
}
// 현재 페이지
// pageable이 가지고 있는 page는 0에서 시작하기 때문에 우리가 보는 부분보다 1이 적다. 그래서 +1
int nowPage = list.getPageable().getPageNumber() + 1;
// 첫 번째 페이지
int firstPage = 1;
// 마지막 페이지
int lastPage = list.getTotalPages();
model.addAttribute("list", list);
model.addAttribute("nowPage", nowPage);
model.addAttribute("firstPage", firstPage);
model.addAttribute("lastPage", lastPage);
return "boardlist";
}
}

searchKeyword=11&page=0 : title에 11을 포함한 것들&1페이지
<!-- start페이지부터 end페이지 까지 반복을 할거다 -->
<th:block th:each="page : ${#numbers.sequence(firstPage,lastPage)}">
<!-- 만약에 페이지 블록에 보이는 페이지가 현재 페이지와 다르다면 -->
<a th:href="@{/board/list(page=${page - 1},searchKeyword = ${param.searchKeyword})}" th:if="${page != nowPage}"
th:text="${page}"></a>
<!-- 만약에 페이지 블록에 보이는 페이지가 현재 페이지와 다르다면 -->
<strong style="color: red" th:if="${page == nowPage}" th:text="${page}"></strong>
</th:block>
<form method="get" th:action="@{/board/list}">
<input name="searchKeyword" type="text">
<button type="submit">검색</button>
</form>

