java项目中对象属性复制,支持单对象、List集合和对象单层属性复制到Map中

//List集合复制
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中
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 {

    /**
     * 对象属性复制,支持单对象、List集合
     */
    @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;
    }

    /**
     * 对象单层属性复制到Map中
     */
    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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值