JavaBean 和 Map 之间互相转换

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.beans.BeanInfo;  
  2. import java.beans.IntrospectionException;  
  3. import java.beans.Introspector;  
  4. import java.beans.PropertyDescriptor;  
  5. import java.lang.reflect.InvocationTargetException;  
  6. import java.lang.reflect.Method;  
  7. import java.util.HashMap;  
  8. import java.util.Map;  
  9.   
  10. /** 
  11.  * JavaBean and map converter. 
  12.  *  
  13.  *  
  14.  */  
  15. public final class BeanMapUtils {  
  16.       
  17.     /** 
  18.      * Converts a map to a JavaBean. 
  19.      *  
  20.      * @param type type to convert 
  21.      * @param map map to convert 
  22.      * @return JavaBean converted 
  23.      * @throws IntrospectionException failed to get class fields 
  24.      * @throws IllegalAccessException failed to instant JavaBean 
  25.      * @throws InstantiationException failed to instant JavaBean 
  26.      * @throws InvocationTargetException failed to call setters 
  27.      */  
  28.     public static final Object toBean(Class<?> type, Map<String, ? extends Object> map)   
  29.             throws IntrospectionException, IllegalAccessException,  InstantiationException, InvocationTargetException {  
  30.         BeanInfo beanInfo = Introspector.getBeanInfo(type);  
  31.         Object obj = type.newInstance();  
  32.         PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();  
  33.         for (int i = 0; i< propertyDescriptors.length; i++) {  
  34.             PropertyDescriptor descriptor = propertyDescriptors[i];  
  35.             String propertyName = descriptor.getName();  
  36.             if (map.containsKey(propertyName)) {  
  37.                 Object value = map.get(propertyName);  
  38.                 Object[] args = new Object[1];  
  39.                 args[0] = value;  
  40.                 descriptor.getWriteMethod().invoke(obj, args);  
  41.             }  
  42.         }  
  43.         return obj;  
  44.     }  
  45.       
  46.     /** 
  47.      * Converts a JavaBean to a map. 
  48.      *  
  49.      * @param bean JavaBean to convert 
  50.      * @return map converted 
  51.      * @throws IntrospectionException failed to get class fields 
  52.      * @throws IllegalAccessException failed to instant JavaBean 
  53.      * @throws InvocationTargetException failed to call setters 
  54.      */  
  55.     public static final Map<String, Object> toMap(Object bean)  
  56.             throws IntrospectionException, IllegalAccessException, InvocationTargetException {  
  57.         Map<String, Object> returnMap = new HashMap<String, Object>();  
  58.         BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());  
  59.         PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();  
  60.         for (int i = 0; i< propertyDescriptors.length; i++) {  
  61.             PropertyDescriptor descriptor = propertyDescriptors[i];  
  62.             String propertyName = descriptor.getName();  
  63.             if (!propertyName.equals("class")) {  
  64.                 Method readMethod = descriptor.getReadMethod();  
  65.                 Object result = readMethod.invoke(bean, new Object[0]);  
  66.                 if (result != null) {  
  67.                     returnMap.put(propertyName, result);  
  68.                 } else {  
  69.                     returnMap.put(propertyName, "");  
  70.                 }  
  71.             }  
  72.         }  
  73.         return returnMap;  
  74.     }  
  75. }  

说明:

(1)上面的方法是对象转map,是根据方法getter方法来的。

字段的属性不管是private或者public都可以。只要相应的字段实现了getter即可。

(2)注意这里如果是继承的关系也是一样的,父类的字段实现getter方法即可,类如:

People.java
public People {
        public int age;
	String name;
	public int getAge() {
		return age;
	}
	public String getName() {
		return name;
	}
}
Student.java
public Student  extends People{
	public float score;
	public int getScore() {
		return age;
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值