一、List类型转换工具类
ps:数据分页的时候,转换当前页数据会比较好,不建议一次性全部转换。一般用于PO转DTO等。
package com.gzgs.study.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class BeanListUtils {
/**
*
* @param sourceList 源list
* @param target 目标类
* @return
* @param <T>
* @param <F>
*/
public static <T,F> List<F> copy(List<T> sourceList,Class<F> target){
if(!CollectionUtils.isEmpty(sourceList)){
List<F> targetList = new ArrayList<>();
for (T t : sourceList){
try {
F f = target.newInstance();
BeanUtils.copyProperties(t,f);
targetList.add(f);
}catch (Exception e){
log.error("list类型转换异常:{}",e);
}
}
return targetList;
}else {
log.error("list类型转换异常:源list为null");
return null;
}
}
}