Map與javaBean的互轉(zhuǎn)

1.map轉(zhuǎn)javaBean

    /**
     * map轉(zhuǎn)換成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轉(zhuǎn)map

第一種辦法:

    /**
     * javaBean轉(zhuǎn)換成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轉(zhuǎn)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轉(zhuǎn)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;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容