| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
- 자바
- RCP
- 이펙티브
- Spring
- Web
- jface
- nodejs
- effective
- 후기
- 맛집
- node
- 백준
- MySQL
- 독후감
- 인터페이스
- JPA
- Git
- 리뷰
- Spring Boot
- boot
- 엘라스틱서치
- error
- 스프링
- java8
- elasticsearch
- kibana
- java
- 자바스크립트
- 알고리즘
- javascript
- Today
- Total
wedul
Mybatis의 동적 SQL 본문
Mybatis의 동적 SQL
Mybatis가 가지는 표현식은 다음과 같다.
If
Choose(when, otherwise)
Trim(where, set)
Foreach
|
기능 |
사용 예 |
설명 |
|
if |
<if test="title != null"> AND title like #{title} </if> |
코드로 작성할 때의 if 구문에 대한 처리 -> 참 거짓을 구별해서 사용할 때 처리한다. |
|
Choose, When, otherwise |
<choose> <when test="title != null"> AND title like #{title"" </when> <when test="author != null and author.name != null"> AND author_name like #{author.name} </when> <otherwise> AND featured = 1 </otherwise> </choose> |
switch와 같은 상황에 대한 처리 |
|
Trim, Where,set |
<trim prefix="where" prefixOverrides="AND/OR"> … </trim> |
로직을 처리하면서 필요한 구문을 변경 |
|
foreach |
<foreach item="item" index="index" collection="list" open="(" separator=," close=""> #{item} </foreach> |
컬렉션에 대한 순환처리 |
Mybatis에서 XML로 처리하는 것이 더 많은 이유는 SQL의 동적 사용 때문이다.
동적 SQL을 이용한 개발은 다음과 같은 단계로 진행한다.
-> 동적 SQL의 적용이 필요한 메소드의 설정
-> XML Mapper를 이용한 SQL 문 처리
-> 동적 SQL문의 생성 확인 및 테스트
<select id="listSearch" resultType="BoardVO">
<![CDATA[
select *
from tbl_board
where bno > 0
order by bno desc
limit #{pageStart}, #{perPageNum}
]]>
<if test="searchType != null" > <!-- if test는 boolean 으로 나오는 결과야 한다. -->
<if test="searchType == 't'.toString()">
and title like CONCAT('%', #{keyword}, '%')
</if>
<if test="searchType == 'c'.toString()">
and content like CONCAT('%', #{keyword}, '%')
</if>
<if test="searchType == 'w'.toString()">
and writer like CONCAT('%', #{keyword}, '%') <!-- like로 구분되어야 한다. -->
</if>
<if test="searchType == 'tc'.toString()">
and (title like CONCAT('%', #{keyword}, '%') OR content like CONCAT('%', #{keyword}, '%'))
</if>
<if test="searchType == 'cw'.toString()">
and (content like CONCAT('%', #{keyword}, '%') OR writer like CONCAT('%', #{keyword}, '%'))
</if>
<if test="searchType == 'tcw'.toString()">
and ( title like CONCAT('%', #{keyword}, '%') OR content like CONCAT('%', #{keyword}, '%') or
writer like CONCAT('%', #{keyword}, '%'))
</if>
</if>
<![CDATA[
order by bno desc limit #{pageStart}, #{perPageNum}
]]>
</select>
SQL문 조각을 붙히는 방법 include
-> SQL을 여러조각으로 나눈다음 원하는 부분에 include를 사용하여 추가 할 수 있다.
<select id="listSearch" resultType="BoardVO">
<![CDATA[
select *
from tbl_board
where bno > 0
order by bno desc
limit #{pageStart}, #{perPageNum}
]]>
<include refid="search"></include> <!-- include를 해서 조각난 sql을 합칠 수 있다. -->
<![CDATA[
order by bno desc limit #{pageStart}, #{perPageNum}
]]>
</select>
<sql id="search">
<if test="searchType != null" > <!-- if test는 boolean 으로 나오는 결과야 한다. -->
<if test="searchType == 't'.toString()">
and title like CONCAT('%', #{keyword}, '%')
</if>
<if test="searchType == 'c'.toString()">
and content like CONCAT('%', #{keyword}, '%')
</if>
<if test="searchType == 'w'.toString()">
and writer like CONCAT('%', #{keyword}, '%') <!-- like로 구분되어야 한다. -->
</if>
<if test="searchType == 'tc'.toString()">
and (title like CONCAT('%', #{keyword}, '%') OR content like CONCAT('%', #{keyword}, '%'))
</if>
<if test="searchType == 'cw'.toString()">
and (content like CONCAT('%', #{keyword}, '%') OR writer like CONCAT('%', #{keyword}, '%'))
</if>
<if test="searchType == 'tcw'.toString()">
and ( title like CONCAT('%', #{keyword}, '%') OR content like CONCAT('%', #{keyword}, '%') or
writer like CONCAT('%', #{keyword}, '%'))
</if>
</if>
</sql>
출처 : 코드로 배우는 스프링 웹 프로젝트 : 남가람북스
'web > Spring' 카테고리의 다른 글
| spring에서 List 또는 Array 데이터를 Controller에서 받기 (0) | 2018.05.27 |
|---|---|
| UriComponents 클래스 (1) | 2016.12.27 |
| STS의 github 연동 (0) | 2016.12.21 |
| Mybatis의 #{} 문법 사용방법 (0) | 2016.12.21 |
| Spring의 UTF-8 처리 필터 등록 (0) | 2016.12.21 |
