需要注意的就是把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的解决方式的更多相关文章

  1. Beanutils.copyProperties( )使用详情总结

    Beanutils.copyProperties( ) 一.简介: ​ BeanUtils提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行处理.我们知道,一个 ...

  2. 小知识点BeanUtils.copyProperties

     通过BeanUtils.copyProperties可以时间拷贝对象中的值,下面的new String[]{"cid","agreeFlag"," ...

  3. 007-TreeMap、Map和Bean互转、BeanUtils.copyProperties(A,B)拷贝、URL编码解码、字符串补齐,随机字母数字串

    一.转换 1.1.TreeMap 有序Map 无序有序转换 使用默认构造方法: public TreeMap(Map<? extends K, ? extends V> m) 1.2.Ma ...

  4. 【对象属性复制】BeanUtils.copyProperties(obj1, obj2);

    实现对象的属性值复制,只会复制命名相同的文件. import org.springframework.beans.BeanUtils; BeanUtils.copyProperties(obj1, o ...

  5. spring: beanutils.copyproperties将一个对象的数据塞入到另一个对象中(合并对象)

    spring: beanutils.copyproperties将一个对象的数据塞入到另一个对象中(合并对象) 它的出现原因: BeanUtils提供对Java反射和自省API的包装.其主要目的是利用 ...

  6. 利用BeanUtils.copyProperties 克隆出新对象,避免对象重复问题

    1.经常用jQuery获取标签里面值val(),或者html(),text()等等,有次想把获取标签的全部html元素包括自己也用来操作,查询了半天发现$("#lefttr1"). ...

  7. 两个对象值转换的方法(BeanUtils.copyProperties与JSONObject.parseObject对比)

    将源对象赋值到目标对象方法: 方法一:BeanUtils.copyProperties(源对象, 目标对象); //org.springframework.beans.BeanUtils 方法二:目标 ...

  8. BeanUtils.copyProperties()方法和PropertyUtils.copyProperties()的区别

    首先两者来源于同一个包: import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.Prop ...

  9. BeanUtils.copyProperties() 用法--部分转

    把一个类对象copy到另一个类对象(这两个可以不同). 1.org.apache.commons.beanutils.BeanUtils.copyProperties(dest, src) org.s ...

随机推荐

  1. c#实现对登陆信息的反馈,实现对网站登录密码的扫描

    最近发现我们学校的电信上网改密码的页面很简单,没有验证码,于是我就很好奇,后来发现原来是我们学校的电信的那个改密码的页面有漏洞于是就可以通过扫描账号免费上网 原理就是对修改密码的页面进行POST请求 ...

  2. GPU编程自学7 —— 常量内存与事件

    深度学习的兴起,使得多线程以及GPU编程逐渐成为算法工程师无法规避的问题.这里主要记录自己的GPU自学历程. 目录 <GPU编程自学1 -- 引言> <GPU编程自学2 -- CUD ...

  3. 使用Junit进行Java单元测试

    1.新建一个Number类,该类中包含两个函数,求和.求差 2.在eclipse上安装Junit 右键test工程,选择“Properties”→“Java Build Path”→“Librarie ...

  4. 记一次git fatal: Unable to find remote helper for 'https'问题的解决

    登陆到远程linux服务器上,使用git, clone的时候报“fatal: Unable to find remote helper for 'https'”错,没管,绕过,使用git clone ...

  5. busybox microcom Segmentation fault

    /********************************************************************************* * busybox microco ...

  6. Nginx 设置rewrite规则是遇到的一个{}大括号引发的报错问题

    一个群友提到: 用nginx image_filter模块裁图,用!拼宽高能够实现,现在想用参数传宽高总是报错,配置如下:   location ~ ^/images/.* {     if ( $q ...

  7. python线程的GIL问题(全局解释器锁)

    造成原因: python ---> 支持线程操作 --->IO的同步和互斥 --> 加锁 ----> 超级锁,给解释器加锁--->解释器同一时刻只能解释一个线程 造成的后 ...

  8. SpringBoot实现网站注册,邮件激活码激活功能

    项目源码:https://gitee.com/smfx1314/springbootemail 上一篇文章已经讲到如何springboot如何实现邮件的发送,趁热打铁,这篇文章实现如下功能. 很多网站 ...

  9. swift3.0  代码创建经典界面的九宫图--优化篇

    在上一篇只是简单实现了九宫图效果,本章需要形成APP界面九宫图效果 override func viewDidLoad() { super.viewDidLoad() createnine() } / ...

  10. swift3.0 创建经典界面的九宫图

    网络上很多例子都是早期的 Object-C的效果,现在用到Swift3.0开发,故把网络上的例子翻译过来,达到基本的效果.可是现在这个还不算很满意,再下次继续进行优化 override func vi ...