package com.utils;
import org.apache.commons.beanutils.BeanUtils;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Map工具类
*
*/
public class MapUtil {
/**
* 从map集合中获取属性值
*
* @param <E>
* @param map
* map集合
* @param key
* 键对
* @param defaultValue
* 默认值
* @return
*/
public final static <E> E get(Map map, Object key, E defaultValue) {
Object o = map.get(key);
if (o == null)
return defaultValue;
return (E) o;
}
/**
* * Map集合对象转化成 JavaBean集合对象
*
* @param javaBean
* JavaBean实例对象
* @param mapList
* Map数据集对象
* @return
*/
public static <T> List<T> map2Bean(T javaBean, List<Map<String, Object>> mapList) {
if (mapList == null || mapList.isEmpty()) {
return null;
}
List<T> objectList = new ArrayList<T>();
T object = null;
for (Map map : mapList) {
if (map != null) {
object = map2Bean(javaBean, map);
objectList.add(object);
}
}
return objectList;
}
/**
* * Map对象转化成 JavaBean对象
*
* @param javaBean
* JavaBean实例对象
* @param map
* Map对象 * @return
*/
public static <T> T map2Bean(T javaBean, Map map) {
try {
// 获取javaBean属性
BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());
// 创建 JavaBean 对象
Object obj = javaBean.getClass().newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
if (propertyDescriptors != null && propertyDescriptors.length > 0) {
String propertyName = null; // javaBean属性名
Object propertyValue = null; // javaBean属性值
Object propertyValueWrapper = null;
for (PropertyDescriptor pd : propertyDescriptors) {
propertyName = pd.getName();
propertyName = propertyName.toUpperCase();
if (map.containsKey(propertyName)) {
propertyValue = map.get(propertyName);
Class<?> clazz = pd.getWriteMethod().getParameterTypes()[0];
if (Integer.class == clazz) {
if (propertyValue == null) {
propertyValueWrapper = null;
} else {
if (BigDecimal.class.equals(propertyValue.getClass())) {
propertyValueWrapper = new Integer(((BigDecimal) propertyValue).intValue());
} else {
propertyValueWrapper = new Integer(Integer.parseInt(propertyValue.toString()));
}
}
} else if (String.class == clazz) {
propertyValueWrapper = (propertyValue == null) ? null : propertyValue.toString();
} else if (BigDecimal.class == clazz) {
if (propertyValue == null) {
propertyValueWrapper = null;
} else {
if (BigDecimal.class.equals(propertyValue.getClass())) {
propertyValueWrapper = propertyValue;
} else {
propertyValueWrapper = new BigDecimal(propertyValue.toString());
}
}
} else {
propertyValueWrapper = propertyValue;
}
pd.getWriteMethod().invoke(obj, new Object[] { propertyValueWrapper });
}
}
return (T) obj;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
/**
* JavaBean对象转化成Map对象
*
* @param javaBean
* @return
**/
public static Map<String, Object> bean2Map(Object javaBean) {
Map<String, Object> map = new HashMap<String, Object>();
try {
// 获取javaBean属性
BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
if (propertyDescriptors != null && propertyDescriptors.length > 0) {
String propertyName = null; // javaBean属性名
Object propertyValue = null; // javaBean属性值
for (PropertyDescriptor pd : propertyDescriptors) {
propertyName = pd.getName();
if (!propertyName.equals("class")) {
Method readMethod = pd.getReadMethod();
propertyValue = readMethod.invoke(javaBean, new Object[0]);
map.put(propertyName, propertyValue);
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return map;
}
/**
* JavaBean对象转化成Map对象
*
* @param javaBean
* @param fieldList
* 需转换的属性集合
* @return
**/
public static Map<String, Object> bean2Map(Object javaBean, List<String> fieldList) {
Map<String, Object> map = new HashMap<String, Object>();
List<String> fields = fieldList;
if (fields == null) {
fields = new ArrayList<String>();
}
try {
// 获取javaBean属性
BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
if (propertyDescriptors != null && propertyDescriptors.length > 0) {
String propertyName = null; // javaBean属性名
Object propertyValue = null; // javaBean属性值
for (PropertyDescriptor pd : propertyDescriptors) {
propertyName = pd.getName().toUpperCase();
if (fields.indexOf(propertyName) == -1) {
continue;
}
if (!propertyName.equals("class")) {
Method readMethod = pd.getReadMethod();
propertyValue = readMethod.invoke(javaBean, new Object[0]);
map.put(StringUtil.lineToHump(propertyName), propertyValue);
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return map;
}
/**
* JavaBean对象转化成Map对象集合
*
* @param javaBean
* @param beanList
* @param fieldList
* 字段集合
* @return
**/
public static <T> List<Map> bean2Map(T javaBean, List<T> beanList, List<String> fieldList) {
List<Map> mapList = new ArrayList<Map>();
if (beanList == null || beanList.isEmpty()) {
return mapList;
}
Map map = null;
for (T rowBean : beanList) {
if (rowBean != null) {
map = bean2Map(rowBean, fieldList);
mapList.add(map);
}
}
return mapList;
}
public static <T> List<T> convertListMap2ListBean(List<Map<String, Object>> listMap, Class T) throws Exception {
List<T> beanList = new ArrayList<T>();
for (int i = 0, n = listMap.size(); i < n; i++) {
Map<String, Object> map = listMap.get(i);
T bean = convertMap2Bean(map, T);
beanList.add(bean);
}
return beanList;
}
public static <T> T convertMap2Bean(Map map, Class T) throws Exception {
if (map == null || map.size() == 0) {
return null;
}
BeanInfo beanInfo = Introspector.getBeanInfo(T);
T bean = (T) T.newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0, n = propertyDescriptors.length; i < n; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
Object value = map.get(propertyName);
BeanUtils.copyProperty(bean, propertyName, value);
}
}
return bean;
}
}
Map Bean转换工具类
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 背景 在写代码的时候,不同的层、不同的接口等地方,有时候都要对对象做一下转换,之前一直用的是BeanUtils,感...
- 原地址:http://tech.dianwoda.com/2017/11/04/gao-xing-neng-te-...
- 前言 其实这个工具是给自己写的,因为自己老是忘记。所以记录一下。 Maven依赖 代码 代码说明 1、主要通过gu...