IT/mybatis
[mybatis] mybatis 동적 SQL 만들기
Bamdule
2021. 2. 1. 12:00
1. if문
<if test="Condition1 and Condition2">
Query
</if>
2. else if
<choose>
<when test="condition1">
Query1
</when>
<when test="condition2">
Query2
</when>
<otherwise>
Query3
</otherwise>
</choose>
3. update set
# 자동으로 ,을 붙인다.
UPDATE table_name
SET
<set>
<if test="condition1">
column1 = #{column1}
</if>
<if test="condition2">
column2 = #{column2}
</if>
</set>
WHERE
....
4. foreach
# ids = [1,2,3,4,5,6]
SELECT *
FROM table_name
WHERE id IN
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
# foreach문이 돌면서 (1,2,3,4,5,6) 문자가 만들어진다.