java Bean转Map

代码如下:


import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import cn.ruis.rscms.model.Website;

public class BeanMapUtil {

	/**
	 * 将一个 JavaBean 对象转化为一个 Map
	 * 
	 * @param bean
	 *            要转化的JavaBean 对象
	 * @return returnMap 转化出来的 Map 对象
	 */
	public static <T> Map<String, Object> beanToMap(T bean) {

		Class<? extends Object> type = bean.getClass();
		Map<String, Object> returnMap = new HashMap<String, Object>();

		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(type);
			PropertyDescriptor[] propertyDescriptors = beanInfo
					.getPropertyDescriptors();
			for (PropertyDescriptor descriptor : propertyDescriptors) {
				String propertyName = descriptor.getName();
				if (!propertyName.equals("class")) {
					Method readMethod = descriptor.getReadMethod();
					Object result = readMethod.invoke(bean, new Object[0]);
					//returnMap.put(propertyName, result != null ? result : "");
					if(result != null)
						returnMap.put(propertyName, result);
					else
						continue;
				}
			}
		} catch (IntrospectionException e) {
			throw new RuntimeException("分析类属性失败", e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException("分析类属性失败", e);
		} catch (InvocationTargetException e) {
			throw new RuntimeException("分析类属性失败", e);
		}
		return returnMap;
	}

	/**
	 * 将一个Map对象转化为一个JavaBean
	 * 
	 * @param map
	 *            包含属性值的map
	 * @param bean
	 *            要转化的类型
	 * @return beanObj 转化出来的JavaBean对象
	 */
	public static <T> T mapToBean(Map<String, Object> paramMap, Class<T> clazz) {
		T beanObj = null;
		try {
			beanObj = clazz.newInstance();
			String propertyName = null;
			Object propertyValue = null;
			for (Map.Entry<String, Object> entity : paramMap.entrySet()) {
				propertyName = entity.getKey();
				propertyValue = entity.getValue();
				setProperties(beanObj, propertyName, propertyValue);
			}
		} catch (IllegalArgumentException e) {
			throw new RuntimeException("不合法或不正确的参数", e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException("实例化JavaBean失败", e);
		} catch (Exception e) {
			throw new RuntimeException("Map转换为Java Bean对象失败", e);
		}
		return beanObj;
	}

	public static <T> void setProperties(T entity, String propertyName,
			Object value) throws IntrospectionException,
			IllegalArgumentException, IllegalAccessException,
			InvocationTargetException {
		PropertyDescriptor pd = new PropertyDescriptor(propertyName, entity
				.getClass());
		Method methodSet = pd.getWriteMethod();
		methodSet.invoke(entity, value);
	}
	/**
	 * 测试 能否将类转换成Mpa
	 * @param args
	 */
	public static void main(String[] args) {
		Website  w = new Website();
		w.setId(1);
		w.setWebsite4Unti("xxx");
		w.setWebsiteName("asdf");
		w.setWebsiteUrl("http://www.baidu.com");
		Map<String, Object> um = BeanMapUtil.beanToMap(w);
		for (Map.Entry<String, Object> entity : um.entrySet()) {
		}
	}	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

275261506

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值