728x90
반응형
<choose>, <when>, <otherwise> 사용법
java의 if~ else~ 문과 비슷함
만약 <when>태그의 조건식중 true 반환한 것이 없다면 <otherwise> 태그 내에 작성된 쿼리문이 실행됨
<otherwise>태그는 생략 가능
<choose>
<when test="조건식1"> 쿼리문1 </when>
<when test="조건식2"> 쿼리문2 </when>
<when test="조건식3"> 쿼리문3 </when>
<when test="조건식4"> 쿼리문4 </when>
<otherwise> 쿼리문5 </otherwise>
</choose>
사용 예제 👇
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test=test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
</choose>
</select>
REFERENCE
mybatis.org/mybatis-3/ko/sqlmap-xml.html
MyBatis – 마이바티스 3 | 매퍼 XML 파일
Mapper XML 파일 마이바티스의 가장 큰 장점은 매핑구문이다. 이건 간혹 마법을 부리는 것처럼 보일 수 있다. SQL Map XML 파일은 상대적으로 간단하다. 더군다나 동일한 기능의 JDBC 코드와 비교하면
mybatis.org
반응형
'프로그래밍 > Mybatis' 카테고리의 다른 글
[Mybatis] 자동 생성 키 SelectKey (0) | 2022.04.05 |
---|---|
[Mybatis] Cause: java.lang.NumberFormatException: For input string: "Y" 에러 해결 방법 (0) | 2022.03.17 |
Invalid bound statement (not found) 오류 (0) | 2021.12.15 |
There is no READABLE property named '***' in class 'java.lang.String' 오류 (0) | 2021.12.14 |
Mybatis의 isNull, isEmpty (0) | 2021.08.25 |