MyBatis의 동적 SQL 기능
| |
| select * from board; |
| |
| |
| select * from board |
| where title = 'winter'; |
| |
| |
| select * from board |
| where title = 'winter' and content = 'summer'; |
위 쿼리 세 개는 게시판에서 SELECT문을 사용하여 데이터를 추출하고 있습니다.
위 세 쿼리들은 조건절만 다르기 때문에 MyBatis의 동적 SQL 기능을 사용하면 세 개의 SQL문을 한 개의 SQL문으로 구현이 가능합니다.
| |
| |
| |
| |
| |
| <where> |
| <if test="조건1">SQL 문</when> |
| <if test="조건2">SQL 문</when> |
| </where> |
사용법
| |
| |
| |
| <if test="조건식">SQL 문</if> |
예제
| |
| <select id="selectBoard" parameterType="String" resultType="Board"> |
| SELECT * FROM board |
| <where> |
| <if test="id != '' and id != null"> |
| title = #{title} |
| </if> |
| <if test="content != '' and content != null"> |
| and content = #{content} |
| </if> |
| </where> |
| order by id desc |
| </select> |
| |
| |
| <select id="selectBoard2" parameterType="String" resultType="Board"> |
| SELECT * FROM board |
| WHERE 1=1 |
| <if test="title !='' and title != null"> |
| AND title LIKE CONCAT('%',#{title},'%') |
| </if> |
| </select> |
- <if> 태그안에는 속성 값을 체크하여 공백이 아니거나 null이 아닌 경우 구문 추가
- 예제 2번 처럼 `WHERE 1=1`을 두고 뒤에 조건식을 붙여도됩니다.
사용법
| |
| |
| |
| |
| |
| <choose> |
| <when test="조건식1"> |
| 구문 |
| </when> |
| <when test="조건식2"> |
| 구문 |
| </when> |
| ... |
| <otherwise> |
| 구문 |
| </otherwise> |
| </choose> |
예제
| |
| <select id="selectBoard" parameterType="String" resultType="Board"> |
| select * from board |
| <where> |
| <choose> |
| <when test="title != '' and title != null and content != '' nad content != null"> |
| title=#{title} and content=#{content} |
| </when> |
| <when test="title != '' and title != null"> |
| title=#{title} |
| </when> |
| <when test="content != '' and content != null"> |
| content=#{content} |
| </when> |
| <otherwise> |
| id = 1 |
| </otherwise> |
| </choose> |
| </where> |
| </select> |
| |
| |
| <select id="selectList" parameterType="map" resultMap="projectResultMap"> |
| select PNO, PNAME, STA_DATE, END_DATE, STATE |
| from PROJECTS |
| order by |
| <choose> |
| <when test="orderCond=='TITLE_ASC'">PNAME asc</when> |
| <when test="orderCond=='TITLE_DESC'">PNAME desc</when> |
| <when test="orderCond=='STARTDATE_ASC'">STA_DATE asc</when> |
| <when test="orderCond=='STARTDATE_DESC'">STA_DATE desc</when> |
| <when test="orderCond=='ENDDATE_ASC'">END_DATE asc</when> |
| ... |
| <when test="orderCond=='PNO_ASC'">PNO asc</when> |
| <otherwise>PNO desc</otherwise> |
| </choose> |
| </select> |
- <otherwise> 태그는 생략할 수 있다.
| |
| |
| |
| |
| |
| <trim prefic="단어" prefixOverrides="문자열|문자열"> |
| <if test="조건1">SQL 문</when> |
| <if test="조건2">SQL 문</when> |
| </trim> |
| |
| |
| |
| |
| |
| <set> |
| <if test="조건1">SQL 문</when> |
| <if test="조건2">SQL 문</when> |
| </set> |
사용법
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <foreach |
| item="항목" |
| index="인덱스" |
| collection="목록" |
| open="시작문자열" |
| close="종료문자열" |
| separator="구분자"> |
| </foreach> |
예제
| <select id="foreachSelect" parameterType="java.util.Map" resultMap="memberResult"> |
| SELECT * FROM member |
| WHERE name IN |
| <foreach item = "item" collection = "list" open="(" separator"," close=")" > |
| #{item} |
| </foreach> |
| </select> |
- SQL문에 배열 혹은 List를 전달하면 자동으로 Map에 전달되어 이름을 key로 사용한다.
- List는 list를 키로 사용
- 배열은 array를 키로 사용
| |
| |
| |
| <bind name="변수명" value="값"/> |
| <update id="update" parameterType="map"> |
| update PROJECTS |
| <set> |
| <if test="title != null and !title.equals('')"> |
| TITLE = #{title}, |
| </if> |
| <if test="content != null and !content.equals('')"> |
| CONTENT = #{content}, |
| </if> |
| UPDATE_DATE = NOW() |
| </set> |
| where ID=#{id}; |
| </update> |
- `<set>` 태그
<set> 태그는 SET 절을 만듭니다. 위 코드로 예시를 든다면 content와 tags의 값이 null이 아니고 빈 문자열이 아닌 경우 다음과 같은 쿼리로 변환되어 처리됩니다.
set TITLE='변경할 제목', CONTENT='변경할 내용'
- 참고: SET절의 끝에 콤마(,)가 있어도 MyBatis에서 마지막에 있는 콤마(,)는 제거하기 때문에 고려하지 않아도 됩니다.