java專案常用工具類之beancopy,bean和map轉換工具類
阿新 • • 發佈:2019-02-14
專案環境:
jdk1.8+spring4.3.12
一、問題描述及試用場景:
在專案規範中,要求類名以DO為尾的類作為資料庫層實體bean,類名以MO為尾的類作為系統傳輸層實體bean,類名以VO為尾的類作為服務端與前端互動的實體bean。由於以上要求,需要在各個bean直接進行copy資料,除了傻瓜式的set/get or constructor來copy資料外,spring提供了直接copybean的工具類,原理其實就是通過java反射來根據目標bean的欄位名呼叫其set方法來設值注入。除此之外,專案中還常見map與bean之間的轉換。特封裝此類,以供大家使用。
二、樣例程式碼:
package org.egg.utils;
import org.egg.enums.CommonErrorEnum;
import org.egg.exception.CommonException;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
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.List;
import java.util.Map;
/**
* @author dataochen
* @Description bean工具類
* @date: 2017/11/7 17:30
*/
public class BeanUtil {
/**
* 拷貝實體,source,target不允許為空
*
* @param source
* @param target
*/
public static void copyProperties(Object source, Object target) {
BeanUtils.copyProperties(source, target);
}
/**
* 拷貝實體集合,sourceList
*只支援自定義實體集合拷貝
*應用場景:DTO <=> DO 等
*/
public static void copyPropertiesList(List sourceList, List targetList,Class clazz) throws InstantiationException,IllegalAccessException {
if (CollectionUtils.isEmpty(sourceList)) {
throw new CommonException(CommonErrorEnum.PARAM_ERROR);
}
for (Object items : sourceList) {
Object target = clazz.newInstance();
BeanUtils.copyProperties(items, target);
targetList.add(target);
}
}
/**
* Map --> Bean 2: 利用org.apache.commons.beanutils 工具類實現 Map --> Bean
*
* @param map
* @param obj
*/
public static void transMap2Bean2(Map<String, Object> map, Object obj) throws InvocationTargetException, IllegalAccessException {
if (map == null || obj == null) {
return;
}
org.apache.commons.beanutils.BeanUtils.populate(obj, map);
}
/**
* Map --> Bean 1: 利用Introspector,PropertyDescriptor實現 Map --> Bean
*
* @param map
* @param obj
*/
public static void transMap2Bean(Map<String, Object> map, Object obj) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (map.containsKey(key)) {
Object value = map.get(key);
// 得到property對應的setter方法
Method setter = property.getWriteMethod();
setter.invoke(obj, value);
}
}
}
/**
* Bean --> Map 1: 利用Introspector和PropertyDescriptor 將Bean --> Map
*
* @param obj
*/
public static Map<String, Object> transBean2Map(Object obj) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 過濾class屬性
if (!key.equals("class")) {
// 得到property對應的getter方法
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
map.put(key, value);
}
}
return map;
}
程式碼所用jar包maven座標:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>