欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

如何使用BeanUtils.copyProperties进行对象之间的属性赋值

时间:2021-05-19 09:39:00|栏目:JAVA代码|点击:

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);
  }
 }
}

上一篇:spring boot如何添加拦截器

栏    目:JAVA代码

下一篇:Jmeter自定义函数base64加密实现过程解析

本文标题:如何使用BeanUtils.copyProperties进行对象之间的属性赋值

本文地址:http://www.codeinn.net/misctech/124231.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有