@Override
public Result<Boolean> saveLog(GoodsEsLogDto goodsEsLogDto) {
GoodsEsLog goodsEsLog = ConvertUtils.convert(goodsEsLogDto,GoodsEsLog.class);
goodsEsLogDao.insert(goodsEsLog);
return Result.success(true);
}
工具类
package com.greenchoice.common.util;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
/**
* @author River
*/
public class ConvertUtils {
public static <T, E> E convert(T source, Class<E> targetClass) {
E target = null;
try {
target = targetClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
BeanUtils.copyProperties(source, target);
return target;
}
public static <T, E> List<E> convertList(List<T> sourceList, Class<E> targetClass) {
List<E> targetList = new ArrayList<>();
for (T source : sourceList) {
E target = convert(source, targetClass);
targetList.add(target);
}
return targetList;
}
}