BeanCopyUtils

package com.songzixian.blog.common.util;

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

/**
 * <p>
 * Description: 实体类转换 List集合转换
 * </p>
 *
 * @author songzixian
 * @version v2.0.0
 * @create 2022-08-27 20:07
 * @see com.songzixian.blog.common.util
 */
public class BeanCopyUtils {

    /**
     * 复制对象
     *
     * @param source 源
     * @param target 目标
     * @return {@link T}
     */
    public static <T> T copyObject(Object source, Class<T> target) {
        T temp = null;
        try {
            temp = target.newInstance();
            if (null != source) {
                org.springframework.beans.BeanUtils.copyProperties(source, temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return temp;
    }

    /**
     * 拷贝集合
     *
     * @param source 源
     * @param target 目标
     * @return {@link List <T>} 集合
     */
    public static <T, S> List<T> copyList(List<S> source, Class<T> target) {
        List<T> list = new ArrayList<>();
        if (null != source && source.size() > 0) {
            for (Object obj : source) {
                list.add(BeanCopyUtils.copyObject(obj, target));
            }
        }
        return list;
    }
}

使用
QQ截图20220904154436.png

Last modification:September 4, 2022
如果觉得这篇技术文章对你有用,请随意赞赏