Java之——对象与Map之间的转化

  1. 原文:http://blog.csdn.net/l1028386804/article/details/72639407
  2. /**  
  3.  * 使用org.apache.commons.beanutils进行转换  
  4.  */    
  5. class A {    
  6.         
  7.     public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {      
  8.         if (map == null)    
  9.             return null;    
  10.     
  11.         Object obj = beanClass.newInstance();    
  12.     
  13.         org.apache.commons.beanutils.BeanUtils.populate(obj, map);    
  14.     
  15.         return obj;    
  16.     }      
  17.         
  18.     public static Map<?, ?> objectToMap(Object obj) {    
  19.         if(obj == null)    
  20.             return null;     
  21.     
  22.         return new org.apache.commons.beanutils.BeanMap(obj);    
  23.     }      
  24.         
  25. }    
  26.     
  27. /**  
  28.  * 使用Introspector进行转换  
  29.  */    
  30. class B {    
  31.     
  32.     public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {      
  33.         if (map == null)     
  34.             return null;      
  35.     
  36.         Object obj = beanClass.newInstance();    
  37.     
  38.         BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());      
  39.         PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();      
  40.         for (PropertyDescriptor property : propertyDescriptors) {    
  41.             Method setter = property.getWriteMethod();      
  42.             if (setter != null) {    
  43.                 setter.invoke(obj, map.get(property.getName()));     
  44.             }    
  45.         }    
  46.     
  47.         return obj;    
  48.     }      
  49.         
  50.     public static Map<String, Object> objectToMap(Object obj) throws Exception {      
  51.         if(obj == null)    
  52.             return null;        
  53.     
  54.         Map<String, Object> map = new HashMap<String, Object>();     
  55.     
  56.         BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());      
  57.         PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();      
  58.         for (PropertyDescriptor property : propertyDescriptors) {      
  59.             String key = property.getName();      
  60.             if (key.compareToIgnoreCase("class") == 0) {     
  61.                 continue;    
  62.             }    
  63.             Method getter = property.getReadMethod();    
  64.             Object value = getter!=null ? getter.invoke(obj) : null;    
  65.             map.put(key, value);    
  66.         }      
  67.     
  68.         return map;    
  69.     }      
  70.         
  71. }    
  72.     
  73. /**  
  74.  * 使用reflect进行转换  ---使用反射机制
  75.  */    
  76. class C {    
  77.     
  78.     public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {      
  79.         if (map == null)    
  80.             return null;      
  81.     
  82.         Object obj = beanClass.newInstance();    
  83.     
  84.         Field[] fields = obj.getClass().getDeclaredFields();     
  85.         for (Field field : fields) {      
  86.             int mod = field.getModifiers();      
  87.             if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){      
  88.                 continue;      
  89.             }      
  90.     
  91.             field.setAccessible(true);      
  92.             field.set(obj, map.get(field.getName()));     
  93.         }     
  94.     
  95.         return obj;      
  96.     }      
  97.     
  98.     public static Map<String, Object> objectToMap(Object obj) throws Exception {      
  99.         if(obj == null){      
  100.             return null;      
  101.         }     
  102.     
  103.         Map<String, Object> map = new HashMap<String, Object>();      
  104.     //通过获取对象中的字段集合,来遍历此集合获取字段对应的值
  105.         Field[] declaredFields = obj.getClass().getDeclaredFields();      
  106.         for (Field field : declaredFields) {      
  107.             field.setAccessible(true);    
  108.             map.put(field.getName(), field.get(obj));    
  109.         }      
  110.     
  111.         return map;    
  112.     }     
  113. }    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值