当前位置:网站首页>一个测试类了解BeanUtils.copyProperties
一个测试类了解BeanUtils.copyProperties
2022-07-27 00:36:00 【深情以改】
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.beans.BeanUtils;
import java.math.BigDecimal;
public class Test2 {
public static void main(String[] args) {
UserDto userDto = new UserDto();
//BeanUtils.copyProperties(a,b)是将a的数据拷贝给b
User user = new User();
Salary salary = new Salary();
//a中与b中相同的属性才会被替换(不同需要手动set),复制类即b不管是否有值;
BeanUtils.copyProperties(userDto, user);
//b中的存在的属性,a中没有默认null
System.out.println(user + "--" + user.getDefaultClass().getInnerNum());
//User(id=2, name=2, sex=2, [email protected], innerClass=User.InnerClass(innerNum=4))--2
BeanUtils.copyProperties(userDto, salary);
System.out.println(salary);
//Salary(id=2, salary=null, idCard=null)
}
}
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
class DefaultClass {
Integer innerNum = 4;
}
@ToString
@Setter//Spring的BeanUtils的CopyProperties方法复制类需要有setter方法;
@Getter
class User {
private Integer id = 4;
private String name = "4";
private String sex = "4";
private DefaultClass defaultClass = new DefaultClass(4);
private InnerClass innerClass = new InnerClass();
// private User.InnerClass innerClass2 = new User.InnerClass();属性完全相同的内部类,但是不是同一个内部类即不是同一个对象,不会复制
@Data
class InnerClass {
Integer innerNum = 4;
}
}
@Data
class Salary {
private Integer id;
private BigDecimal salary;
private String idCard;
}
@Getter
//Spring的BeanUtils的CopyProperties方法被复制类需要有getter方法;
class UserDto {
private Integer id = 2;
private String name = "2";
private String sex = "2";
private DefaultClass defaultClass = new DefaultClass(2);
private InnerClass innerClass = new InnerClass();
// private UserSalaryDto.InnerClass innerClass2 = new UserSalaryDto.InnerClass();
class InnerClass {
Integer innerNum = 2;
}
}
边栏推荐
- 次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
- 手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
- [动态规划中等题] LeetCode 198. 打家劫舍 740. 删除并获得点数
- 从ACL 2022 Onsite经历看NLP热点
- 软件测试相关试题知识点
- Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
- [untitled]
- idea中常用的快捷键
- typora详细教程
- 快速排序(Quick sort)
猜你喜欢

MySQL master-slave database configuration based on docker for Ubuntu

ArduinoUNO驱动RGB模块全彩效果示例

static关键字

CS224W fall 1.2 Applications of Graph ML

How to do the system security test? Let's talk about it in detail

Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round

CS224W fall 1.2 Applications of Graph ML

setTimeout第一个参数应该注意的地方
软件测试面试常见问题及答案(发散思维、接口、性能、概念、)

系统安全测试要怎么做,详细来说说
随机推荐
素因子分解--C(gcc)--PTA
What is a process?
Web3.0世界知识体系分享-什么是Web3.0
Cookie addition, deletion, modification and query methods
How to do the system security test? Let's talk about it in detail
论构造函数的原型是谁
I heard that you knelt on the interface test during the interview?
Thread.Sleep(0)的作用
Debezium系列之:基于debezium offset拉取历史数据,确保数据没有丢失
数据资产管理的概念
C language program compilation (preprocessing)
Plato Farm全新玩法,套利ePLATO稳获超高收益
哪家券商开户买REITs基金比较安全?
Database read-write separation and database and table segmentation
Talk about connection pools and threads
Static keyword
[redis] quick start
White box test case design (my grandfather can understand it)
How small programs help the new model of smart home ecology
确定了,2022下半年软考报名8月开始