首页 > 代码库 > 判断对象的属性值是否为null

判断对象的属性值是否为null

判断对象的属性值是否为null

核心处理:

    private Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(o, new Object[] {});
            return value;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

 

运用示例:

        DoctorVo doctorVo = new DoctorVo();
        // doctorVo.setId(3942);
        Object id = this.getFieldValueByName("id", doctorVo);
        if (null != id) {
            bdId = doctorVo.getId();
            logger.info("传递了医生ID,doctorId = " + bdId);
        }

 

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Monaco; min-height: 21.0px } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Monaco } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Monaco; color: #4e9072 } span.s1 { color: #7e504f } span.s2 { color: #931a68 } span.s3 { color: #000000 } span.Apple-tab-span { white-space: pre }</style>

判断对象的属性值是否为null