-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeanMapperUtils.java
More file actions
45 lines (39 loc) · 1.21 KB
/
BeanMapperUtils.java
File metadata and controls
45 lines (39 loc) · 1.21 KB
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
package com.dto;
/**
* @Auther: Administrator
* @Date: 2019\9\30 0030 23:11
* @Description:
*/
import java.util.Collection;
import java.util.List;
import com.google.common.collect.Lists;
import org.dozer.DozerBeanMapper;
public class BeanMapperUtils {
/**
* 持有Dozer单例, 避免重复创建DozerMapper消耗资源.
*/
private static DozerBeanMapper dozer = new DozerBeanMapper();
/**
* 基于Dozer转换对象的类型.
*/
public static <T> T map(Object source, Class<T> destinationClass) {
return dozer.map(source, destinationClass);
}
/**
* 基于Dozer转换Collection中对象的类型.
*/
public static <T> List<T> mapList(Collection<?> sourceList, Class<T> destinationClass) {
List<T> destinationList = Lists.newArrayList();
for (Object sourceObject : sourceList) {
T destinationObject = dozer.map(sourceObject, destinationClass);
destinationList.add(destinationObject);
}
return destinationList;
}
/**
* 基于Dozer将对象A的值拷贝到对象B中.
*/
public static void copy(Object source, Object destinationObject) {
dozer.map(source, destinationObject);
}
}