map集合和javabean对象之间相互转换方法

本文介绍两种高效转换JavaBean对象与Map集合的方法:使用反射和Introspector。通过实例代码展示了如何将Map转换为JavaBean,以及如何将JavaBean对象转换为Map集合,适用于各种Java开发场景。

亲测可用, 简单易读

1、使用反射 

/**
 * 使用reflect进行转换,map集合转javabean
 *
 * @param map
 * @param beanClass
 * @return
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws IllegalAccessException, InstantiationException {
    if (map == null){
        return null;
    }
        
    Object 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;
}
 
/**
 * 使用reflect进行转换,javabean对象转map集合
 *
 * @param obj
 * @return map集合
 * @throws IllegalAccessException java.lang.IllegalAccessException
 * @throws InstantiationException java.lang.InstantiationException
 */
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;
}

 

2、使用Introspector(推荐)

/**
 * 使用Introspector,map集合成javabean
 *
 * @param map       map
 * @param beanClass bean的Class类
 * @return bean对象
 */
public static <T> T mapToBean(Map<String, Object> map, Class<T> beanClass) {
 
    if (MapUtils.isEmpty(map)) {
        return null;
    }
 
    try {
        T t = beanClass.newInstance();
 
        BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            Method setter = property.getWriteMethod();
            if (setter != null) {
                setter.invoke(t, map.get(property.getName()));
            }
        }
        return t;
    } catch (Exception ex) {
        log.error("########map集合转javabean出错######,错误信息,{}", ex.getMessage());
        throw new RuntimeException();
    }
 
}
 
/**
 * 使用Introspector,对象转换为map集合
 *
 * @param beanObj javabean对象
 * @return map集合
 */
public static Map<String, Object> beanToMap(Object beanObj) {
 
    if (null == beanObj) {
        return null;
    }
 
    Map<String, Object> map = new HashMap<>();
 
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(beanObj.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(beanObj) : null;
            map.put(key, value);
        }
 
        return map;
    } catch (Exception ex) {
        log.error("########javabean集合转map出错######,错误信息,{}", ex.getMessage());
        throw new RuntimeException();
    }
}

 

原贴地址:https://blog.csdn.net/hanxue6898/article/details/81187104

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值