|
|
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);
|
|
|
}
|
|
|
} |
...
|
...
|
|