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 ...
随机推荐
- Java9新特性
转载:http://blog.csdn.net/qq_32524177/article/details/77014757 写在前面的话:Java9来了,搜索了很多关于Java9的新特性,但文献不多,特 ...
- mysqlbinlog初识
mysql-binlog->解析mysql的binlog日志 mysql的binlog日志是什么? 数据目录下的日下文件就是mysql的binlog日志 mysql-bin.00001 mysq ...
- Webroot SecureAnywhere AntiVirus 2014 – 免费6个月
Webroot SecureAnywhere 是由webroot推出的一款云安全软件,除了能够清除病毒外,特点是体积小.强力查杀木马.间谍软件.Rootkit 等等,为你的个人私隐信息提供全面的保护. ...
- 『转』Emsisoft Anti-Malware 8刷Key教程 - 文字版
先分主机和客机,下载好 EAM8安装包 和 30天重置工具EAM Trial Reset 1.1.exe 1. 主机安装 Emsisoft Anti-Malware 8 并激活30天试用版 如果已 ...
- New Concept English Two 19 49
$课文47 嗜酒的鬼魂 481. A public house which was recently bought by Mr.Ian Thompson is up for sale. 伊恩.汤普森先 ...
- ubuntu16 Intellij Idea Install&config
1,下载idea二进制文件,有免费和不免费的. https://www.jetbrains.com/idea/download/#section=linux 2,解压下载文件. tar -xvzf . ...
- mysql sql语句高级写法
将user表的内容,插入到team_member表INSERT INTO team_member (Nike,HeadImageUrl) SELECT Nike,HeadImageUrl FROM u ...
- 20155322 2016-2017-2 《Java程序设计》第8周学习总结
20155322 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 第八周学习的主要内容是课本的第十四第十五章,主要学习了以下知识点: 了解NIO 会使用Cha ...
- 在Linux中批量修改字符串的命令
昨天一个朋友忽然问我,在Linux下如何批量修改字符串,当时瞬间懵逼了,完全想不起来....... 今天特意的重温了一下Linux下的一些常用命令,并将这个遗忘的批量修改字符串的命令记录下来(资料来自 ...
- 【转】linux sed命令详解
原文网址:http://www.iteye.com/topic/587673 1. Sed简介sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”( ...