mybatis OGNL应用

mybatis中使用ognl的扩展,实现判断传入的字段:

Mapper文件中:

[sql]  view plain  copy
  1. <if test="@Ognl@isNotEmpty(id)">  
  2.                 AND id = #{id}  
  3.             </if>  
  4.            <if test="@Ognl@isNotEmpty(createrTime)">  
  5.                 AND createrTime >= #{createrTime}  
  6.            </if>  

  Ognl.java文件:

[java]  view plain  copy
  1. public class Ognl {  
  2.   
  3.     /** 
  4.      * 可以用于判断 Map,Collection,String,Array是否为空 
  5.      *  
  6.      * @param o 
  7.      * @return 
  8.      */  
  9.     @SuppressWarnings("unchecked")  
  10.     public static boolean isEmpty(Object o) throws IllegalArgumentException {  
  11.         if (o == null)  
  12.             return true;  
  13.         if (o instanceof String) {  
  14.             return StringUtils.isEmpty((String) o);  
  15.         } else if (o instanceof Collection) {  
  16.             return CollectionUtils.isEmpty((Collection) o);  
  17.         } else if (o.getClass().isArray()) {  
  18.             return ArrayUtils.isEmpty((Object[]) o);  
  19.         } else if (o instanceof Map) {  
  20.             MapUtils.isEmpty((Map) o);  
  21.         } else if (o instanceof Date) {  
  22.             return o == null;  
  23.         } else if (o instanceof Number) {  
  24.             return o == null;  
  25.         } else if (o instanceof Boolean) {  
  26.             return o == null;  
  27.         } else {  
  28.             throw new IllegalArgumentException("Illegal argument type,must be : Map,Collection,Array,String. but was:"  
  29.                     + o.getClass());  
  30.         }  
  31.   
  32.         return false;  
  33.     }  
  34.   
  35.     /** 
  36.      * 可以用于判断 Map,Collection,String,Array是否不为空 
  37.      *  
  38.      * @param c 
  39.      * @return 
  40.      */  
  41.     public static boolean isNotEmpty(Object o) {  
  42.         return !isEmpty(o);  
  43.     }  
  44.   
  45.     public static boolean isNotEmpty(Object... objects) {  
  46.         if (objects == null)  
  47.             return false;  
  48.         for (Object obj : objects) {  
  49.             if (isEmpty(obj)) {  
  50.                 return false;  
  51.             }  
  52.             ;  
  53.         }  
  54.         return true;  
  55.     }  
  56.   
  57.     public static boolean isNotBlank(Object o) {  
  58.         return !isBlank(o);  
  59.     }  
  60.   
  61.     public static boolean isNumber(Object o) {  
  62.         if (o instanceof Number) {  
  63.             return true;  
  64.         } else if (o instanceof String) {  
  65.             return NumberUtils.isNumber((String) o);  
  66.         } else {  
  67.             return false;  
  68.         }  
  69.     }  
  70.   
  71.     public static boolean isBlank(Object o) {  
  72.         return StringUtils.isBlank((String) o);  
  73.     }  
  74.   
  75.   
  76.   
  77.     public static boolean isBlank(String str) {  
  78.         return StringUtils.isBlank(str);  
  79.     }  
  80.   
  81. }  
Ognl.java 必须放在class目录,也就是没有包名。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值