BeanTools

Posted IT的鱼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BeanTools相关的知识,希望对你有一定的参考价值。


import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

TUuTGGMqeYmJ9PkQAoTSB9V7QzgsNPPJ7o
public class BeanTools 

    private BeanTools() 
        throw new IllegalStateException("Utility class");
    

    @SneakyThrows
    public static <T> List<T> copy(Class<T> clz, List<?> list) 
        List<T> rlist = new LinkedList<>();
        if (list == null || list.isEmpty()) 
            return rlist;
        
        for (Object obj : list) 
            T t = clz.newInstance();
            BeanUtils.copyProperties(obj, t);
            rlist.add(t);
        
        return rlist;
    

    @SneakyThrows
    public static <T> T copy(Class<T> clz, Serializable bean) 
        if (bean == null) 
            return null;
        
        T t = clz.newInstance();
        BeanUtils.copyProperties(bean, t);
        return t;
    

    @SneakyThrows
    public static <T> T copy(Class<T> clz, Object bean) 
        if (bean == null) 
            return null;
        
        T t = clz.newInstance();
        BeanUtils.copyProperties(bean, t);
        return t;
    



使用:

BeanTools.copy(LegendDTO.class, legendReq))

以上是关于BeanTools的主要内容,如果未能解决你的问题,请参考以下文章

BeanTools