mybatis 使用ognl

@Ognl @isNotEmpty() for String,Array,Collection,Map Test

now, test empty string test. must use:

<if test="userId != null && ''.equals(userId)">
and user_id = #{userId}
</if>

but, OGNL support Calling Static Methods.
http://www.opensymphony.com/ognl/html/LanguageGuide/staticMethods.html

so, we can use this for test empty String,Array,Collection,Map.
look like this:

<if test="@Ognl @isNotEmpty(userId)">
        and user_id = #{userId}
</if>




source code:

public class Ognl {

/**
* test for Map,Collection,String,Array isEmpty
* @param o
* @return
*/
public static boolean isEmpty(Object o) throws IllegalArgumentException {
if(o == null) return true;

if(o instanceof String) {
if(((String)o).length() == 0){
return true;
}
} else if(o instanceof Collection) {
if(((Collection)o).isEmpty()){
return true;
}
} else if(o.getClass().isArray()) {
if(Array.getLength(o) == 0){
return true;
}
} else if(o instanceof Map) {
if(((Map)o).isEmpty()){
return true;
}
}else {
return false;
}

return false;
}

/**
* test for Map,Collection,String,Array isNotEmpty
* @param c
* @return
*/
public static boolean isNotEmpty(Object o) {
return !isEmpty(o);
}
}


and will put this class to "default package".

 

要把ognl.java文件放到default package下,否则会报错。。。

错误:ibatis Method "isNotEmpty" failed for object Ognl [java.lang.ClassNotFoundException: java.lang.Ognl]

你可能感兴趣的:(java,ibatis,Ognl)