Orika属性映射工具
引入pom依赖
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.5.4</version>
</dependency>
上干货
封装的工具类:OriUtils
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.ClassMapBuilder;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Description: Orika封装的工具类
* @date 2021/7/2 16:16
* @AuThor mb
*/
public class OrikaUtils {
private static final MapperFactory FACTORY = new DefaultMapperFactory.Builder().build();
/**
* 缓存实例集合
*/
private static Map<String, MapperFacade> cacheMapper = new ConcurrentHashMap<>();
private MapperFacade mapper;
public OrikaUtils(MapperFacade mapper) {
this.mapper = mapper;
}
/**
* 转换实体函数
*
* @Param sourceEntity 源实体
* @param targetClass 目标类对象
* @param refMap 配置源类与目标类不同字段名映射
* @param <S> 源泛型
* @param <T> 目标泛型
* @Return 目标实体
*/
public static <S, T> T convert(S sourceEntity, Class<T> targetClass, Map<String, String> refMap) {
if (sourceEntity == null) {
return null;
}
return classMap(sourceEntity.getClass(), targetClass, refMap).map(sourceEntity, targetClass);
}
/**
* 转换实体函数
*
* @param sourceEntity 源实体
* @param targetClass 目标类对象
* @param <S> 源泛型
* @param <T> 目标泛型
* @return 目标实体
*/
public static <S, T> T convert(S sourceEntity, Class<T> targetClass) {
return convert(sourceEntity, targetClass, null);
}
/**
* 转换实体集合函数
*
* @param sourceEntityList 源实体集合
* @param targetClass 目标类对象
* @param refMap 配置源类与目标类不同字段名映射
* @param <S> 源泛型
* @param <T> 目标泛型
* @return 目标实体集合
*/
public static <S, T> List<T> convertList(List<S> sourceEntityList, Class<T> targetClass, Map<String, String> refMap) {
if (sourceEntityList == null) {
return null;
}
if (sourceEntityList.size() == 0) {
return new ArrayList<>(0);
}
return classMap(sourceEntityList.get(0).getClass(), targetClass, refMap).mapAsList(sourceEntityList, targetClass);
}
/**
* 转换实体集合函数
*
* @param sourceEntityList 源实体集合
* @param targetClass 目标类对象
* @param <S> 源泛型
* @param <T> 目标泛型
* @return 目标实体集合
*/
public static <S, T> List<T> convertList(List<S> sourceEntityList, Class<T> targetClass) {
return convertList(sourceEntityList, targetClass, null);
}
/**
* @param source
* @param target
* @param <V>
* @param <P>
* @return
*/
public static <V, P> OrikaUtils classMap(Class<V> source, Class<P> target) {
return classMap(source, target, null);
}
/**
* 属性名称一致可用
*
* @param source
* @param target
* @param <V>
* @param <P>
* @return
*/
public static synchronized <V, P> OrikaUtils classMap(Class<V> source, Class<P> target, Map<String, String> refMap) {
String key = source.getCanonicalName() + ":" + target.getCanonicalName();
if (cacheMapper.containsKey(key)) {
return new OrikaUtils(cacheMapper.get(key));
}
if (CollectionUtils.isEmpty(refMap)) {
FACTORY.classMap(source, target).byDefault().register();
} else {
ClassMapBuilder<V, P> classMapBuilder = FACTORY.classMap(source, target);
refMap.forEach(classMapBuilder::field);
classMapBuilder.byDefault().register();
}
MapperFacade mapperFacade = FACTORY.getMapperFacade();
cacheMapper.put(key, mapperFacade);
return new OrikaUtils(mapperFacade);
}
/**
* 复制对象
*
* @param source
* @param target
* @param <V>
* @param <P>
* @return
*/
public <V, P> P map(V source, Class<P> target) {
return mapper.map(source, target);
}
/**
* 复制List
*
* @param source
* @param target
* @param <V>
* @param <P>
* @return
*/
public <V, P> List<P> mapAsList(List<V> source, Class<P> target) {
return CollectionUtils.isEmpty(source) ? Collections.emptyList() : mapper.mapAsList(source, target);
}
}
演示示例:
// 单个对象属性映射
Map<String, String> refMap = new HashMap<>(2);
// 属性名的对应关系(属性名不一致的情况下使用)
refMap.put("postId", "blogId");
Comment com = OrikaUtils.convert(comment, Comment.class, refMap);
@Override
public List<ResponseSimpleTopic> queryTopicList(Integer desc) {
if (desc.equals(1)) {
return baseMapper.queryTopicList();
}
LambdaQueryWrapper<Topic> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(Topic::getId, Topic::getTopicName);
List<Topic> topics = this.list(queryWrapper);
// List集合属性映射
return OrikaUtils.classMap(Topic.class, ResponseSimpleTopic.class)
.mapAsList(topics, ResponseSimpleTopic.class);
}