본문 바로가기

IT/mybatis

[mybatis] mybatis 동적 SQL 만들기

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) 문자가 만들어진다.

'IT > mybatis' 카테고리의 다른 글

[mybatis] insert 시 auto_increment로 생성된 키 값 얻기  (0) 2020.10.22