Bean类和Map的相互转换

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="" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值