mybatis 使用ognl

匿名 (未验证) 提交于 2019-12-03 00:02:01

@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]

转载于:https://my.oschina.net/zhenghuazhi/blog/199121

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!