BeanCopyUtils 实现单个对象,多个对象属性Copy

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @version 1.0.0
 * @createTime 2019/11/22 11/52
 */
@Slf4j
public class BeanCopyUtils {

    public static  T copyBean(Object source,Class target){
        assert target != null;
        T t = null;
        try {
            t = target.newInstance();
            BeanUtils.copyProperties(source,t);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
// 抛异常出去,或者返回 null
       // throw new BusinessException(RespCodeEnum.SYSTEM_ERROR,"BeanCopyUtils copyBean source 转 Class 异常。source:"+ source.toString() + "; target class :" + target);
    }


    public static  List copyList(List sourceList, Class targetObject) {
        assert targetObject != null;
        assert !CollectionUtils.isEmpty(sourceList);
        List targetList = new ArrayList<>();
        sourceList.forEach(s ->{
            T t = null;
            try {
                t = targetObject.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            BeanUtils.copyProperties(s, t);
            targetList.add(t);
        });
        return targetList;
    }
}

你可能感兴趣的:(BeanCopyUtils 实现单个对象,多个对象属性Copy)