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