pom.xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
工具类
package com.xiaobu.util;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
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;
@Slf4j
public class EntityUtils {
public static <T> List<T> castEntity(List<Object[]> list, Class<T> clazz) {
List<T> returnList = new ArrayList<>();
if (list.size() == 0) {
return returnList;
}
Class[] c2 = null;
Constructor[] constructors = clazz.getConstructors();
for (Constructor constructor : constructors) {
Class[] tClass = constructor.getParameterTypes();
if (tClass.length == list.get(0).length) {
c2 = tClass;
break;
}
}
for (Object[] o : list) {
Constructor<T> constructor = null;
try {
constructor = clazz.getConstructor(c2);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
assert constructor != null;
returnList.add(constructor.newInstance(o));
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
return returnList;
}
public static <T> T convertBean(Object object, Class<T> entityClass) {
if (null == object) {
return null;
}
String jsonStr = JSONUtil.toJsonStr(object);
return JSONUtil.toBean(jsonStr, entityClass);
}
public static Map<?, ?> objectToMap(Object object) {
return convertBean(object, Map.class);
}
public static <T> T mapToBean(Map<String, Object> map, Class<T> t) throws InstantiationException, IllegalAccessException, InvocationTargetException {
T instance = t.newInstance();
org.apache.commons.beanutils.BeanUtils.populate(instance, map);
return instance;
}
public static Map<String, String> transBean2Map(Object obj) {
if (obj == null) {
return null;
}
Map<String, String> map = new HashMap<>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (!"class".equals(key)) {
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
map.put(key, value.toString());
}
}
} catch (Exception e) {
System.out.println("transBean2Map Error " + e);
}
return map;
}
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;
}
public static Map convertBean2Map(Object bean) throws Exception {
Class type = bean.getClass();
Map returnMap = new HashMap();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo
.getPropertyDescriptors();
for (int i = 0, n = propertyDescriptors.length; i < n; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean);
if (result != null) {
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, "");
}
}
}
return returnMap;
}
public static <T> List<T> convertListMap2ListBean(List<Map<String, Object>> listMap, Class T) throws Exception {
List<T> beanList = new ArrayList<T>();
for (Map<String, Object> map : listMap) {
T bean = convertMap2Bean(map, T);
beanList.add(bean);
}
return beanList;
}
public static List<Map<String, Object>> convertListBean2ListMap(List<Object> beanList) throws Exception {
List<Map<String, Object>> mapList = new ArrayList<>();
for (Object bean : beanList) {
Map map = convertBean2Map(bean);
mapList.add(map);
}
return mapList;
}
}
|