마이바티스 동적 SQL

1. <if>

<if test="type == 'T'.toString()">
(title like concat('%',#{type},'%'))
</if>

2. <choose>

  • 여러 상황들 중 하나의 상황에서 동작
  • java의 ‘if ~ else' 와 유사함
  • 예시
<choose>
<when test="type == 'T'.toString()">
    (title like concat('%',#{type},'%'))
</when>
<when test="type == 'C'.toString()">
    (content like concat('%',#{type},'%'))
</when>
<otherwise>
    (title like concat('%',#{type},'%') or content like concat('%',#{type},'%') or writer like concat('%',#{type},'%'))
</otherwise>
</choose>

3. <trim>,<where>,<set>

  • trim, where, set은 단독 사용하지 않음. if,choose등과 같은 태그들을 내포하여 SQL 연결해 주고, AND, OR, WHERE 등 앞.뒤에 필요한 구문들을 추가 및 생략하는 역할

where 태그

  • 태그 안쪽에서 SQL이 생성될 때 Where 구문이 붙고 그렇지 않으면 생성되지 않음
  • 예시
select * from tbl_board
<where>
    <if test="bno != null">
    bno = #{bno}
</where>
  • 결과 1 bno가 없는 경우 : select * from tbl_board where bno = 123
  • 결과 2 bno가 있는 경우 : select * from tbl_board

trim 태그

  • trim 안쪽에 만들어지는 SQL문을 조사하여, 앞쪽에 추가적인 SQL을 넣는 역할
  • 예시
select * from tbl_board
<where>
    <if test='bno != null'>
        bno = #{bno}
    </if>
    <trim prefixOverrides='and'>
    rownum = 1
    </trim>
</where>
  • 결과 1 bno 값 존재 시 : select * from tbl_board Where bno = 33 and rownum = 1
  • 결과 2 bno 값 null : select * from tbl_board wehre rownum = 1

  • prefix : trim 내부 구문의 제일 앞에 추가할 문자열
  • suffix : trim 내부 구문의 제일 마지막에 추가할 문자열
  • prefixOverrides : trim 내부 구문의 제일 앞에서, prefixOverrides 에 명시된 문자열 제거
  • suffixOverrides : trim 내부 구문의 제일 뒤에서, suffixOverrides 에 명시된 문자열 제거
  • with : overrides에 명시된 것을 지우고 with 속성에 명시된 것을 추가

4. <foreach>

  • List, 배열, 맵 등을 이용해서 루프 처리. 주로 IN 조건에서 많이 사용.
  • 배열이나 list 이용 시, item 속성만 사용
  • map 형태로 key 와 value 이용 시, index와 item 속성 사용
Map<String, String> map = new HashMap();
map.put("T","TTT");
map.put("C","CCC");
select * from tbl_board
<trim prefix="where (" suffix=")" prefixOverrides="OR">
    <foreach item="val" index="key" collection="map">

        <trim prefix = "OR">
            <if test="key=='C'.toString()">
            content = #{val}
            </if>
            <if test="key=='T'.toString()">
            title = #{val}
            </if>
            <if test="key=='W'.toString()">
            writer = #{val}
            </if>
        </trim>
    </foreach>

5. <sql><include>

  • 재사용 가능한 구문을 <sql id="test">…</sql> 에 선언하고 <include refid='test'></include> 를 사용하여 재사용 한다.