码迷,mamicode.com
首页 > 编程语言 > 详细

java中Bean类和Map的相互转换

时间:2015-08-07 12:47:06      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

  1 package com.studey.mapTbean;
  2 
  3 import java.beans.BeanInfo;
  4 import java.beans.IntrospectionException;
  5 import java.beans.Introspector;
  6 import java.beans.PropertyDescriptor;
  7 import java.lang.reflect.InvocationTargetException;
  8 import java.lang.reflect.Method;
  9 import java.util.HashMap;
 10 import java.util.Map;
 11 
 12 public class BeanUtil {
 13 
 14     public static void main(String[] args) throws Exception {
 15 
 16         Map<String, Object> mp = new HashMap<String, Object>();
 17         mp.put("name", "Tom Cat");
 18         mp.put("addr", 178);
 19         mp.put("tel", "123456");
 20 
 21         PersonBean person = map2Bean(mp, PersonBean.class);
 22         System.out.println("transMap2Bean Map Info:");
 23         for (Map.Entry<String, Object> entry : mp.entrySet()) {
 24             System.out.println(entry.getKey() + ": " + entry.getValue());
 25         }
 26         System.out.println("Bean Info:");
 27         System.out.println("name: " + person.getName());
 28         System.out.println("addr: " + person.getaddr());
 29         System.out.println("mN: " + person.gettel());
 30 
 31         bean2Map(person, mp);
 32         System.out.println("transBean2Map Map Info:");
 33         for (Map.Entry<String, Object> entry : mp.entrySet()) {
 34             System.out.println(entry.getKey() + ": " + entry.getValue());
 35         }
 36     }
 37 
 38     // mapToBean方法:
 39 
 40     public static <T, K, V> T map2Bean(Map<K, V> mp, Class<T> beanCls)
 41             throws Exception, IllegalArgumentException, InvocationTargetException {
 42         T t = null;
 43         try {
 44 
 45             BeanInfo beanInfo = Introspector.getBeanInfo(beanCls);
 46             PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
 47 
 48             t = beanCls.newInstance();
 49 
 50             for (PropertyDescriptor property : propertyDescriptors) {
 51                 String key = property.getName();
 52 
 53                 if (mp.containsKey(key)) {
 54                     Object value = mp.get(key);
 55                     Method setter = property.getWriteMethod(); 56       
57 setter.invoke(t, value); 58 } 59 } 60 61 } catch (IntrospectionException e) { 62 63 e.printStackTrace(); 64 } 65 return t; 66 } 67 68 // beanToMap方法: 69 70 public static <T, K, V> Map<String, Object> bean2Map(T bean, Map<String, Object> mp) 71 throws Exception, IllegalAccessException { 72 73 if (bean == null) { 74 return null; 75 } 76 77 try { 78 BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass()); 79 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); 80 81 for (PropertyDescriptor property : propertyDescriptors) { 82 String key = property.getName(); 83 84 if (!key.equals("class")) { 85 86 Method getter = property.getReadMethod();
87
88 Object value; 89 90 value = getter.invoke(bean); 91 mp.put(key, value); 92 } 93 94 } 95 96 } catch (IntrospectionException e) { 97 98 e.printStackTrace(); 99 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 100
101 e.printStackTrace(); 102 103 } 104 return mp; 105 106 } 107 }
 1 package com.studey.mapTbean;
 2 
 3 public class PersonBean { 
 4     private String name; 
 5     private Integer addr; 
 6     private String tel; 
 7   
 8     public String getName() { 
 9         return name; 
10     } 
11   
12     public void setName(String name) { 
13         this.name = name; 
14     } 
15   
16     public Integer getaddr() { 
17         return addr; 
18     } 
19   
20     public void setaddr(Integer addr) { 
21         this.addr = addr; 
22     } 
23   
24     public String gettel() { 
25         return tel; 
26     } 
27   
28     public void settel(String tel) { 
29         this.tel = tel; 
30     } 
31   
32 } 

运行结果:

 1 transMap2Bean Map Info:
 2 name: Tom Cat
 3 tel: 123456
 4 addr: 178
 5 Bean Info:
 6 name: Tom Cat
 7 addr: 178
 8 mN: 123456
 9 transBean2Map Map Info:
10 name: Tom Cat
11 tel: 123456
12 addr: 178

 

java中Bean类和Map的相互转换

标签:

原文地址:http://www.cnblogs.com/Avanet/p/4710275.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!