mybatis를 이용해서 게시물의 검색 기능을 구현해봅니다.
mybatis는 동적 태그 기능이 있습니다.
if
test라는 속성이 true가 되었을 때 if tag안에 선언된 sql이 실행됩니다.
if문 안의 표현식은 OGNL 표현식이라고 합니다. OGNL 참고 링크
example
<if test="type"=='T'.toString()"> (title like '%'||#{search_keyword}||'%') </if>
choose
일반적인 프로그래밍에서 if,else if,else문입니다. switch문 으로 봐도 됩니다.
when tag가 내부에 쓰입니다. when tag는 if와 동일하게 test 속성을 가지고 거의 같습니다.
'else'는 otherwise tag에 해당합니다.
example
<choose> <when test="type =='T'.toString()"> title like '%'||#{keyword}||'%' </when> <when test="type =='W'.toString()"> writer like '%' ||#{keyword}||'%' </when> <when test="type =='C'.toString()"> content like '%'||#{keyword}||'%' </when> <!-- <otherwise> title like '%'||#{keyword}||'%' </otherwise> --> </choose>
trim : prefix,suffix 등의 속성과 함께 쓰입니다. where절에서 sql 조건들을 연결해 줍니다.
나머진 자세히 쓰진 않겠습니다..
example
<foreach item='type' collection="typeArr"> <trim prefix="OR"> <if test=~> sql1 </if> <if test=~> sql2 </if> ... </trim> </foreach>
크게 아래 4가지 부분을 신경써주면 됩니다.