[spring 토이프로젝트] 게시판 crud, 검색, 페이징 구현- 5. 페이지 검색기능 구현

이건회·2022년 7월 29일
0

spring게시판1

목록 보기
5/6

레퍼런스 : https://velog.io/@max9106/Spring-Boot-JPA-MySQL-%EA%B2%80%EC%83%89%EA%B8%B0%EB%8A%A5

프로젝트 전체 소스코드 : https://github.com/rawfishthelgh/boardcrud.git

  • list.html에서 검색 form을 누르면 "/board/search"로 get 요청이 들어가며, "keyword"라는 이름으로 검색어 값을 넘겨준다.

  • @RequestParam을 통해 가져올 데이터의 이름(keyword)을 넣어주면, 앞으로 Service에 구현할 searchPost 메소드가 해당 키워드가 포함된 리스트를 BoardDto로 변환해 model에 "boardList" 라는 이름으로 model에 저장하고 이 값을 view에 넘겨준다.(참고 : https://hongku.tistory.com/119)

  • repository에 title에 keyword 값이 포함된 데이터를 찾는 findByTitleContainding()을 구현할 것이고, 이를 사용해 데이터를 가져와 boards 라는 이름의 리스트에 넣는다.
  • for 문을 이용해 board를 차례로 가져와, 엔티티를 Dto로 변환하는 convertEntityToDto를 구현하고 board를 변환한 Dto 타입 엔티티를 boardDtoList에 넣어준다.

  • JpaRepository에서 By뒷 부분은 where 절에 해당한다. 또 Containing 을 붙여주면 "like" 검색이 되므로 findByTitleContaing은 "select * from Board where title like "%{keyword}%" 가 된다.(참고: https://exhibitlove.tistory.com/262)

  • 전체 테이블 목록에서 키워드를 검색하면
  • 키워드에 따른 검색목록이 완성된다.
profile
하마드

0개의 댓글