언어/Ibatis

[ibatis + MySQL] 동적쿼리 - isNotEmpty 사용법

antoroong 2025. 2. 23. 23:39

ibatis의 동적쿼리에 대해서 알아보자

어떤 변수가 있는지 없는지를 확인하여

출력하는 기능이다. 나는 where절 밑 조건문에 사용했다.

java에서 원하는 조건에 따라 dp_code를 set해서 보내면

 

				String dp_code = (String) session.get("DR_CODE");

				//DR1001 = 교사  , DR1002 = 의사 dr_code에 set하여 ibatis 쿼리로 보낸다
				if(dr_code.equals("DR1001") || dr_code.equals("DR1002")) {
					nbean.setDr_code(dr_code);
				}else {
						//나머지는 set하지 않을거임
				}

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" 
"http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="member">
	<typeAlias alias="memberBean" type="model.member.bean.MemberBean" />

<select id="getMemberList" parameterClass="memberBean" resultClass="memberBean">
	/*getMemberList 리스트 출력*/
	
		SELECT
				a.member_type,
				a.dr_code,
		FROM 
				tb_member a
		 WHERE
				 a.del_flag='N'
		<isNotEmpty property="dr_code">
			AND a.dr_code = #dr_code#
		</isNotEmpty>		
		ORDER BY a.member_seq DESC
		LIMIT #startData#,#endData#
		
	</select>
</sqlMap>

 

 

dr_code를 보냈을때에 property로 dr_code를 받아

isNotEmpty가 있는지 확인을 한다.

dr_code라는 property를 받는다면

AND a.dr_code = #dr_code# 조건문 실행

dr_code라는 property를 받지 않는다면

조건문은 실행하지 않고 나온다