bean与map 互转

一个工具类,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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Map和bean互转
 * @author liunn
 */
public class BeanAndMapUtil {
	
	/**
	 * map转bean
	 * @param bean
	 * @param map
	 * @return 实体类
	 */
	@SuppressWarnings("unchecked")
	public static <T> T map2Bean(T bean, Map<String,Object> map) {
		try {
			Object object=bean.getClass().newInstance();
			BeanInfo beanInfo=Introspector.getBeanInfo(bean.getClass());
			PropertyDescriptor[] PropertyDescriptors=beanInfo.getPropertyDescriptors();
			if (PropertyDescriptors!=null&&PropertyDescriptors.length>0) {
				String propertyName;
				Object propertyValue;
				for (PropertyDescriptor pd:PropertyDescriptors) {
					propertyName=pd.getName();
					//检查map中是否包含这个key
					if (map.containsKey(propertyName)) {
						propertyValue=map.get(propertyName);
						if (propertyValue!=null&&!"null".equals(propertyValue)&&!"NULL".equals(propertyValue)) {
							//相当于setXxx()set方法
							pd.getWriteMethod().invoke(object, new Object[] {propertyValue});
						}
					}
				}
				return (T) object;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * Map集合转 bean集合
	 * @param listMap
	 * @param t
	 * @return
	 */
	public static <T> List<T> listMap2ListBean(List<Map<String,Object>> listMap,Class<T> t){
		T bean =null;
		try {
			bean=t.newInstance();
		} catch (Exception e) {
		e.printStackTrace();
		}
		if (listMap==null||listMap.size()==0||listMap.isEmpty()) {
			return null;
		}
		List<T> list=new ArrayList<T>();
		T object = null;
		for (Map<String,Object> map:listMap) {
			if (map!=null) {
				object=map2Bean(bean,map);
			}
			list.add(object);
		}
		return list;
		
	}
	
	/**
	 * 把bean 转成map
	 * @param bean 被转成map的bean
	 * @return
	 * @throws IntrospectionException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 */
	public static Map<String,Object> bean2Map(Object bean) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Class<?extends Object> type=bean.getClass();
		Map<String,Object> returnMap=new HashMap<String, Object>();
		BeanInfo beanInfo=Introspector.getBeanInfo(type);
		PropertyDescriptor[] PropertyDescriptors=beanInfo.getPropertyDescriptors();
		for (int i = 0; i < PropertyDescriptors.length; i++) {
			PropertyDescriptor PropertyDescriptor=PropertyDescriptors[i];
			String propertyName=PropertyDescriptor.getName();
			if (!"class".equals(propertyName)) {
				Method getMethod=PropertyDescriptor.getReadMethod();
				Object result=getMethod.invoke(bean, new Object[0]);
				if (result!=null) {
					returnMap.put(propertyName, result);
				}else {
					returnMap.put(propertyName, null);
				}
			}
		}
		return returnMap;	
	}
	
	/**
	 * @param listBean bean 的集合
	 * @param t 泛型
	 * @return 实体类
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 * @throws IntrospectionException
	 */
	public static <T> List<Map<String,Object>> listBean2ListMap(List<T> listBean,Class<T> t) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		List<Map<String,Object>> listMap =new ArrayList<Map<String,Object>>();
		for (int i = 0; i < listMap.size(); i++) {
			Object bean=listBean.get(i);
			Map<String,Object> map=bean2Map(bean);
			listMap.add(map);
		}
		return listMap;
	}
	
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值