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

在做导入的时候,遇到了需要将map对象转化 成javabean的问题,也就是说,不清楚javabean的内部字段排列,只知道map的 key代表javabean的字段名,value代表值。

那现在就需要用转化工具了。是通用的哦!

首先来看 JavaBean 转化成Map的方法:

 

[java]  view plain copy
 
  1. /** 
  2.      * 将一个 JavaBean 对象转化为一个  Map 
  3.      * @param bean 要转化的JavaBean 对象 
  4.      * @return 转化出来的  Map 对象 
  5.      * @throws IntrospectionException 如果分析类属性失败 
  6.      * @throws IllegalAccessException 如果实例化 JavaBean 失败 
  7.      * @throws InvocationTargetException 如果调用属性的 setter 方法失败 
  8.      */  
  9.     @SuppressWarnings({ "rawtypes""unchecked" })  
  10.     public static Map convertBean(Object bean)  
  11.             throws IntrospectionException, IllegalAccessException, InvocationTargetException {  
  12.         Class type = bean.getClass();  
  13.         Map returnMap = new HashMap();  
  14.         BeanInfo beanInfo = Introspector.getBeanInfo(type);  
  15.   
  16.         PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();  
  17.         for (int i = 0; i< propertyDescriptors.length; i++) {  
  18.             PropertyDescriptor descriptor = propertyDescriptors[i];  
  19.             String propertyName = descriptor.getName();  
  20.             if (!propertyName.equals("class")) {  
  21.                 Method readMethod = descriptor.getReadMethod();  
  22.                 Object result = readMethod.invoke(bean, new Object[0]);  
  23.                 //if (result != null) {  
  24.                     returnMap.put(propertyName, result);  
  25.                 //} else {  
  26.                  //   returnMap.put(propertyName, "");  
  27.                 //}  
  28.             }  
  29.         }  
  30.         return returnMap;  
  31.     }  

下面是将Map转化成JavaBean对象的方法:

 

 

[java]  view plain copy
 
  1. /** 
  2.      * 将一个 Map 对象转化为一个 JavaBean 
  3.      * @param type 要转化的类型 
  4.      * @param map 包含属性值的 map 
  5.      * @return 转化出来的 JavaBean 对象 
  6.      * @throws IntrospectionException 如果分析类属性失败 
  7.      * @throws IllegalAccessException 如果实例化 JavaBean 失败 
  8.      * @throws InstantiationException 如果实例化 JavaBean 失败 
  9.      * @throws InvocationTargetException 如果调用属性的 setter 方法失败 
  10.      */  
  11.     @SuppressWarnings("rawtypes")  
  12.     public static Object convertMap(Class type, Map map)  
  13.             throws IntrospectionException, IllegalAccessException,  
  14.             InstantiationException, InvocationTargetException {  
  15.         BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性  
  16.         Object obj = type.newInstance(); // 创建 JavaBean 对象  
  17.   
  18.         // 给 JavaBean 对象的属性赋值  
  19.         PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();  
  20.         for (int i = 0; i< propertyDescriptors.length; i++) {  
  21.             PropertyDescriptor descriptor = propertyDescriptors[i];  
  22.             String propertyName = descriptor.getName();  
  23.   
  24.             if (map.containsKey(propertyName)) {  
  25.                 // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。  
  26.                 Object value = map.get(propertyName);  
  27.   
  28.                 Object[] args = new Object[1];  
  29.                 args[0] = value;  
  30.   
  31.                 descriptor.getWriteMethod().invoke(obj, args);  
  32.             }  
  33.         }  
  34.         return obj;  
  35.     }  



以上内容我测试过,是没有问题的。供大家参考学习。

猜你喜欢

转载自wangjin161.iteye.com/blog/2216087