우선 mybatis와 ibatis에서는 동적태그를 기본적으로 지원을 합니다.
즉, 프로시저로 짜여져 있는 로직을 동적태그를 사용하여 쿼리로 변경할 수 있습니다.
장점이 있을수도 있지만 자칫 잘못 사용하면 큰 문제를 일으킬수 있다는 단점이있을수 있다는…
양날의 칼날과 같다는것만 알아두시면 되겠습니다.
ibatis 비교문 지원 태그
isNull : "널일경우"
isNotNull : "널이아닐경우"
isEmpty : "공백일경우"
isNotEmpty : "공백이아닐경우"
isGreaterTan : ">"
isGreaterEqual : ">="
isLessThan : "<"
isLessEqual : "<="
isEqual : "=="
isNotEqual : "!="
mybatis 비교문 지원 태그
if : 단일조건문
choose when otherwise : 다중조건문
위와같이 각 mybatis(ibatis) 별 동적태그를 지원합니다.
ibatis에 비해 mybatis의 비교문태그가 간단해졌습니다.
ibatis에서 지원되는 태그들 if문과 choose문만을 이용하여 비교가 가능해 진것이죠.
일반 자바코드와 ibatis/mybatis 별로 비교문 예를 들어보도록 하겠습니다.
Null 비교문
if(stringProperty == null) {
// code here
}
<!--ibatis -->
<isNull property="stringProperty">
조건문
</isNull>
<!--mybatis-->
<if test="stringProperty == null">
조건문
</if>
NOT NULL 비교문
if(stringProperty != null) {
// code here
}
<!--ibatis -->
<isNotNull property="stringProperty">
조건문
</isNotNull>
<!--mybatis(1)-->
<if test="stringProperty != null">
조건문
</if>
<!--mybatis(2)-->
<choose>
<when test="stringProperty == null">
NULL일경우 조건문
</when>
<otherwise>
NULL이 아닐경우 조건문
</otherwise>
</choose>
공백일경우 비교문
if(stringProperty.equals("")) {
//code here
}
<!--ibatis-->
<isEmpty property="stringProperty">
조건문
</isEmpty>
<!--mybatis-->
<if test="stringProperty == ''">
조건문
</if>
공백이 아닐경우 비교문
if(!stringProperty.equals("")) {
//code here
}
<!--ibatis-->
<isNotEmpty property="stringProperty">
조건문
</isNotEmpty>
<!--mybatis(1)-->
<if test="stringProperty != ''">
조건문
</if>
<!--mybatis(2)-->
<choose>
<when test="stringProperty == ''">
공백일경우 조건문
</when>
<otherwise>
공백 아닐경우 조건문
</otherwise>
</choose>
숫자비교문(1)
if(numberProperty > 5) {
//code here
}
<!--ibatis-->
<isGreaterThan property="numberProperty" compareValue="5">
5보다 클경우 조건문
</isGreaterThan>
<!--mybatis-->
<if test='id > 5'>
5보다 클경우 조건문
</if>
숫자비교문(2)
if(numberProperty >= 5) {
//code here
}
<!--ibatis-->
<isGreaterEqual property="numberProperty" compareValue="5">
5보다 크거나같을경우 조건문
</isGreaterEqual>
<!--mybatis-->
<if test='id >= 5'>
5보다 크거나같을경우 조건문
</if>
숫자비교문(3)
if(numberProperty < 5) {
//code here
}
<!--ibatis-->
<isLessThan property="numberProperty" compareValue="5">
5보다 작을경우 조건문
</isLessThan>
<!--mybatis-->
<if test='id < 5'>
5보다 작을경우 조건문
</if>
숫자비교문(4)
if(numberProperty <= 5) {
//code here
}
<!--ibatis-->
<isLessEqual property="numberProperty" compareValue="5">
5보다 작거나같을경우 조건문
</isLessEqual>
<!--mybatis-->
<if test='id <= 5'>
5보다 작거나같을경우 조건문
</if>
문자열 비교(1)
if(stringProperty.equals("title")) {
//code here
}
<!--ibatis-->
<isEqual property="stringProperty" compareValue="title">
stringProperty 속성값이 "title"일 경우
</isEqual>
<!--mybatis-->
<if test='stringProperty == "title"'>
stringProperty 속성값이 "title"일 경우
</if>
문자열 비교(2)
if(!stringProperty.equals("title")) {
//code here
}
<!--ibatis-->
<isNotEqual property="stringProperty" compareValue="title">
stringProperty 속성값이 "title"이 아닐경우
</isNotEqual>
<!--mybatis-->
<if test='stringProperty != "title"'>
stringProperty 속성값이 "title"이 아닐경우
</if>