实体类转换Map Map转换实体类

  1. public class JavaBeanUtil {
  2.     private static Logger logger = LoggerFactory.getLogger(JavaBeanUtil.class);


  3.     /**
  4.      * 实体类转map
  5.      * @param obj
  6.      * @return
  7.      */
  8.     public static Map<String, Object> convertBeanToMap(Object obj) {
  9.         if (obj == null) {
  10.             return null;
  11.         }
  12.         Map<String, Object> map = new HashMap<String, Object>();
  13.         try {
  14.             BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
  15.             PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  16.             for (PropertyDescriptor property : propertyDescriptors) {
  17.                 String key = property.getName();
  18.                 // 过滤class属性
  19.                 if (!key.equals("class")) {
  20.                     // 得到property对应的getter方法
  21.                     Method getter = property.getReadMethod();
  22.                     Object value = getter.invoke(obj);
  23.                     if(null==value){
  24.                         map.put(key,"");
  25.                     }else{
  26.                         map.put(key,value);
  27.                     }
  28.                 }


  29.             }
  30.         } catch (Exception e) {
  31.             logger.error("convertBean2Map Error {}" ,e);
  32.         }
  33.         return map;
  34.     }


  35.     /**
  36.      * map 转实体类
  37.      * @param clazz
  38.      * @param map
  39.      * @param <T>
  40.      * @return
  41.      */
  42.     public static <T> T convertMapToBean(Class<T> clazz, Map<String,Object> map) {
  43.         T obj = null;
  44.         try {
  45.             BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
  46.             obj = clazz.newInstance(); // 创建 JavaBean 对象


  47.             // 给 JavaBean 对象的属性赋值
  48.             PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  49.             for (int i = 0; i < propertyDescriptors.length; i++) {
  50.                 PropertyDescriptor descriptor = propertyDescriptors[i];
  51.                 String propertyName = descriptor.getName();
  52.                 if (map.containsKey(propertyName)) {
  53.                     // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
  54.                     Object value = map.get(propertyName);
  55.                     if ("".equals(value)) {
  56.                         value = null;
  57.                     }
  58.                     Object[] args = new Object[1];
  59.                     args[0] = value;
  60.                     descriptor.getWriteMethod().invoke(obj, args);


  61.                 }
  62.             }
  63.         } catch (IllegalAccessException e) {
  64.             logger.error("convertMapToBean 实例化JavaBean失败 Error{}" ,e);
  65.         } catch (IntrospectionException e) {
  66.             logger.error("convertMapToBean 分析类属性失败 Error{}" ,e);
  67.         } catch (IllegalArgumentException e) {
  68.             logger.error("convertMapToBean 映射错误 Error{}" ,e);
  69.         } catch (InstantiationException e) {
  70.             logger.error("convertMapToBean 实例化 JavaBean 失败 Error{}" ,e);
  71.         }catch (InvocationTargetException e){
  72.             logger.error("convertMapToBean字段映射失败 Error{}" ,e);
  73.         }catch (Exception e){
  74.             logger.error("convertMapToBean Error{}" ,e);
  75.         }
  76.         return (T) obj;
  77.     }


  78. }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值