• 분류 전체보기 (132)
    • 공지사항 (2)
    • 블로그팁 (4)
    • GKSkin (5)
      • 소개 (1)
      • 스킨다운로드 (1)
      • 사용법 (3)
    • GKTool (4)
      • 소개 (1)
      • 시연영상 (3)
    • 코드저장소 (41)
      • javascript (2)
      • jQuery 플러그인 (7)
      • java (12)
      • sql (10)
      • mybatis(ibatis) (3)
      • 스마트에디터연동 (3)
      • 다음에디터연동 (2)
      • 샘플링답변 (2)
    • 웹개발강좌 (48)
      • jQuery (8)
      • jQueryUI (5)
      • ExtJS (28)
      • 부트스트랩 (7)
    • 모바일웹강좌 (1)
      • SenchaTouch (1)
    • 개발에필요한연동법 (27)
      • 스프링연동 (16)
      • 리눅스서버구축 (11)
댓글
/100
2014. 11. 17. 13:30
mybatis와 ibatis별 동적태그 비교문 알아보도록 하자

우선 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>




마지막으로 다중 조건체크에 대한 예를 들어보도록 하겠습니다.




if(stringProperty != null && !stringProperty.equals("")) {
    //code here				
}


<!--ibatis-->
<isNotNull property="stringProperty">
	<isNotEmpty property="stringProperty">
	    조건문
        </isNotEmpty>
</isNotNull>
<!--mybatis-->
<if test='stringProperty != null and stringProperty == ""'>
	stringProperty 속성값이 "title"이 아닐경우
</if>


위처럼 ibatis의 경우에는 태그안에 태그를 여러번 넣어줘야해서 솔직히 번거롭습니다.

하지만 mybatis는 JSTL과 동일한 문법으로 사용이 가능하여 

편하게 비교문을 다중조건을 주어서 사용할 수 있습니다.


다만 다른 점이 있다면

&& 문을 and || 문을 or 로 작성 해주셔야 합니다.


by 개발로짜




저작자표시 비영리 변경금지 (새창열림)

'코드저장소 > mybatis(ibatis)' 카테고리의 다른 글

Spring3 + Mybatis 연동 후 DBMS 종류별(MySQL,MSSQL,Oracle) 데이터 insert 후 시퀀스 가져오기  (0) 2014.11.19
mybatis와 ibatis 반복문 비교하기(foreach vs iterate)  (0) 2014.11.17

티스토리툴바