import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class aaaaa extends BeanUtils{
public static <S, T> T convertTo(S source, Supplier<T> targetSupplier) {
return convertTo(source, targetSupplier, null);
}
/**
* 轉換對象
*
* @param source 源對象
* @param targetSupplier 目標對象供應方
* @param callBack 回調方法
* @param <S> 源對象類型
* @param <T> 目標對象類型
* @return 目標對象
*/
public static <S, T> T convertTo(S source, Supplier<T> targetSupplier, ConvertCallBack<S, T> callBack) {
if (null == source || null == targetSupplier) {
return null;
}
T target = targetSupplier.get();
BeanUtils.copyProperties(source, target);
if (callBack != null) {
callBack.callBack(source, target);
}
return target;
}
public static <S, T> List<T> convertListTo(List<S> sources, Supplier<T> targetSupplier) {
return convertListTo(sources, targetSupplier, null);
}
/**
* 轉換對象
*
* @param sources 源對象list
* @param targetSupplier 目標對象供應方
* @param callBack 回調方法
* @param <S> 源對象類型
* @param <T> 目標對象類型
* @return 目標對象list
*/
public static <S, T> List<T> convertListTo(List<S> sources, Supplier<T> targetSupplier, ConvertCallBack<S, T> callBack) {
if (null == sources || null == targetSupplier) {
return null;
}
List<T> list = new ArrayList<>(sources.size());
for (S source : sources) {
T target = targetSupplier.get();
BeanUtils.copyProperties(source, target);
if (callBack != null) {
callBack.callBack(source, target);
}
list.add(target);
}
return list;
}
/**
* 回調接口
*
* @param <S> 源對象類型
* @param <T> 目標對象類型
*/
@FunctionalInterface
public interface ConvertCallBack<S, T> {
void callBack(S t, T s);
}
}
測試類
//EntAppealDTO和AppealInitateBO字段屬性一樣,自己隨意DIY吧。
List<EntAppealDTO> entAppealDTO=new ArrayList<>();
EntAppealDTO dto=new EntAppealDTO();
dto.setAppealDate(new Date());
List<EntAppealFilesDTO> entAppealFilesDTOList=new ArrayList<>();
EntAppealFilesDTO filesDTO=new EntAppealFilesDTO();
filesDTO.setFileId("123456");
entAppealFilesDTOList.add(filesDTO);
dto.setEntAppealFilesDTOList(entAppealFilesDTOList);
entAppealDTO.add(dto);
//通過lambda表達式特殊處理個別字段
List<AppealInitateBO> initateBOList=aaaaa.convertListTo(entAppealDTO,AppealInitateBO::new,(s, t) -> t.setCityCode(s.getEntAppealFilesDTOList().get(0).getFileId()));
//不帶單獨處理
//List<AppealInitateBO> initateBOList=aaaaa.convertListTo(entAppealDTO,AppealInitateBO::new);
System.out.println(initateBOList);
工具類2
//單獨實體類拷貝轉換
public static <T,F> T toDtoOrInfo(F f, T d){
BeanUtils.copyProperties(f,d);
return d;
}
//嵌套list實體類拷貝轉換
public static <S,T> List<T> toInfoList(List<S> sList, Class<T> tClass){
List<T> tList = new ArrayList<>();
try {
Constructor<?> constructor = tClass.getConstructor();
sList.forEach( s -> {
Object o;
try {
o = constructor.newInstance();
tList.add((T)toDtoOrInfo(s, o));
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
});
} catch ( NoSuchMethodException e) {
e.printStackTrace();
}
return tList;
}
//使用
toDtoOrInfo(源實體類,new 目標實體類)
toInfoList(源實體類,目標實體類.class)
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。