双重预防项目-国泰新华二开定制版
16639036659
2022-09-27 7e7195766eb018b4ba00b2d0663f6dcb11adefc8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.ruoyi.common.utils;
 
import org.springframework.beans.BeanUtils;
 
import java.util.List;
import java.util.stream.Collectors;
 
public class BeanCopyUtils {
 
    private BeanCopyUtils() {
    }
 
    /**练习
    public static Object copyBean(Object source, Class clazz) {
        //创建目标对象
        Object result = null;
        try {
            result = clazz.newInstance();
            //实现属性copy
            BeanUtils.copyProperties(source, result);
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回结果
        return result;
    }*/
 
    public static <V> V copyBean(Object source,Class<V> clazz) {
        //创建目标对象
        V result = null;
        try {
            result = clazz.newInstance();
            //实现属性copy
            BeanUtils.copyProperties(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回结果
        return result;
    }
 
    /**练习
     *  public static <V> List<V>  copyBeanList(List<Object> list, Class<V> clazz){
            return list.stream()
                    .map(o -> copyBean(o, clazz))
                    .collect(Collectors.toList());
        }*/
 
    public static <O,V> List<V> copyBeanList(List<O> list, Class<V> clazz){
        return list.stream()
                .map(o -> copyBean(o, clazz))
                .collect(Collectors.toList());
    }
}