两个实体类间的转换

package com.lrhealth.mappingintegration.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author ppq
*/
@Slf4j
public class BeanHelper {

public static <T> T copyProperties(Object source, Class<T> target){
try {
T t = target.newInstance();
BeanUtils.copyProperties(source, t);
return t;
} catch (Exception e) {
log.error("【数据转换】数据转换出错,目标对象{}构造函数异常", target.getName(), e);
return null;
}
}

public static <T> List<T> copyWithCollection(List<?> sourceList, Class<T> target){
try {
return sourceList.stream().map(s -> copyProperties(s, target)).collect(Collectors.toList());
} catch (Exception e) {
log.error("【数据转换】数据转换出错,目标对象{}构造函数异常", target.getName(), e);
return null;
}
}

public static <T> Set<T> copyWithCollection(Set<?> sourceList, Class<T> target){
try {
return sourceList.stream().map(s -> copyProperties(s, target)).collect(Collectors.toSet());
} catch (Exception e) {
log.error("【数据转换】数据转换出错,目标对象{}构造函数异常", target.getName(), e);
return null;
}
}
}
posted @ 2021-12-23 10:48  小小菜包子  阅读(443)  评论(0)    收藏  举报