如何使用BeanUtils.copyProperties进行对象之间的属性赋值
作者:Terisadeng 时间:2023-10-17 19:01:40
1、使用org.springframework.beans.BeanUtils.copyProperties方法进行对象之间属性的赋值,避免通过get、set方法一个一个属性的赋值
/**
* 对象属性拷贝 <br>
* 将源对象的属性拷贝到目标对象
*
* @param source 源对象
* @param target 目标对象
*/
public static void copyProperties(Object source, Object target) {
try {
BeanUtils.copyProperties(source, target);
} catch (BeansException e) {
LOGGER.error("BeanUtil property copy failed :BeansException", e);
} catch (Exception e) {
LOGGER.error("BeanUtil property copy failed:Exception", e);
}
}
2、List集合之间的对象属性赋值
/**
* @param input 输入集合
* @param clzz 输出集合类型
* @param <E> 输入集合类型
* @param <T> 输出集合类型
* @return 返回集合
*/
public static <E, T> List<T> convertList2List(List<E> input, Class<T> clzz) {
List<T> output = Lists.newArrayList();
if (CollectionUtils.isNotEmpty(input)) {
for (E source : input) {
T target = BeanUtils.instantiate(clzz);
BeanUtil.copyProperties(source, target);
output.add(target);
}
}
return output;
}
比如有两个类,User和Employee,将存储Employee对象的List赋给存储User对象的List。
User类:
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Employee类:
public class Employee {
private String name;
private Integer age;
private String dept;
public Employee(String name, Integer age, String dept) {
this.name = name;
this.age = age;
this.dept = dept;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", dept='" + dept + '\'' +
'}';
}
}
测试类:
@RunWith(PowerMockRunner.class)
public class TestUtil
{
@Test
public void test(){
Employee ee1=new Employee("A",21,"it");
Employee ee2=new Employee("B",23,"account");
User user=new User();
BeanUtil.copyProperties(ee1, user);
System.out.println(user);
System.out.println("-------------分割线--------------");
List<User> output=new ArrayList<>();
List<Employee> source= Arrays.asList(ee1,ee2);
output=BeanUtil.convertList2List(source,User.class);
for (User str:output) {
System.out.println(str);
}
}
}
来源:https://blog.csdn.net/dongyuxu342719/article/details/90242904
标签:BeanUtils.copyProperties,对象赋值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Spring 中 Bean 对象的存储和取出
2023-08-06 02:42:46
![](https://img.aspxhome.com/file/2023/2/57832_0s.png)
Android实现背景图滑动变大松开回弹效果
2022-10-15 10:45:15
![](https://img.aspxhome.com/file/2023/9/108239_0s.jpg)
Spring4下validation数据校验无效(maven)的解决
2022-01-24 03:01:04
Java实现人机猜拳游戏
2023-10-16 08:47:56
![](https://img.aspxhome.com/file/2023/5/80205_0s.png)
简单了解springboot的jar包部署步骤
2021-07-02 14:22:48
java状态机方案解决订单状态扭转示例详解
2022-01-10 02:55:43
![](https://img.aspxhome.com/file/2023/8/87208_0s.jpg)
RabbitMQ消息中间件示例详解
2021-12-10 23:09:58
Java生产1-100的随机数简单实例(分享)
2021-08-15 02:52:28
Spring Native项目实战(体验79毫秒启动springboot应用)
2022-03-14 22:18:42
![](https://img.aspxhome.com/file/2023/1/78361_0s.png)
Android开发应用中Broadcast Receiver组件详解
2023-04-25 09:35:35
![](https://img.aspxhome.com/file/2023/1/122231_0s.png)
深入理解Java虚拟机 JVM 内存结构
2022-05-01 20:49:57
![](https://img.aspxhome.com/file/2023/7/103987_0s.png)
java简单工厂模式实例及讲解
2021-09-19 14:08:39
Android Jetpack架构组件 ViewModel详解
2021-09-08 00:29:20
![](https://img.aspxhome.com/file/2023/1/137711_0s.png)
轻松学习C#的结构和类
2023-12-10 13:46:19
C#实现字符串与图片的Base64编码转换操作示例
2021-07-06 14:29:21
Java安全-ClassLoader
2023-08-18 02:12:21
![](https://img.aspxhome.com/file/2023/9/80329_0s.png)
SpringBoot 整合 Shiro 密码登录与邮件验证码登录功能(多 Realm 认证)
2023-06-23 02:45:28
SpringBoot实现单文件与多文件上传功能
2023-03-22 23:44:11
![](https://img.aspxhome.com/file/2023/9/76229_0s.png)
Java编译器用maven打war包出错解决办法
2022-01-18 06:13:29
![](https://img.aspxhome.com/file/2023/9/119129_0s.png)
关于MyBatis结果映射的实例总结
2022-02-21 02:31:31
![](https://img.aspxhome.com/file/2023/1/128911_0s.jpg)