Bean與Map的轉換 和 Map與Bean的轉換


package com.JUtils.beanConvert;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Bean與Map的轉換
 *
 * @since 1.0.0
 */
public class BeanMapConvert {
    /**
     * Bean轉換為Map
     *
     * @param
     * @return String-Object的HashMap
     * @since v1.0.0
     */

    public static void main(String[] args) {
        
        //Bean轉換為Map
        Berth b = new Berth("45", 55d, 65d, 6565d, 63d, 9666l, 9656l, "fgj");
        Map<String, Object> stringObjectMap = bean2MapObject(b);
        System.out.println("Bean轉換為Map  "+stringObjectMap);


        //Map轉換為Bean
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("berthId","berthId");
        map.put("coastBearing",6556d);
        map.put("faceBearing",5645d);
        map.put("lat",6896868d);
        map.put("lon",6868868d);
        map.put("ppdAta",525l);
        map.put("sldAta",66666l);
        map.put("type","fdgfd");
        Berth nn = new Berth();
        Object o = map2Bean(map, nn);
        System.out.println("Map轉換為Bean  "+o);

    }
    public static Map<String,Object> bean2MapObject(Object object){
        if(object == null){
            return null;
        }

        Map<String, Object> map = new HashMap<String, Object>();
        try {

            //Introspector類:
            //  將JavaBean中的屬性封裝起來進行操作。在程序把一個類當做JavaBean來看,
            // 就是調用Introspector.getBeanInfo()方法,得到的BeanInfo對象封裝了把這個類當做JavaBean的結果信息,即屬性的信息。


            BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 過濾class屬性
                if (!key.equals("class")) {
                    // 得到property對應的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(object);

                    map.put(key, value);
                }
            }
        } catch (Exception e) {
           e.printStackTrace();
        }

        return map;
    }

    /**
     * Map轉換為Java Bean
     *
     * @param map
     *              待轉換的Map
     * @param object
     *              Java Bean
     * @return java.lang.Object
     */
    public static Object map2Bean(Map map,Object object){
        if(map == null || object == null){
            return null;
        }
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                if (map.containsKey(key)) {
                    Object value = map.get(key);
                    // 得到property對應的setter方法
                    Method setter = property.getWriteMethod();
                    setter.invoke(object, value);
                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return object;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM