Java 中 Map与JavaBean实体类之间的相互转化

    /**  

  1.      * 将一个 JavaBean 对象转化为一个  Map  
  2.      * @param bean 要转化的JavaBean 对象  
  3.      * @return 转化出来的  Map 对象  
  4.      * @throws IntrospectionException 如果分析类属性失败  
  5.      * @throws IllegalAccessException 如果实例化 JavaBean 失败  
  6.      * @throws InvocationTargetException 如果调用属性的 setter 方法失败  
  7.      */    
  8. @SuppressWarnings({ "rawtypes", "unchecked" })    
  9. public static Map convertBean(Object bean)    
  10. throws IntrospectionException, IllegalAccessException, InvocationTargetException {    
  11.         Class type = bean.getClass();    
  12.         Map returnMap = new HashMap();    
  13.         BeanInfo beanInfo = Introspector.getBeanInfo(type);    
  14.         PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();    
  15. for (int i = 0; i< propertyDescriptors.length; i++) {    
  16.             PropertyDescriptor descriptor = propertyDescriptors[i];    
  17.             String propertyName = descriptor.getName();    
  18. if (!propertyName.equals("class")) {    
  19.                 Method readMethod = descriptor.getReadMethod();    
  20.                 Object result = readMethod.invoke(bean, new Object[0]);    
  21. if (result != null) {    
  22.                     returnMap.put(propertyName, result);    
  23.                 } else {    
  24.                     returnMap.put(propertyName, "");    
  25.                 }    
  26.             }    
  27.         }    
  28. return returnMap;    
  29.     }  
  30. /**  
  31.      * 将一个 Map 对象转化为一个 JavaBean  
  32.      * @param type 要转化的类型  
  33.      * @param map 包含属性值的 map  
  34.      * @return 转化出来的 JavaBean 对象  
  35.      * @throws IntrospectionException 如果分析类属性失败  
  36.      * @throws IllegalAccessException 如果实例化 JavaBean 失败  
  37.      * @throws InstantiationException 如果实例化 JavaBean 失败  
  38.      * @throws InvocationTargetException 如果调用属性的 setter 方法失败  
  39.      */    
  40. @SuppressWarnings("rawtypes")    
  41. public static Object convertMap(Class type, Map map)    
  42. throws IntrospectionException, IllegalAccessException,    
  43.             InstantiationException, InvocationTargetException {    
  44.         BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性    
  45.         Object obj = type.newInstance(); // 创建 JavaBean 对象    
  46. // 给 JavaBean 对象的属性赋值    
  47.         PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();    
  48. for (int i = 0; i< propertyDescriptors.length; i++) {    
  49.             PropertyDescriptor descriptor = propertyDescriptors[i];    
  50.             String propertyName = descriptor.getName();    
  51. if (map.containsKey(propertyName)) {    
  52. // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。    
  53.                 Object value = map.get(propertyName);    
  54.                 Object[] args = new Object[1];    
  55.                 args[0] = value;    
  56.                 descriptor.getWriteMethod().invoke(obj, args);    
  57.             }    
  58.         }    
  59. return obj;    
  60.     }