智慧保安后台管理-外网
Administrator
2022-06-16 8b375fe00a241b3a769b82fe3dac8d1c9dce8a02
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package org.springblade.common.utils;
 
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
 
/**
 * ClassName: BeanCopyUtils
 * Description: Bean拷贝工具类
 *
 */
public class BeanCopyUtils {
 
    private BeanCopyUtils() {
    }
 
    /**
     * 将对象属性拷贝到目标类型的同名属性字段中
     * @param <T>
     * @param source
     * @param targetClazz
     * @return
     */
    public static <T> T copyProperties(Object source, Class<T> targetClazz) {
 
        T target = null;
        try {
            target = targetClazz.newInstance();
            BeanUtils.copyProperties(source, target);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
 
        return target;
    }
 
    /**
     * 将对象属性拷贝到目标类型的同名属性字段中
     * @param source
     * @param target
     * @return
     */
    public static <T> T copyProperties(Object source, T target) {
        BeanUtils.copyProperties(source, target);
        return target;
    }
 
 
    /**
     * 将list的对象拷贝到目标类型对象中
     * @param list
     * @param clazz
     * @return
     */
    public static <V, E> List<E> copy(Collection<V> list, Class<E> clazz) {
        List<E> result = new ArrayList<>(12);
 
        if (!CollectionUtils.isEmpty(list)) {
            for (V source : list) {
                E target = null;
                try {
                    target = (E) clazz.newInstance();
                    BeanUtils.copyProperties(source, target);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
 
                result.add(target);
            }
        }
 
        return result;
    }
 
}