javabean实体类对象转为Map类型对象的方法

/ 0评 / 0

javabean实体类对象转为Map类型对象

public static Map<String, Object> beanToMap(Object obj) {
         Map<String, Object> params = new HashMap<String, Object>(0);
         try {
             PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
             PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj);
             for (int i = 0; i < descriptors.length; i++) {
                 String name = descriptors[i].getName();
                 if (!"class".equals(name)) {
                     params.put(name, propertyUtilsBean.getNestedProperty(obj, name));
                 }
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
         return params;
}

需要的依赖

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>


        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.2</version>
        </dependency>

发表评论

电子邮件地址不会被公开。 必填项已用*标注