Map与javaBean的互转

1.map转javaBean

    /**
     * map转换成javaBean
     * @param map
     * @return
     */
    public static Object transMap2Bean(Map<String,Object> map,Object obj){
        try{
            //1.获取bean信息
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor prop: properties) {
                String key = prop.getName();
                if(map.containsKey(key) && map.get(key) != null){
                    Object value = map.get(key);
                    Method setMethod = prop.getWriteMethod();
                    setMethod.invoke(obj,value);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return obj;
    }

2.javaBean转map

第一种办法:

    /**
     * javaBean转换成map
     * @param obj
     * @return
     */
    public static Map<String,Object> transBean2Map(Object obj){
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            //1.获取bean信息
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
            if(properties != null && properties.length>0){
                for (PropertyDescriptor prop :properties) {
                    //2.得到属性名
                    String name = prop.getName();
                    //3.过滤class属性
                    if(!"class".equals(name)){
                        //4.得到属性的get方法
                        Method getter = prop.getReadMethod();
                        //5.获取属性值
                        Object value = getter.invoke(obj);
                        //6.放入map中
                        map.put(name,value);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

第二种办法:

    public static Map<String,Object> transBean2Map2(Object obj){
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            Field[] fields = obj.getClass().getDeclaredFields();
            if (fields != null && fields.length > 0){
                for (Field field : fields) {
                    int a = field.getModifiers();
                    System.out.println(a);
                    //当属性的修饰符为private时,需要先setAccessible(true)
                    if (!field.isAccessible()){
                        field.setAccessible(true);
                    }
                    Object value = field.get(obj);
                    map.put(field.getName(),value);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return map;
    }

3.利用BeanMap转map的方式

速度快,提供map的接口操作对象

  • Object get(Object key)
  • Object put(Object key, Object value)
  • void putAll(Map t)
  • Set entrySet()
  • Collection values()
  • boolean containsKey(Object key)
    ……
    /**
     * 将map装换为javabean对象
     * @param map
     * @param bean
     * @return
     */
    public static <T> T mapToBean(Map<String, Object> map,T bean) {
        BeanMap beanMap = BeanMap.create(bean);
        beanMap.putAll(map);
        return bean;
    }
    /**
     * bean转Map
     * @param <T>
     * @return
     */
    public static <T> Map<String,Object> bean2Map(T bean){
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            if (bean != null){
                BeanMap beanMap = BeanMap.create(bean);
                map.putAll(beanMap);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容