package com.example.demo_mybatis.util;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
public class ConvertUtil {
public static <T> T convertObj(Object req, T resp){
if (req==null){
return null;
}
BeanUtils.copyProperties(req,resp);
return resp;
}
public static <E,T> List<E> convertList(List<T> req, Class<E> target){
if (req==null){
return null;
}
if (target==null){
return null;
}
if (req.isEmpty()){
return new ArrayList<>();
}
List<E> resp = new ArrayList<>(req.size());
req.forEach(s->{
E e = null;
try {
e = target.newInstance();
BeanUtils.copyProperties(s,e);
resp.add(e);
} catch (Exception e1) {
e1.printStackTrace();
}
});
return resp;
}
}