Map和Bean互相转化

import org.apache.commons.beanutils.BeanUtils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.*;
import java.util.Map.Entry;

/**
 * Description	Map和Bean互相转化
 * @author 19040838
 * @date 2019年8月7日
 */
public class ConvertUtils {
	/**
	 * Description	将 Map对象转化为JavaBean
	 * @param map
	 * @throws Exception 
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
    public static <T> T convertMapTOBean(Map<String, Object> map, Class<T> T){
        if (map == null || map.size() == 0) {
            return null;
        }
        T bean = null;
        try {
            // 获取map中所有的key值,全部更新成大写,添加到keys集合中
            Object mvalue = null;
            Map<String, Object> newMap = new HashMap<>();
            Iterator<Entry<String, Object>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                String key = it.next().getKey();
                mvalue = map.get(key);
                newMap.put(key.toUpperCase(Locale.US), mvalue);
            }

            BeanInfo beanInfo = Introspector.getBeanInfo(T);
            bean = T.newInstance();
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (int i = 0, n = propertyDescriptors.length; i < n; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = descriptor.getName();
                String upperPropertyName = propertyName.toUpperCase();

                if (newMap.keySet().contains(upperPropertyName)) {
                    Object value = newMap.get(upperPropertyName);
                    // 这个方法不会报参数类型不匹配的错误。
                    BeanUtils.copyProperty(bean, propertyName, value);
                }
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return bean;
    }
    
    /**
     * 	实体类转换为Map对象
     * 	功能描述: <br>
     * 	〈功能详细描述〉
     *
     * @param obj
     * @return
     * @throws Exception
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @SuppressWarnings("rawtypes")
	public static Map<String, Object> convertObjToMap(Object obj){
        Map<String, Object> reMap = new HashMap<String, Object>();
        if (obj == null){
            return null;
        }
        List<Field> fields = new ArrayList<Field>();
        List<Field> childFields;
        List<String> fieldsName = new ArrayList<String>();
        Class tempClass = obj.getClass();
        // 当父类为null的时候说明到达了最上层的父类(Object类).
        while (tempClass != null) {
            fields.addAll(Arrays.asList(tempClass.getDeclaredFields()));
            tempClass = tempClass.getSuperclass(); 
        }
        childFields = Arrays.asList(obj.getClass().getDeclaredFields());
        for (Field field : childFields) {
            fieldsName.add(field.getName());
        }
        try {
            // 循环转换
            for (Field field : fields) {
                if (fieldsName.contains(field.getName())) {
                    Field f = obj.getClass().getDeclaredField(field.getName());
                    f.setAccessible(true);
                    Object o = f.get(obj);
                    reMap.put(field.getName(), o);
                } else {
                    // 获取父类字段
                    Field f = obj.getClass().getSuperclass().getDeclaredField(field.getName());
                    f.setAccessible(true);
                    Object o = f.get(obj);
                    reMap.put(field.getName(), o);
                }
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return reMap;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软软的铲屎官

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值