1
yangbin
2024-08-15 fc38e2635216775a80210d0df109dc6174d66813
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
package org.jeecg.modules.utils;
 
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.converter.builtin.PassThroughConverter;
import ma.glasnost.orika.impl.DefaultMapperFactory;
 
import java.util.List;
import java.util.Set;
 
/**
 * 复制对象属性的工具类。
 * Created by sanerhe on 2014/12/28.
 */
public class BeanMapper {
 
    /**
     * 实例.
     */
    private static MapperFacade mapper;
 
    static {
        // 如果src中属性为null,就不复制到dest
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().mapNulls(false).build();
        // 如果属性是Object,就只复制引用,不复制值,可以避免循环复制
        mapperFactory.getConverterFactory().registerConverter(new PassThroughConverter(Object.class));
        mapper = mapperFactory.getMapperFacade();
    }
 
    /**
     * 把source中的值复制到destination中.
     */
    public static void copy(Object source, Object destination) {
        mapper.map(source, destination);
    }
 
    /**
     * 把source中的值复制到destinationCls中.
     */
    public static <S, D> List<D> copyList(List<S> source, Class<D> destinationCls) {
        return mapper.mapAsList(source, destinationCls);
    }
 
    /**
     * 把source中的值复制到destination中.
     */
    public static <S, D> List<D> copyCollection(List<S> source, List<D> destination, Class<D> destinationCls) {
        mapper.mapAsCollection(source, destination, destinationCls);
        return destination;
    }
 
    /**
     * 把source中的值复制到destination中.
     */
    public static <S, D> Set<D> copyCollection(Set<S> source, Class<D> destinationCls) {
        Set<D> destination = mapper.mapAsSet(source, destinationCls);
        return destination;
    }
}