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.     }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM