您的位置:首页 > 其它

Bean类和Map的相互转换

2015-09-16 19:05 295 查看
package com.htong_04;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* javaBean与Map<String,Object>互转
*
*
*/
public class BeanUtil {
public static void main(String[] args) {
PersonBean person = new PersonBean();
Map<String, Object> mp = new HashMap<String, Object>();

mp.put("name", "Jack");
mp.put("age", 40);
mp.put("mN", "male");
person=map2Bean(mp, person.getClass());
System.out.println("transMap2Bean Map Info:");
for (Map.Entry<String, Object> entry : mp.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
System.out.println("Bean Info:");
System.out.println("name: " + person.getName());
System.out.println("age: " + person.getAge());
System.out.println("mN: " + person.getmN());

Map<String, Object> map=bean2Map(PersonBean.class,mp);
System.out.println("transBean2Map Map Info:");
//Set<Map.Entry<K,V>> entrySet():返回的是键值对对象的集合
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}

public static <T, K, V> T map2Bean(Map<String, Object> map, Class<T> beanCls) {
// TODO Auto-generated method stub
T bean=null;
try {
bean=beanCls.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(beanCls);//通过类 Introspector 来获取某个对象的 BeanInfo信息
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();//通过 BeanInfo 来获取属性的描述器
//通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后我们就可以通过反射机制来调用这些方法
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if(map.containsKey(key)){
Object value=map.get(key);
if("".equals(value)){
value=null;
}
Object[] args=new Object[1];
args[0]=value;
property.getWriteMethod().invoke(bean, args);
}

}
} catch (Exception e) {

// TODO Auto-generated catch block
e.printStackTrace();
}
return (T)bean;
}

public static<T, K, V> Map<K, V> bean2Map(Class<T> beanCls, Map<K, V> map) {
// TODO Auto-generated method stub
T t = null;
try {
BeanInfo beanInfo = Introspector.getBeanInfo(beanCls);//通过类 Introspector 来获取某个对象的 BeanInfo信息
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();//通过 BeanInfo 来获取属性的描述器
//通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后我们就可以通过反射机制来调用这些方法
t = beanCls.newInstance();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (map.containsKey(key)) {
Object value = map.get(key);
Method setter = property.getWriteMethod();
setter.invoke(t, value);//第一个参数表示对象是谁,第二个参数表示调用该方法的实际参数
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;

}
}

package com.htong_04;

public class PersonBean {
private String name;
private Integer age;
private String mN;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getmN() {
return mN;
}

public void setmN(String mN) {
this.mN = mN;
}

}
最后结果为:
<img src="https://img-blog.csdn.net/20150916190627453?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: