OrikaUtils.java
1.55 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
46
47
48
49
50
package com.yohoufo.common.utils;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.ClassMapBuilder;
import java.util.List;
/**
* @author kun.wang
* @date 2018/9/11
*/
public final class OrikaUtils {
private OrikaUtils() {
}
private static MapperFactory mapperFactory;
private static MapperFacade mapperFacade;
static {
mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFacade = mapperFactory.getMapperFacade();
}
public static <S, D> void map(S source, D dest) {
mapperFacade.map(source, dest);
}
public static <S, D> D map(S source, Class<D> destinationClass) {
return mapperFacade.map(source, destinationClass);
}
public static <S, D> List<D> mapToList(Iterable<S> source, Class<D> destinationClass) {
return mapperFacade.mapAsList(source, destinationClass);
}
public static <S, D> D mapWithExclude(S source, Class<D> destinationClass, String... fieldNames) {
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
ClassMapBuilder<S, D> classMapBuilder = mapperFactory.classMap((Class<S>) source.getClass(), destinationClass);
for (String fieldName : fieldNames) {
classMapBuilder.exclude(fieldName);
}
classMapBuilder.byDefault().register();
MapperFacade mapperFacade = mapperFactory.getMapperFacade();
return mapperFacade.map(source, destinationClass);
}
}