cn.meteor.module.util.mapper.BeanMapper Maven / Gradle / Ivy
package cn.meteor.module.util.mapper;
import java.util.List;
import java.util.Map;
import org.dozer.DozerBeanMapper;
import org.dozer.MappingException;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.ClassMapBuilder;
import ma.glasnost.orika.metadata.Type;
import ma.glasnost.orika.metadata.TypeFactory;
/**
* 封装orika, 实现深度的BeanOfClasssA<->BeanOfClassB复制 pom文件引用orika
*
* 封装dozer,实现map和对象相互转换 pom文件引用dozer
*
* 不用Apache Common BeanUtils进行类复制,每次就行反射查询对象的属性列表, 非常缓慢.
*
*/
public class BeanMapper {
private static MapperFacade mapper;
private static DozerBeanMapper dozerBeanMapper;
static {
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapper = mapperFactory.getMapperFacade();
dozerBeanMapper = new DozerBeanMapper();
}
/**
* 适用于map转对象
* @param source
* @param destinationClass
* @return
* @throws MappingException
*/
public static D mapWithDozer(Object source, Class destinationClass) throws MappingException {
return dozerBeanMapper.map(source, destinationClass);
}
/**
* 适用于对象转map
* @param source
* @param destination
* @throws MappingException
*/
public static void mapWithDozer(Object source, Object destination) throws MappingException {
dozerBeanMapper.map(source, destination);
}
public MapperFacade getCustomMapperFacade(Class sourceClass, Class destinationClass, Map customMap) {
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
MapperFacade mapper = mapperFactory.getMapperFacade();
ClassMapBuilder classMapBuilder = mapperFactory.classMap(sourceClass, destinationClass);
for (String key : customMap.keySet()) {
classMapBuilder.field(key, customMap.get(key));
}
classMapBuilder.byDefault().register();
return mapper;
}
/**
* 简单的复制出新类型对象.
*
* 通过source.getClass() 获得源Class
*/
public static D map(S source, Class destinationClass) {
return mapper.map(source, destinationClass);
}
/**
* 极致性能的复制出新类型对象.
*
* 预先通过BeanMapper.getType() 静态获取并缓存Type类型,在此处传入
*/
public static D map(S source, Type sourceType, Type destinationType) {
return mapper.map(source, sourceType, destinationType);
}
/**
* 简单的复制出新对象列表到ArrayList
*
* 不建议使用mapper.mapAsList(Iterable,Class)接口, sourceClass需要反射,实在有点慢
*/
public static List mapList(Iterable sourceList, Class sourceClass, Class destinationClass) {
return mapper.mapAsList(sourceList, TypeFactory.valueOf(sourceClass), TypeFactory.valueOf(destinationClass));
}
/**
* 极致性能的复制出新类型对象到ArrayList.
*
* 预先通过BeanMapper.getType() 静态获取并缓存Type类型,在此处传入
*/
public static List mapList(Iterable sourceList, Type sourceType, Type destinationType) {
return mapper.mapAsList(sourceList, sourceType, destinationType);
}
/**
* 简单复制出新对象列表到数组
*
* 通过source.getComponentType() 获得源Class
*/
public static D[] mapArray(final D[] destination, final S[] source, final Class destinationClass) {
return mapper.mapAsArray(destination, source, destinationClass);
}
/**
* 极致性能的复制出新类型对象到数组
*
* 预先通过BeanMapper.getType() 静态获取并缓存Type类型,在此处传入
*/
public static D[] mapArray(D[] destination, S[] source, Type sourceType, Type destinationType) {
return mapper.mapAsArray(destination, source, sourceType, destinationType);
}
/**
* 预先获取orika转换所需要的Type,避免每次转换.
*/
public static Type getType(final Class rawType) {
return TypeFactory.valueOf(rawType);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy