一个工具类,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;
}
}