조건문에는 단일(if), 다중(choose, when, otherwise) 조건문이 있습니다.
1. 단일 조건문
1 2 3 4 5 6 7 8 9
<selectid="findBySearch"resultType="Sample"parameterType="SearchParam"> SELECT * FROM TB_SAMPLE WHERE column1 = #{parameter1} <iftest="parameter2 != null and parameter2 != ''"> AND column2 LIKE %'||#{parameter2}||'%' </if> </select>
2. 다중 조건문
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<selectid="findBySearch"resultType="Sample"parameterType="SearchParam"> SELECT * FROM TB_SAMPLE WHERE column1 = #{parameter1} <choose> <whentest="parameter2 != null and parameter2 == ''"> AND column2 = #{parameter2} </when> <otherwise> AND column3 = #{parameter3} </otherwise> </choose> </select>
비교 형식
1 2 3 4 5 6 7 8 9
<!-- 문자열 비교 --> <iftest="parameter != null and (parameter eq 'all'.toString())"></if> <iftest="!parameter.equals('all')"></if>
<!-- 공백 비교 --> <iftest="parameter == ' '"></if>
<!-- 값 비교 --> <iftest="uesYn == 'Y'"></if>
for 문
foreach를 사용하여 조건을 만들 수 있습니다.
1 2 3 4 5 6 7 8
<selectid="findBySearch"resultType="Sample"parameterType="SearchParam"> SELECT * FROM TB_SAMPLE WHERE <foreachitem="item"index="index"collection="list"open="("separator=","close=")"> column1 IN ${item} </foreach> </select>
where 절
1 2 3 4 5 6 7 8 9 10 11 12
<selectid="findBySearch"resultType="Sample"parameterType="SearchParam"> SELECT * FROM TB_SAMPLE <trimprefix="WHERE"prefixOverrides="AND|OR"> <iftest="parameter1 != null and parameter1 != ''"> AND column1 = #{parameter1} </if> <iftest="parameter2 != null and parameter2 != ''"> AND column2 = #{parameter2} </if> </trim> </select>