DESC

내가 보려고 쓰는 블로그

«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Today
-
Yesterday
-
Total
-
  • Mybatis < foreach > 요소로 동적 쿼리 작성
    JAVA 2022. 12. 14. 15:33
    반응형

    1. 호출에 사용할 Request Dto 작성

     HaileyReqDto.java 

    package com.hailey.dto.request;
    
    import java.util.List;
    
    public class HaileyReqDto {
    
      private String  rid;
      private List    ridList; // foreach 에 사용할 변수
      private String  loginId;
        
      public String getRid() {
        return rid;
      }
      public void setRid(String rid) {
        this.rid = rid;
      }
      public List getRidList() {
        return ridList;
      }
      public void setRidList(List ridList) {
        this.ridList = ridList;
      }
      public List getLoginId() {
        return loginId;
      }
      public void setLoginId(List loginId) {
        this.loginId = loginId;
      }
    
    }

     

    2. Mybatis 쿼리 xml 파일 작성

      HaileyMapper.xml 

    <update id="updateData" parameterType="com.hailey.dto.request.HaileyReqDto">
      /* HaileyMapper.updateData */
      UPDATE tb_hailey_data  a
      SET 
          a.modify_date   = SYSDATE
        , a.modify_by     = #{loginId}
          
      WHERE  a.flag = 1
        <if test=" @com.hailey.common.util.CommonUtil@isNotEmpty( ridList ) " >
          AND a.rid IN
          <foreach collection="ridList" item="targetRid" index="index" separator="," open="(" close=")">
            #{targetRid}
          </foreach>
        </if>
        <if test=" @com.hailey.common.util.CommonUtil@isEmpty( ridList ) " >
          AND a.rid = #{rid}
        </if>
    </update>

    collection 명은 DTO에 리스트 형식으로 선언되어있어야 하고, item, index 의 변수명은 DTO와 무관하게 임의로 지정하면 된다. separator, open, close 의 경우 필요에 따라 생략가능하다. 

     

    - 참고 : 위 if 문에 사용된 UTIL 소스

    아래와 같이 static 함수를 선언해두면 위의 쿼리에서 처럼   @  키워드를 사용하여 호출할 수 있다.

    if 문 외에도 아래 method 의 경우에는 해당사항이 없지만  ${@com.hailey.common.util.CommonUtil@isNotEmpty(param)}  와 같이 파라미터를 java method 를 활용하여 가공하여 쿼리에 심을 수 있다. ( string parameter   # { } 아니고   $  )

      

      CommonUtil.java 

    package com.hailey.common.util;
    
    import java.util.Collection;
    import java.util.Map;
    
    import java.lang.reflect.Array;
    
    
    public class CommonUtil {
      
      public static boolean isNotEmpty(Object object) {
    
        return !isEmpty(object);
      }
    
      @SuppressWarnings("unchecked")
      public static boolean isEmpty(Object object) {
        if (object == null) {
          return true;
        }
    
        if (object instanceof String) {
          String str = (String) object;
          return str.length() == 0;
        }
    
        if (object instanceof Collection) {
          Collection collection = (Collection) object;
          return collection.size() == 0;
        }
        
        if (object instanceof Map) {
          Map map = (Map) object;
          return map.isEmpty();
        }
        
        if (object.getClass().isArray()) {
          try {
            if (Array.getLength(object) == 0) {
              return true;
            }
          } catch (IllegalArgumentException e) {
            e.printStackTrace();
          }
        }
    
        return false;
      }
    
    }

     

    반응형

    댓글

Customed By Hailey Gong.