List<UserInfoTmp> userInfoTmpList = userInfoTmpMapper.selectUserListById(id);
List<UserInfo> userInfoList = BeanUtils.propertiesCopy(userInfoTmpList , new UserInfo());
UserInfoTmp userInfoTmp = userInfoTmpMapper.selectUserById(id);
UserInfo userInfo = BeanUtils.propertiesCopy(userInfoTmp , new UserInfo());
Map<String, Object> params = BeanUtils.propertiesCopyToMap(user);
package com.common.utils;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
public class BeanUtils {
@SuppressWarnings("unchecked")
public static <S, T, R> R propertiesCopy(S source, T target) {
try {
Class<? extends Object> cla = target.getClass();
if (source instanceof List<?> sList) {
List<T> tList = new ArrayList<T>(10);
for (Object src : sList) {
T tar = beanPropertiesCopy(src, cla);
tList.add(tar);
}
return (R) tList;
}
if (source instanceof Set<?> sSet) {
Set<T> tSet = new HashSet<T>();
for (Object src : sSet) {
T tar = beanPropertiesCopy(src, cla);
tSet.add(tar);
}
return (R) tSet;
}
if (source instanceof Array sArr) {
Array[] tArr = new Array[Array.getLength(sArr)];
for (int i = 0; i < Array.getLength(sArr); i++) {
T tar = beanPropertiesCopy(Array.get(sArr, i), cla);
Array.set(tArr, i, tar);
}
return (R) tArr;
}
return beanPropertiesCopy(source, cla);
} catch (Exception e) {
e.printStackTrace();
}
return (R) target;
}
@SuppressWarnings("unchecked")
private static <T> T beanPropertiesCopy(Object source, Class<?> cla) throws Exception {
T target = (T) cla.newInstance();
org.springframework.beans.BeanUtils.copyProperties(source, target);
return target;
}
public static Map<String, Object> propertiesCopyToMap(Object source, String... ignores) {
List<Field> fields = FieldUtils.getAllFieldsList(source.getClass());
Map<String, Object> map = new HashMap<String, Object>(8);
for (Field field : fields) {
field.setAccessible(true);
if (StringUtils.equals(field.getName(), "serialVersionUID")) {
continue;
}
if (null != ignores && ignores.length > 0) {
List<String> ignoreFields = Arrays.asList(ignores);
if (ignoreFields.contains(field.getName())) {
continue;
}
}
try {
Object obj = FieldUtils.readField(field, source);
if (!isEmpty(obj)) {
map.put(field.getName(), obj);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return map;
}
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
if (obj.getClass().isArray()) {
return Array.getLength(obj) == 0;
}
if (obj instanceof CharSequence) {
return ((CharSequence) obj).length() == 0;
}
if (obj instanceof Collection) {
return ((Collection) obj).isEmpty();
}
if (obj instanceof Map) {
return ((Map) obj).isEmpty();
}
return false;
}
}