티스토리 뷰

mybatis

동적쿼리

yimoc 2018. 6. 22. 16:17

동적 Query 작성

 

<if> : 조건 처리

 

<if test="shopNo > 0">

WHERE shop_np = #{shopNo}

 

<choose> :    선택

<choose>

<when test="shopStatus != null and shopStatus == 'O'.toString()">

WHERE SHOP_STATUS = 'Y'

</when>

<when test="shopStatus != null and shopStatus == 'X'.toString()">

WHERE SHOP_STATUS = 'N'

</when>

<otherwise>

WHERE SHOP_STATUS IS NULL

</otherwise>

<choose>

 

<where> where절을 동적으로 추가할때 사용

<where>

<if test="shopNo > 0">

WHERE SHOP_NO=#{shopNo}

</if>

<if test="shopStatus != null and shopStatus != ''">

WHERE SHOP_STATUS = #{shopStatus}

</if>

</where>

 

<trim> 사용자가 원하는 문자나 문자열을 명시적으로 추가 혹은 삭제

<trim prefix="WHERE" prefixOverrides="AND|OR">

<if test="shopNo > 0">

SHOP_NO=#{shopNo}

</if>

<if test="shopStatus != null and shopStatus != ''">

SHOP_STATUS = #{shopStatus}

</if>

<tirm>

 cf)

suffix : 구문 맨 뒤에 추가되는 문자나 문자열 지어

suffixOverrides : suffix 속성값 앞에 바로오는 생략 문자열을 지정

 

<set> set절을 동적으로 추가

<set>

<if test="shopName != null and shopName !='">

SET SHOP_NAME = #{shopName},

</if>

<if test="shopStatus != null and shopStatus !=''">

SET SHOP_STATUS = #{shopStatus},

</if>

</set>

 

<foreach> 반복 구성 요소 :IN 절 지정시 사용

 

SELECT SHOP_NO, SHOP_NAME, SHOP_LOCATION, SHOP_STATUS

FROM SHOP

<where>

<if test="shopNo > 0">

AND SHO_NO = #{shopNo}

</if>

<foreach collection="shopStatuses" item="shopStatus" index="count"

open="AND SHOP_STATUS IN (" separator="," close=")">

#{shopStatus}

</foreach>

</where>

 

=>

SELECT SHOP_NO, SHOP_NAME, SHOP_LOCATION, SHOP_STATUS

FROM SHOP

WHERE SHOP_NO =? AND SHOP_STAUTS IN ( ? , ? )

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함