게시판에 검색기능을 넣으려고 하는데
컨트롤러 부분
@GetMapping("/paging")
public String paging(@RequestParam(required = false, defaultValue = "") String searchKeyword,
@PageableDefault(page = 1, size=10, sort="id", direction = Sort.Direction.DESC) Pageable pageable,
Model model) {
Page<BoardDTO> boardList = null;
if (searchKeyword == null || searchKeyword.isEmpty()) {
boardList = boardService.paging(pageable);
} else {
boardList = boardService.boardSearchList(searchKeyword, pageable);
}
int blockLimit = 3;
int startPage = (((int) (Math.ceil((double) pageable.getPageNumber() / blockLimit))) - 1) * blockLimit + 1;
int endPage = ((startPage + blockLimit - 1) < boardList.getTotalPages()) ? startPage + blockLimit - 1 : boardList.getTotalPages();
model.addAttribute("boardList", boardList);
model.addAttribute("startPage", startPage);
model.addAttribute("endPage", endPage);
model.addAttribute("searchKeyword", searchKeyword);
return "/board/paging";
}
public String paging(@RequestParam(required = false, defaultValue = "") String searchKeyword,
@PageableDefault(page = 1, size=10, sort="id", direction = Sort.Direction.DESC) Pageable pageable,
Model model) {
Page<BoardDTO> boardList = null;
if (searchKeyword == null || searchKeyword.isEmpty()) {
boardList = boardService.paging(pageable);
} else {
boardList = boardService.boardSearchList(searchKeyword, pageable);
}
int blockLimit = 3;
int startPage = (((int) (Math.ceil((double) pageable.getPageNumber() / blockLimit))) - 1) * blockLimit + 1;
int endPage = ((startPage + blockLimit - 1) < boardList.getTotalPages()) ? startPage + blockLimit - 1 : boardList.getTotalPages();
model.addAttribute("boardList", boardList);
model.addAttribute("startPage", startPage);
model.addAttribute("endPage", endPage);
model.addAttribute("searchKeyword", searchKeyword);
return "/board/paging";
}
레파지토리 제목으로 찾는 구간
public interface BoardRepository extends JpaRepository<BoardEntity, Long> {
// update board_table set board_hits=board_hits+1 where id=?
@Modifying
@Query(value = "update BoardEntity b set b.hit=b.hit+1 where b.id=:id")
void updateHits(@Param("id") Long id);
Page<BoardEntity> findByTitleContaining(String searchKeyword, Pageable pageable);
}
// update board_table set board_hits=board_hits+1 where id=?
@Modifying
@Query(value = "update BoardEntity b set b.hit=b.hit+1 where b.id=:id")
void updateHits(@Param("id") Long id);
Page<BoardEntity> findByTitleContaining(String searchKeyword, Pageable pageable);
}
서비스구간
public Page<BoardDTO> boardSearchList(String searchKeyword, Pageable pageable) {
log.info("searchKeyword: " + searchKeyword);
if (searchKeyword == null) {
return Page.empty();
}
Page<BoardEntity> boardEntities = boardRepository.findByTitleContaining(searchKeyword, pageable);
return boardEntities.map(board -> new BoardDTO(board.getId(), board.getBoardWriter(), board.getTitle(), board.getHit(), board.getCreatedTime(), board.getUpdatedTime()));
}
log.info("searchKeyword: " + searchKeyword);
if (searchKeyword == null) {
return Page.empty();
}
Page<BoardEntity> boardEntities = boardRepository.findByTitleContaining(searchKeyword, pageable);
return boardEntities.map(board -> new BoardDTO(board.getId(), board.getBoardWriter(), board.getTitle(), board.getHit(), board.getCreatedTime(), board.getUpdatedTime()));
}
이러는데 검색결과가 안나와요 뭐가 문제일까요?
댓글 0