修改convert
This commit is contained in:
parent
14f4975ebf
commit
234a8d41cd
|
|
@ -0,0 +1,56 @@
|
|||
package com.ruoyi.common.core.utils;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 通用转换器基类
|
||||
*
|
||||
* @param <F> Source类型
|
||||
* @param <T> Target类型
|
||||
* @author ruoyi
|
||||
*/
|
||||
public abstract class BaseConvert<F, T> {
|
||||
|
||||
private final Class<F> sourceClass;
|
||||
private final Class<T> targetClass;
|
||||
|
||||
protected BaseConvert(Class<F> sourceClass, Class<T> targetClass) {
|
||||
this.sourceClass = sourceClass;
|
||||
this.targetClass = targetClass;
|
||||
}
|
||||
|
||||
protected T innerFrom(F source) {
|
||||
if (source == null) return null;
|
||||
try {
|
||||
T target = targetClass.getDeclaredConstructor().newInstance();
|
||||
BeanUtils.copyProperties(source, target);
|
||||
return target;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected F innerTo(T target) {
|
||||
if (target == null) return null;
|
||||
try {
|
||||
F source = sourceClass.getDeclaredConstructor().newInstance();
|
||||
BeanUtils.copyProperties(target, source);
|
||||
return source;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected List<T> innerFromList(List<F> sourceList) {
|
||||
if (sourceList == null) return null;
|
||||
return sourceList.stream().map(this::innerFrom).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
protected List<F> innerToList(List<T> targetList) {
|
||||
if (targetList == null) return null;
|
||||
return targetList.stream().map(this::innerTo).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue