728x90
MyBatis의 가장 강력한 기능 중 하나는 특정 조건에 따라 변경되는 동적 SQL (Dynamic SQL) 기능이다.
if
조건식 작성
<!-- DeptMapper.xml -->
<!-- dname 파라미터 값이 null 여부에 따라 SQL문 실행 -->
<select id="selectAllorDname" resultType="DeptDTO" parameterType="string">
select deptno, dname, loc
from dept
<where>
<if test="dname != null">
dname = #{dname}
</if>
</where>
order by 1
</select>

choose / when / otherwise
다중 조건식 작성
<!-- DeptMapper.xml -->
<!-- loc = 값 에 따라 SELECT 결과가 달라지는 SQL 쿼리 작성-->
<select id ="selectByLocChoose" parameterType="hashmap" resultType="DeptDTO">
select deptno, dname, loc
from dept
<where>
<choose>
<when test="loc == '서울'">
deptno IN (50, 60)
</when>
<when test="loc == '부산'">
deptno = 70
</when>
<otherwise>
deptno In (10,20,30,40)
</otherwise>
</choose>
</where>
</select>

728x90
'Framework & Library > MyBatis' 카테고리의 다른 글
[Oracle/MyBatis] 에러 모음 (0) | 2023.08.07 |
---|---|
[MyBatis] 동적 SQL (Dynamic SQL) 기능 - foreach문 (0) | 2023.08.07 |
[MyBatis] SQL Map XML 파일 (0) | 2023.08.07 |
[MyBatis] INSERT문/UPDATE문/DELETE문 요청 (0) | 2023.08.04 |
[MyBatis] MyBatis 실행 순서 정리 ✍️ - SELECT 문 (0) | 2023.08.04 |
728x90
MyBatis의 가장 강력한 기능 중 하나는 특정 조건에 따라 변경되는 동적 SQL (Dynamic SQL) 기능이다.
if
조건식 작성
<!-- DeptMapper.xml -->
<!-- dname 파라미터 값이 null 여부에 따라 SQL문 실행 -->
<select id="selectAllorDname" resultType="DeptDTO" parameterType="string">
select deptno, dname, loc
from dept
<where>
<if test="dname != null">
dname = #{dname}
</if>
</where>
order by 1
</select>

choose / when / otherwise
다중 조건식 작성
<!-- DeptMapper.xml -->
<!-- loc = 값 에 따라 SELECT 결과가 달라지는 SQL 쿼리 작성-->
<select id ="selectByLocChoose" parameterType="hashmap" resultType="DeptDTO">
select deptno, dname, loc
from dept
<where>
<choose>
<when test="loc == '서울'">
deptno IN (50, 60)
</when>
<when test="loc == '부산'">
deptno = 70
</when>
<otherwise>
deptno In (10,20,30,40)
</otherwise>
</choose>
</where>
</select>

728x90
'Framework & Library > MyBatis' 카테고리의 다른 글
[Oracle/MyBatis] 에러 모음 (0) | 2023.08.07 |
---|---|
[MyBatis] 동적 SQL (Dynamic SQL) 기능 - foreach문 (0) | 2023.08.07 |
[MyBatis] SQL Map XML 파일 (0) | 2023.08.07 |
[MyBatis] INSERT문/UPDATE문/DELETE문 요청 (0) | 2023.08.04 |
[MyBatis] MyBatis 실행 순서 정리 ✍️ - SELECT 문 (0) | 2023.08.04 |