java:获取对象/实体类的全部字段_倔强的姑娘^o^的博客-CSDN博客_java获取类的所有字段

Number 1

public <T> T compareObjUtil(CompareSettlementDto t1) throws IllegalAccessException {        Map<String, Object> map = new HashMap<>();        Field[] objFields = t1.getClass().getDeclaredFields();//字段信息集合        Field.setAccessible(objFields, true);        if (objFields != null && objFields.length > 0) {            for (int i = 0; i < objFields.length; i++) {                String objName = objFields[i].getName();//字段名称                Object promptValue = objFields[i].get(t1);//字段值                String valueType = objFields[i].getType().toString();//字段类型             }        }        return (T) map;    }

Number 2

public static Map<String, Object> entityToMap(Object bean) throws Exception {        Class type = bean.getClass();        Map<String,Object> returnMap = new HashMap<>();        BeanInfo beanInfo = Introspector.getBeanInfo(type);        PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();        for (PropertyDescriptor descriptor : propertyDescriptors) {            String propertyName = descriptor.getName();//name            if (!propertyName.equals("class")) {                Method readMethod = descriptor.getReadMethod();                Object result = readMethod.invoke(bean);//value                String typeName = descriptor.getPropertyType().getName();//type            }        }        return returnMap;    }

原网址: 访问
创建于: 2022-11-16 09:45:14
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论