平时我们常会遇到Bean对象和Map的转换,对于字段比较少并且结构简单的Bean对象和Map的转换,常规做法就是逐个去set属性和值,或者逐个put属性和值,但是如果对于字段比较多或者嵌套结构复杂的Bean对象和Map转换,常规做法就远远力不从心了,那么我们就需要封装一个公共的高效的泛型工具类去进行转换。笔者结合平时的编码经验积累,经过整理,本案例给大家分享5种方式实现Bean对象和Map的转换,本案例基于BeanUtils.populate(bean, map)和JDK自带的反射机制,废话不多时,直接上代码。
准备工作:先在pom文件中添加org.apache.commons的依赖,如下所示:
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>
工具类的5种具体实现方式:
package com.shsc.base.data.common.utils;
import org.apache.commons.beanutils.BeanUtils;
import com.shsc.base.data.common.mapper.entity.CustomerProduct;
import com.shsc.base.data.common.mapper.entity.ProductPic;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
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;
public class BeanConverterUtils {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, Object> map = new HashMap<String, Object>();
List <ProductPic> pics = new ArrayList<ProductPic>();
ProductPic pic = new ProductPic();
pic.setAttachmentId("12324324353464747");
pics.add(pic);
map.put("customerId", "111");
map.put("productId", "222");
map.put("pics", pics);
CustomerProduct c = new CustomerProduct();
try {
// map2Bean(map,c);
// mapToBean(map,c.getClass());
map2JavaBean(map,c);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Map转换Bean对象方式一:使用apache.commons.beanutils.BeanUtils实现(需在pom文件中添加依赖)
* 使用泛型免去了类型转换的麻烦。
* @param <T>
* @param map
* @param beanClass
* @return
*/
public static <T> T map2Bean(Map<String, Object> map, T t) {
try {
BeanUtils.populate(t, map);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return t;
}
/**
* Map转换Bean对象方式二:使用apache.commons.beanutils.BeanUtils实现(需在pom文件中添加依赖)
* @param <T>
* @param map
* @param beanClass
* @return
*/
public static <T> T mapToBean(Map<String, Object> map, Class<T> beanClass){
T bean = null;
try {
bean = beanClass.newInstance();
BeanUtils.populate(bean, map);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return bean;
}
/**
* Map转换Bean对象方式三(基于JDK实现)
* @param map
* @param t
* @param <T>
* @return
*/
public static <T> T map2JavaBean(Map<String, Object> map, T t) {
if(map == null || t == null) {
return null;
}
try {
BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor pd : pds) {
String key = pd.getName();
if(map.containsKey(key)) {
Object value = map.get(key);
Method setter = pd.getWriteMethod();
setter.invoke(t, value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
/**
* Bean对象转换Map方式一(基于JDK实现)
* @param obj
* @return
*/
public static Map<String, Object> bean2Map(Object obj)throws Exception{
Map<String, Object> map = new HashMap<String, Object>();
Class<?> clazz = obj.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
Method getMethod = pd.getReadMethod();
Object o = getMethod.invoke(obj);
map.put(field.getName(), o);
}
return map;
}
/**
* Bean对象转换Map方式二(基于JDK实现)
* @param obj
* @return
*/
public static <T> Map<String, Object> beanToMap(T bean)throws Exception{
if(bean == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor pd : pds) {
String key = pd.getName();
Method getMethod = pd.getWriteMethod();
Object o = getMethod.invoke(bean);
map.put(key, o);
}
return map;
}
}
欢迎各位开发者朋友一起交流。笔者电话(微信):18629374628