map和bean的相互转换

1.利用org.apache.commons.beanutils.BeanUtils工具类进行转换:

package edu.hrbeu.platform.modeling.common.util;

import java.util.Map;

/**
 * 
* @ClassName: MapTransformUtils 
* @Description: TODO(map和bean之间相互转换) 
* @date 2018年1月24日 下午5:03:19 
*
 */
public class MapTransformUtils {
    /**
     * 
    * @Title: mapToObject
    * @Description: TODO(map转换为bean)
    * @return T    返回类型
    * @param map
    * @param beanClass
    * @return
    * @throws Exception
     */
    public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) throws Exception {    
        if (map == null) {
            return null;
        }

        T obj = beanClass.newInstance();  
        org.apache.commons.beanutils.BeanUtils.populate(obj, map);  

        return obj;
    }    

    /**
     * 
    * @Title: objectToMap
    * @Description: TODO(bean转换为Map)
    * @return Map<?,?>    返回类型
    * @param obj
    * @return
     */
    public static Map<?, ?> objectToMap(Object obj) {  
        if(obj == null) {
             return null;   
        }
        return new org.apache.commons.beanutils.BeanMap(obj);  
    } 
}

2.使用reflect反射进行转换

package edu.hrbeu.platform.modeling.common.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

/**
 * 使用reflect进行转换
 */
public class MapTransformUtils1 {

    public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) throws Exception {
        if (map == null)
            return null;

        T obj = beanClass.newInstance();

        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            int mod = field.getModifiers();
            if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                continue;
            }

            field.setAccessible(true);
            field.set(obj, map.get(field.getName()));
        }

        return obj;
    }

    public static Map<String, Object> objectToMap(Object obj) throws Exception {
        if (obj == null) {
            return null;
        }

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

        Field[] declaredFields = obj.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            map.put(field.getName(), field.get(obj));
        }

        return map;
    }
}

3.使用Introspector进行转换

package edu.hrbeu.platform.modeling.common.util;

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

/**
 * 使用Introspector进行转换
 */
public class MapTransformUtils2 {

    public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) throws Exception {
        if (map == null)
            return null;

        T obj = beanClass.newInstance();

        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            Method setter = property.getWriteMethod();
            if (setter != null) {
                setter.invoke(obj, map.get(property.getName()));
            }
        }

        return obj;
    }

    public static Map<String, Object> objectToMap(Object obj) throws Exception {
        if (obj == null)
            return null;

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

        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (key.compareToIgnoreCase("class") == 0) {
                continue;
            }
            Method getter = property.getReadMethod();
            Object value = getter != null ? getter.invoke(obj) : null;
            map.put(key, value);
        }

        return map;
    }

}

猜你喜欢

转载自blog.csdn.net/tiantangdizhibuxiang/article/details/80575412