BeanUtils.copyProperties不能copy复杂对象List的解决方式
需要注意的就是把List拆分,遍历add,然后把list设置到返回对象中
package test.test; import java.util.ArrayList;
import java.util.List; import org.springframework.beans.BeanUtils; import com.alibaba.fastjson.JSON; import test.entity.Book;
import test.entity.Classs;
import test.entity.Student;
import test.entity.Teacher; public class Test {
public static void main(String[] args) {
Classs c = new Classs();
c.setClassName("班级1"); List<Student> sl1 = new ArrayList<>();
List<Book> bl1 = new ArrayList<>();
Student s1 = new Student();
Book b1 = new Book();
b1.setName("傲慢与偏见");
b1.setNumber(498);
bl1.add(b1);
s1.setBookList(bl1);
s1.setAddress("龙华民治街道");
s1.setClassName("班级1");
s1.setName("小红"); sl1.add(s1);
c.setStuList(sl1); List<Teacher> tl1 = new ArrayList<>();
Teacher t1 = new Teacher();
t1.setName("张三");
t1.setAddress("深圳南山");
tl1.add(t1);
c.setTeaList(tl1); // System.out.println(JSON.toJSON(c)); test.entity2.Classs c2 = new test.entity2.Classs(); BeanUtils.copyProperties(c, c2); List<test.entity2.Student> sl2 = new ArrayList<>();
for (Student stu1 : sl1) {
test.entity2.Student stu2 = new test.entity2.Student();
BeanUtils.copyProperties(stu1, stu2); List<test.entity2.Book> bl2 = new ArrayList<>();
for (Book bo1 : stu1.getBookList()) {
test.entity2.Book b2 = new test.entity2.Book();
BeanUtils.copyProperties(bo1, b2); bl2.add(b2);
}
stu2.setBookList(bl2);
sl2.add(stu2);
}
c2.setStuList(sl2); List<test.entity2.Teacher> tl2 = new ArrayList<>();
for (Teacher tea1 : tl1) {
test.entity2.Teacher tea2 = new test.entity2.Teacher(); BeanUtils.copyProperties(tea1, tea2);
tl2.add(tea2); }
c2.setTeaList(tl2); System.out.println(JSON.toJSON(c2));
}
}
BeanUtils.copyProperties不能copy复杂对象List的解决方式的更多相关文章
- Beanutils.copyProperties( )使用详情总结
Beanutils.copyProperties( ) 一.简介: BeanUtils提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行处理.我们知道,一个 ...
- 小知识点BeanUtils.copyProperties
通过BeanUtils.copyProperties可以时间拷贝对象中的值,下面的new String[]{"cid","agreeFlag"," ...
- 007-TreeMap、Map和Bean互转、BeanUtils.copyProperties(A,B)拷贝、URL编码解码、字符串补齐,随机字母数字串
一.转换 1.1.TreeMap 有序Map 无序有序转换 使用默认构造方法: public TreeMap(Map<? extends K, ? extends V> m) 1.2.Ma ...
- 【对象属性复制】BeanUtils.copyProperties(obj1, obj2);
实现对象的属性值复制,只会复制命名相同的文件. import org.springframework.beans.BeanUtils; BeanUtils.copyProperties(obj1, o ...
- spring: beanutils.copyproperties将一个对象的数据塞入到另一个对象中(合并对象)
spring: beanutils.copyproperties将一个对象的数据塞入到另一个对象中(合并对象) 它的出现原因: BeanUtils提供对Java反射和自省API的包装.其主要目的是利用 ...
- 利用BeanUtils.copyProperties 克隆出新对象,避免对象重复问题
1.经常用jQuery获取标签里面值val(),或者html(),text()等等,有次想把获取标签的全部html元素包括自己也用来操作,查询了半天发现$("#lefttr1"). ...
- 两个对象值转换的方法(BeanUtils.copyProperties与JSONObject.parseObject对比)
将源对象赋值到目标对象方法: 方法一:BeanUtils.copyProperties(源对象, 目标对象); //org.springframework.beans.BeanUtils 方法二:目标 ...
- BeanUtils.copyProperties()方法和PropertyUtils.copyProperties()的区别
首先两者来源于同一个包: import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.Prop ...
- BeanUtils.copyProperties() 用法--部分转
把一个类对象copy到另一个类对象(这两个可以不同). 1.org.apache.commons.beanutils.BeanUtils.copyProperties(dest, src) org.s ...
随机推荐
- HDU 5186 zhx's submissions 模拟,细节 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=5186 题意是分别对每一位做b进制加法,但是不要进位 模拟,注意:1 去掉前置0 2 当结果为0时输出0,而不是全 ...
- hdu 3264 09 宁波 现场 E - Open-air shopping malls 计算几何 二分 圆相交面积 难度:1
Description The city of M is a famous shopping city and its open-air shopping malls are extremely at ...
- 记录Tomcat8.5文件上传,文件权限无法访问
部署一个服务,文件上传本地可以,但是在Linux上通过docker发布到容器上,文件上传后,没有权限访问,查了好久才发现是Tomcat8.5的问题,低版本没有这个问题,现记录下. tomcat/bin ...
- linux系统之间共享文件(CentOS6)
Server IP: 192.168.2.128 nfs, rpcbind(portmap) installed Client IP: 192.168.2.254 nfs, rpcbind(portm ...
- docx文件怎样打开 - 转
如何打开docx文件?在office2007及2010退出多年后,诸如docx.xlsx.pptx类文件越来越多,我们从网络下载或者别人复制过来的这类文件越来越多.docx文件怎样打开呢?下面有图小站 ...
- 微信逆向工程之远程操作Mac
远程控制指令: (功能-指令-是否开启) macbook控制: 屏幕保护-ScreenSave-开启 锁屏-LockScreen-开启 休眠-Sleep-开启 关机-Shutdown-开启 重启-Re ...
- C语言extern关键字使用
在chinaunix上看见一篇转载的文章,觉得特别好,关于extern使用的解释: 参考链接:http://doc.chinaunix.net/CPP/201206/2248432.shtml 在C语 ...
- PHP 中的文本截取分析之效率
PHP 中的文本截取分析之效率 在使用 ThinkPHP 或 Laravel 时都会有用到文本截取的帮助函数. 分别是 msubstr (ThinkPHP 3,ThinkPHP 5 没找到) mb_s ...
- POJ3070 矩阵快速幂模板
题目:http://poj.org/problem?id=3070 矩阵快速幂模板.mod写到乘法的定义部分就行了. 别忘了 I ( ) 和 i n i t ( ) 要传引用! #include< ...
- 解决标准FPGA资源丰富却浪费的问题
FPGA以计算速度快.资源丰富.可编程著称,之前一直应用于高速数字信号领域和ASIC验证.随着逻辑资源的丰富和编程工具的改进,FPGA在机器学习和硬件加速上得到越来越多的重视,目前数据中心已经大量采用 ...