개발/정리 내용
검색처리
aihtnyc_h
2023. 1. 15. 23:19
728x90
반응형
SMALL
- 단일 항목 검색
제목/내용/작성자 - 다중 항목 검색
제목 or 내용, 제목 or 작성자, 내용 or 작성자, 제목 or 내용 or 작성자
(1) MyBatis의 동적 태그들
if
choose(when, otherwise)
where
trim
foreach
(2) if
검색조건이 'T' : 제목이 키워드인 항목을 검색
<if test="type == 'T'.toString()">
(title like '%'||#{keyword}||'%')
</if>
- 검색조건이 'C' : 내용이 키워드인 항목을 검색
<if test="type == 'C'.toString()">
(content like '%'||#{keyword}||'%')
</if>
- 검색조건이 'W' : 작성자가 키워드인 항목을 검색
<if test="type == 'W'.toString()">
(writer like '%'||#{keyword}||'%')
</if>
(3) choose
<choose>
<when test="type == 'T'.toString()">
(title like '%'||#{keyword}||'%')
</when>
...
<otherwise>
(title like '%'||#{keyword}||'%' OR content like '%'||#{keyword}||'%'
</otherwise>
</choose>
(4) where
select * from tbl_board
<where>
<if test="bno != null">
bno = #{bno}
</if>
</where>
(5) trim
select * from tbl_board
<where>
<if test="bno != null">
bno = #{bno}
</if>
<trim prefixOverrides = "and">
rownum = 1
</trim>
</where>
* bno가 null일 때
select * from tbl_board where rownum = 1
* bno가 null이 아닐 때
select * from tbl_board where bno = 변수 and rownum = 1
(6) foreach
List, 배열, Map 등을 이용하여 루프 처리
*파라미터
Map map = new HashMap();
map.put("T","TTTT");
map.put("C","CCCC");
*쿼리
select * from board
// 앞에 붙는 OR를 없애주기 위함
<trim prefix="where (" suffix=")" prefixOverrides="OR">
<foreach item="val" index="key" collection="map">
<trim prefix="OR">
<if test="key == 'T'.toString()">
title = #{val}
</if>
<if test="key == 'C'.toString()">
content = #{val}
</if>
<if test="key == 'W'.toString()">
writer = #{val}
</if>
</trim>
</foreach>
</trim>
* 결과
select * from tbl_board where (content = 'CCCC' OR title = 'TTTT')
728x90
반응형
LIST