java 判断对象的属性是否为空(对象的属性非空判断)

我以一个简单的实例去实现,具体实现看service层以及去看util类

entity:

public class WwDept {

    private Integer wwDeptId;
    private String wwDeptName;
    private Integer wwDeptParentid;

}

service:

public Map addDept(WwDept dept) {

    //查询出对象所有的属性
    Field[] fields = dept.getClass().getDeclaredFields();
    //用于判断所有属性是否为空,如果参数为空则不查询
    boolean flag = false;
    for (Field field : fields) {
        //不检查 直接取值
        field.setAccessible(true);
        try {
            if (StringUtils.isNotNull(field.get(dept))) {
                //不为空
                flag = true;
                //当有任何一个参数不为空的时候则跳出判断直接查询
                break;
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    if (flag == true) {
        return MapUtils.getMap("fail");
    } else {
        if (deptMapper.addDept(dept) > 0) return MapUtils.getMap("success");
        return MapUtils.getMap("fail");
    }
}

util:

public class PropertyUtils {

    public static boolean isNull(Object[] objs) {
        if (objs == null || objs.length == 0)
            return true;
        return false;
    }
    public static boolean isNull(Object obj) {
        if (obj == null || isNull(obj.toString())){
            return true;
        }
        return false;
    }

    public static boolean isNull(Integer integer) {
        if (integer == null || integer == 0)
            return true;
        return false;
    }

    public static boolean isNull(Collection collection) {
        if (collection == null || collection.size() == 0)
            return true;
        return false;
    }

    public static boolean isNull(Map map) {
        if (map == null || map.size() == 0)
            return true;
        return false;
    }

    public static boolean isNull(String str) {
        return str == null || "".equals(str.trim())
            || "null".equals(str.toLowerCase());
    }

    public static boolean isNull(Long longs) {
        if (longs == null || longs == 0)
            return true;
        return false;
    }

    public static boolean isNotNull(Long longs) {
        return !isNull(longs);
    }

    public static boolean isNotNull(String str) {
        return !isNull(str);
    }

    public static boolean isNotNull(Collection collection) {
        return !isNull(collection);
    }

    public static boolean isNotNull(Map map) {
        return !isNull(map);
    }

    public static boolean isNotNull(Integer integer) {
        return !isNull(integer);
    }

    public static boolean isNotNull(Object[] objs) {
        return !isNull(objs);
    }
    public static boolean isNotNull(Object obj) {
        return !isNull(obj);
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值