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 ...
随机推荐
- 201621123010《Java程序设计》第7周学习总结
1. 本周学习总结 1.1思维导图:Java图形界面总结 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. addActionListener(new Actio ...
- vue.js 源代码学习笔记 ----- codegen.js
/* @flow */ import { genHandlers } from './events' import { baseWarn, pluckModuleFunction } from '.. ...
- js设计模式整理
单例模式 恶汉式单例 实例化时 懒汉式单例 调用时构造函数模式 1.实现一 function Car(model, year, miles) { this.model = model; this.ye ...
- C#中IEnumerable和IEnumerator区别
IEnumerator:是一个真正的集合访问器,提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型.IEnumerable: ...
- (转)OAuth 2.0的设计思路
OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R ...
- Echart参数详解收藏
最全: https://www.cnblogs.com/Kqingniao/p/5833419.html 柱形图: https://blog.csdn.net/qq_36330228/article/ ...
- ElasticSearch(一):windows下安装ElasticSearch6.3.2
前言 最近开始学习ElasticSearch,从官网上下载的最新版的es,但是网上安装教程大部分是早期5.X版本的,因为6.x版本出现了不同,所以记录下. 正文 1. 下载ElasticSearch ...
- test20181021 快速排序
题意 对于100%的数据,\(n,m \leq 10^5\) 分析 考场上打挂了. 最大值就是后半部分和减前半部分和. 最小是就是奇偶相减. 方案数类似进出栈序,就是catalan数 线段树维护即可, ...
- test20181024 hao
题意 分析 考场10分 直接\(O(nm)\)模拟即可. #include<cstdlib> #include<cstdio> #include<cmath> #i ...
- UNIX 时间戳 C#
/// 将Unix时间戳转换为DateTime类型时间 /// </summary> /// <param name="d">double 型数字</ ...