검색기능 추가하기

Que Lin·2021년 12월 31일
0

게시물 전체조회 시 작성자 / 제목 을 입력받아 해당 내용을 ajax로 조회하기. (조회순/ 추천순 정렬도 비슷하게 하면 될 것 같다?)

jsp body에서

<form class="row g-3 container text-center" style="margin-top: 100px;" action="/board/search" method="get">
<div class="col-auto" >
<select class="form-select" style="width:100px;height:40px; display:inline;" name="searchtype">
<option value="b_title">제목</option>
<option value="m_id">작성자</option>
</select>
</div>

<div class="col-auto">
<input class="form-control"  type="text" name="keyword">
</div>

<div class="col-auto">
<input class="form-control"  type="submit" value="검색">
</div>
</form>

controller

@RequestMapping(value="search", method=RequestMethod.GET)
	public String search(@RequestParam("searchtype") String searchtype,
			@RequestParam("keyword") String keyword, Model model) {
		List<BoardDTO> bList = bs.search(searchtype, keyword);
		model.addAttribute("bList", bList);
		
		return "/board/boardFindAll";
	}

serviceImpl - 검색종류와 키워드 두 값을 넘겨야 해서 Map으로 담음

@Override
	public List<BoardDTO> search(String searchtype, String keyword) {
		Map<String, String> searchParam = new HashMap<String, String>();
		searchParam.put("type", searchtype);
		searchParam.put("word", keyword);
		List<BoardDTO> bList = br.search(searchParam);
		return bList;
	}

repository

public List<BoardDTO> search(Map<String, String> searchParam) {
		return sql.selectList("Board.search", searchParam);
	}

mapper에서 like 연산자 (% 와일드 카드)를 사용 할 때 concat을 붙여줘야 한다!

<select id="search" parameterType="java.util.HashMap" resultType="board">
		select * from board_table
		<include refid="sear"></include>
	</select>

	<sql id="sear">
		<choose>
			<when test="type=='b_title'">
				where b_title like concat ('%', #{word}, '%')
			</when>

			<when test="type=='m_id'">
				where m_id like concat ('%', #{word}, '%')
			</when>
		</choose>
	</sql>

mapper가 처음보는 형태라 이해가 더디지만,
좋아요순 정렬, 조회수 정렬을 해보면 내것이 될 것 같다

profile
1일 1커밋 1일 1벨로그!

0개의 댓글