[Mybatis] 동적쿼리, 관계연산자, if, choose, foreach, set

*테이블 명이나 컬럼명이 동적인 경우

 - PreparedStatement에서는 테이블명이나 컬럼명 bind변수( ? )로 처리할 수 없다.

 - MyBatis에서는  #{ 이름 }을 사용하면 해당위치가 bind변수로 치환된다.

 - ${ 이름 } 을 사용하면 bind변수가 아닌 쿼리문에 입력값이 직접 생성된다.\

)

  <select>

    select 컬럼명,,,

    from  #{  이름  }

  </select>

해결)

 <select>

    select 컬럼명,,,

    from  ${  이름  }

  </select>

 <select>

    select 컬럼명,,,

    from  테이블명

    where 컬럼명=${  이름  }

  </select>

 

*동적쿼리

 - 상황에 따라 다른 쿼리문이 만들어져서 실행 되는 것.

 - OGNL (Object Graph Navigation Language ) : 노드를 사용하여 노드에 일치하는 언어의

   문법을 찾아서 실행하는 언어.

  <if test=“조건식”>    =>  if( 조건식 ){

 

parameterType 속성에 선언된 값 ( 기본형, Wrapper Class, String, DTO, VO)

getter method명 또는 입력값  연산자  비교할 값

 

-관계연산자

 > (gt), < (lt) , >= (gte), <= ( lte ), == ( eq ), != (neq)

-논리연산자

  && -  기호로 직접 사용할 수 없고, and로만 사용가능

    || -  기호, or 둘다 사용가능

 

  -where, if, choose, foreach, set 등을 지원

 

 *if) - 비교할 때 사용.

 문법)

  <if test=“조건식”>

   조건에 맞을 때 실행될 문장 들

  </if>

  *<where>

    : where절을 동적을 붙일 때 사용

  

*)

   부서번호가 입력되면 부서번호에 해당하는 사원을 검색.

   부서번호가 입력되지 않으면 모든 사원을 검색.

 <select id=“” resultType=“” paramterType=“DTO”>

   select 컬럼명,,,,

   from  테이블명

   <where>

   <if test=“deptno neq 0”>

    deptno=#{ deptno }

  </if>

   </where>

 </select>

 

*choose

 - 연관된 조건을 비교할 때 사용.

문법)

 <choose>

   <when test=“조건식”>

     조건에 맞을 때 수행될 문장들….

   </when>

  <when test=“조건식”>

     조건에 맞을 때 수행될 문장들….

   </when>

    .

   <otherwise>

      모든 조건에 맞지 않을 때 수행될 문장들..

   </otherwise>

  </choose>

   

.

입력값이 1이면 10번 부서의 모든 사원을 조회

 2라면 20번 부서의 모든 사원을 조회

그렇지 않으면 30번 부서의 모든 사원을 조회

 

문법)

<select id=“” resultType=“” parameterType=“int”>

select 사원정보,,,,,,

from  테이블명

<where>

 <choose>

   <when test=“num eq 1”>

    deptno=10

   </when>

  <when test=“num eq 2”>

   deptno=20

   </when>

   <otherwise>

     deptno=30  

   </otherwise>

  </choose>

  </where> 

 

*foreach

 - 값을 반복시켜 쿼리문을 만들 때.

 - 값은 java.util.List 또는 배열에 들어가고 Map을 통해서 MyBatis로 전달.

select * from EMP

where empno in (7521, 7782, 7844, 7876 );

 Map<String,Object> map=new HashMap<String,Object>();

 map.put(“empnoArr”, request.getParameterValues(“이름”) );

문법)

<foreach collection=“map   open=“시작기호” item=“변수명” separator=“구분자” close=“닫힘기호”>

   #{ 변수명 }

</foreach>

 동작)

 open 속성이 한번 출력되고, item속성과 separator속성이 반복 출력되고, close속성이

    마지막으로 한번만 출력된다..

)

 List<Integer> list=new ArrayList<Integer>();

  list.add(7521);   list.add( 7782); list.add(7844); list.add(7876);

  map.put(“list”, list);

 <foreach collection=“list”  open=“(” item=“empno” separator=“,” close=“)”>

   #{empno }

</foreach>

 

*set

 - update시에 사용

 - 동적으로 set이 설정된다.

문법)

 <update id=“아이디parametetType=“XxxxDTO”>

   update 테이블명

   <set>

     <if test=“조건”>

     변경할 컬럼명=#{ 이름 },

     </if>

     <if test=“조건”>

     변경할 컬럼명=#{ 이름 },

     </if> 

     <if test=“조건”>

      변경할 컬럼명=#{ 이름 },

      </if>

   </set>

   where  조건컬럼명=#{ getter method }

 </update

'MyBatis' 카테고리의 다른 글

[Mybatis] Procedure 호출  (0) 2025.08.31
[MyBatis] 구조, 설정, Handler, Mapper 사용  (0) 2025.08.24