JAVA

Mybatis null check java method로 처리하기

Developer Garam.Choi 2020. 8. 25. 12:39

 

 

회사 내부에서 mybatis를 사용하다보면, 

 

 

<if test="p.param != null and p.param  != ''">

 

와 같은 문구를 많이 본다.

 

이에 대해서 조금더 명확한 방법으로 처리 할 수 있을지 하여 찾아보는 도중, 

 

java method를 호출하여 처리하는 방안이 있어서 이를 활용하기로 하였다.

 

package mybatis;

import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;

public class MybatisEmpty {


    public static Boolean empty(Object obj){
        if (obj instanceof String) return obj == null || "".equals(obj.toString().trim());
        else if (obj instanceof List) return obj == null || ((List<?>) obj).isEmpty();
        else if (obj instanceof Map) return obj == null || ((Map<?, ?>) obj).isEmpty();
        else if (obj instanceof Object[]) return obj == null || Array.getLength(obj) == 0;
        else return obj == null;
    }


    public static Boolean notEmpty(Object obj){
        return !empty(obj);
    }


}

 위와 같이 처리 한 후 

 

<if test="@mybatis.MybatisEmpty@notEmpty(p.param)">

 

위와 같이 호출하면 하면 된다.