- 对所有 Java 源文件应用统一的代码格式化 - 统一缩进为 4 个空格 - 清理尾随空白字符和文件末尾换行 - 优化导入语句组织 - 总计格式化 654 个 Java 文件 有问题可以回滚或者找我聊
48 lines
1.3 KiB
Java
48 lines
1.3 KiB
Java
package com.starry.common.utils;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.List;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
/**
|
|
* @author admin
|
|
*/
|
|
@Slf4j
|
|
public class ConvertUtil {
|
|
|
|
public static <T> T entityToVo(Object source, Class<T> target) {
|
|
if (source == null) {
|
|
return null;
|
|
}
|
|
T targetObject = null;
|
|
try {
|
|
targetObject = target.newInstance();
|
|
BeanUtils.copyProperties(source, targetObject);
|
|
} catch (Exception e) {
|
|
log.error("entityToVo error,source = {},target={}", source, target, e);
|
|
}
|
|
return targetObject;
|
|
}
|
|
|
|
public static <T> List<T> entityToVoList(Collection<?> sourceList, Class<T> target) {
|
|
if (sourceList == null) {
|
|
return null;
|
|
}
|
|
List<T> targetList = new ArrayList<>(sourceList.size());
|
|
|
|
try {
|
|
for (Object source : sourceList) {
|
|
T targetObject = target.newInstance();
|
|
BeanUtils.copyProperties(source, targetObject);
|
|
targetList.add(targetObject);
|
|
}
|
|
} catch (Exception e) {
|
|
log.error("entityToVo error,source = {},target={}", sourceList, target, e);
|
|
}
|
|
return targetList;
|
|
}
|
|
|
|
}
|