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);
}
/**
* 轉(zhuǎn)換對(duì)象
*
* @param source 源對(duì)象
* @param targetSupplier 目標(biāo)對(duì)象供應(yīng)方
* @param callBack 回調(diào)方法
* @param <S> 源對(duì)象類(lèi)型
* @param <T> 目標(biāo)對(duì)象類(lèi)型
* @return 目標(biāo)對(duì)象
*/
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);
}
/**
* 轉(zhuǎn)換對(duì)象
*
* @param sources 源對(duì)象list
* @param targetSupplier 目標(biāo)對(duì)象供應(yīng)方
* @param callBack 回調(diào)方法
* @param <S> 源對(duì)象類(lèi)型
* @param <T> 目標(biāo)對(duì)象類(lèi)型
* @return 目標(biāo)對(duì)象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;
}
/**
* 回調(diào)接口
*
* @param <S> 源對(duì)象類(lèi)型
* @param <T> 目標(biāo)對(duì)象類(lèi)型
*/
@FunctionalInterface
public interface ConvertCallBack<S, T> {
void callBack(S t, T s);
}
}
測(cè)試類(lèi)
//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);
//通過(guò)lambda表達(dá)式特殊處理個(gè)別字段
List<AppealInitateBO> initateBOList=aaaaa.convertListTo(entAppealDTO,AppealInitateBO::new,(s, t) -> t.setCityCode(s.getEntAppealFilesDTOList().get(0).getFileId()));
//不帶單獨(dú)處理
//List<AppealInitateBO> initateBOList=aaaaa.convertListTo(entAppealDTO,AppealInitateBO::new);
System.out.println(initateBOList);
工具類(lèi)2
//單獨(dú)實(shí)體類(lèi)拷貝轉(zhuǎn)換
public static <T,F> T toDtoOrInfo(F f, T d){
BeanUtils.copyProperties(f,d);
return d;
}
//嵌套list實(shí)體類(lèi)拷貝轉(zhuǎn)換
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(源實(shí)體類(lèi),new 目標(biāo)實(shí)體類(lèi))
toInfoList(源實(shí)體類(lèi),目標(biāo)實(shí)體類(lèi).class)