public class BeanCopyUtils extends BeanUtils {
/**
* 根据现有对象的属性创建目标对象,并赋值
* @param source 结果
* @param target 入参
* @param <T>
* @return
* @throws Exception
*/
public static <T> T copyObject(Object source,Class<T> target) {
T temp = null;
try {
temp = target.newInstance();
if (null != source) {
BeanUtils.copyProperties(source, temp);
}
} catch (Exception e) {
e.printStackTrace();
}
return temp;
}
/**
* 拷贝集合
* @param source 结果
* @param target 入参
* @param <T>
* @param <S>
* @return
*/
public static <T,S> List<T> copyList(List<S> source, Class<T> target){
List<T> list = new ArrayList<>();
if (null != source && source.size() > 0) {
for (Object obj : source) {
list.add(BeanCopyUtils.copyObject(obj, target));
}
}
return list;
}
JavaBean拷贝工具类
最新推荐文章于 2024-05-15 18:04:03 发布