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-09-12 16:09:45 发布
该代码实现了一个BeanCopyUtils类,扩展了BeanUtils的功能。主要包含两个方法:一是根据源对象创建并赋值目标对象;二是批量拷贝列表中的对象到指定类型的目标列表。这两个方法在处理对象属性复制时非常有用。
1万+

被折叠的 条评论
为什么被折叠?



