검색창을 만들고 검색기능을 만들어보자
먼저 검색 버튼을 눌렀을때만 해당 검색창이 뜨게 할 경우 html 은 보이지 않게 설정한 뒤
<input id="search-header" type="search" name="title" class="form-control"
placeholder="검색" style="display: none;"
onkeypress="if(event.keyCode=='13'){event.preventDefault(); searchEvt();}" />
검색 버튼을 클릭하면 해당 창이 보이게 설정한뒤 포커스를 준다.
function searchBox() {
const searchHeader = document.getElementById("search-header");
if (searchHeader.style.display === "none") {
searchHeader.style.display = "block";
searchHeader.focus();
}
}
검색어를 입력하고 엔터를 치면 쿼리스트링을 이용해서 해당 검색을 수행한다.
function searchEvt() {
const keyword = $('#search-header').val();
location.href = "/jobs/search?keyword=" + keyword;
}
연결된 컨트롤러에서 해당 검색의 결과를 model 에 넣은뒤 페이지로 연결 시킨다.
검색을 하는 쿼리는 동적쿼리를 이용했다.
여러개의 조건으로 검색하는 쿼리를 정규표현식으로 연결시켰다.
where title regexp #{keyword} or
c.comp_name regexp #{keyword} or
career regexp #{keyword} or
education regexp #{keyword} or
position regexp #{keyword} or
address regexp #{keyword}
이와 비슷한 내용으로 이전 포스팅에서는 여러개의 체크박스 조건으로 검색하는 경우를 동적쿼리를 이용해서 처리했다.
<if test="jDto.address != null and jDto.address.size() > 0">
address in
<foreach collection="jDto.address" item="address" open="(" close=")" separator=",">
#{address}
</foreach>
and
</if>
<if test="jDto.skill != null and jDto.skill.size() > 0">
skill in
<foreach collection="jDto.skill" item="skill" open="(" close=")" separator=",">
#{skill}
</foreach>
and
</if>
<if test="jDto.position != null and jDto.position.size() > 0">
position in
<foreach collection="jDto.position" item="position" open="(" close=")" separator=",">
#{position}
</foreach>
and
</if>
career regexp #{jDto.career}
List 를 받아서 여러줄의 로우를 한번에 insert 하는 경우가 있을때도 동적쿼리를 이용한다.
파라미터 2개를 받을 경우
public int insertRequiredSkill(
@Param("skillList") List<String> skillList,
@Param("jobsId") Integer jobsId);
<insert id="insertRequiredSkill">
insert into required_skill_tb (jobs_id, skill)
values
<foreach collection="skillList" item="skill" separator=",">
(#{jobsId}, #{skill})
</foreach>
</insert >
파라미터를 1개로 받을 경우
public int insert(@Param("iDto") InterestChangeReqDto iDto);
<insert id="insert">
insert into interest_tb ( user_id , interest_ct )
values
<foreach collection="iDto.interestList" item="interest" separator=",">
(#{iDto.userId}, #{interest})
</foreach>
</insert>
null 아닌 파라미터가 들어왔을때만 실행시키고 싶을때 이전 조건에 콤마찍고 다음코드 연결
<if test="userId != null">
ifnull(( select user_scrap_id from user_scrap_tb where user_id = #{userId}
and jobs_id = j.jobs_id ),null) user_scrap_id
</if>