Java

1、使用org.springframework.beans.BeanUtils.copyProperties方法进行对象之间属性的赋值,避免通过getset方法一个一个属性的赋值

  1. /**
  2. * 对象属性拷贝 <br>
  3. * 将源对象的属性拷贝到目标对象
  4. *
  5. * @param source 源对象
  6. * @param target 目标对象
  7. */
  8. public static void copyProperties(Object source, Object target) {
  9. try {
  10. BeanUtils.copyProperties(source, target);
  11. } catch (BeansException e) {
  12. LOGGER.error("BeanUtil property copy failed :BeansException", e);
  13. } catch (Exception e) {
  14. LOGGER.error("BeanUtil property copy failed:Exception", e);
  15. }
  16. }

2、List集合之间的对象属性赋值

  1. /**
  2. * @param input 输入集合
  3. * @param clzz 输出集合类型
  4. * @param <E> 输入集合类型
  5. * @param <T> 输出集合类型
  6. * @return 返回集合
  7. */
  8. public static <E, T> List<T> convertList2List(List<E> input, Class<T> clzz) {
  9. List<T> output = Lists.newArrayList();
  10. if (CollectionUtils.isNotEmpty(input)) {
  11. for (E source : input) {
  12. T target = BeanUtils.instantiate(clzz);
  13. BeanUtil.copyProperties(source, target);
  14. output.add(target);
  15. }
  16. }
  17. return output;
  18. }

比如有两个类,User和Employee,将存储Employee对象的List赋给存储User对象的List。
User类:

  1. public class User {
  2. private String name;
  3. private Integer age;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. public Integer getAge() {
  11. return age;
  12. }
  13. public void setAge(Integer age) {
  14. this.age = age;
  15. }
  16. @Override
  17. public String toString() {
  18. return "User{" +
  19. "name='" + name + '\'' +
  20. ", age=" + age +
  21. '}';
  22. }
  23. }

Employee类:

  1. public class Employee {
  2. private String name;
  3. private Integer age;
  4. private String dept;
  5. public Employee(String name, Integer age, String dept) {
  6. this.name = name;
  7. this.age = age;
  8. this.dept = dept;
  9. }
  10. public String getName() {
  11. return name;
  12. }
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16. public Integer getAge() {
  17. return age;
  18. }
  19. public void setAge(Integer age) {
  20. this.age = age;
  21. }
  22. public String getDept() {
  23. return dept;
  24. }
  25. public void setDept(String dept) {
  26. this.dept = dept;
  27. }
  28. @Override
  29. public String toString() {
  30. return "Employee{" +
  31. "name='" + name + '\'' +
  32. ", age=" + age +
  33. ", dept='" + dept + '\'' +
  34. '}';
  35. }
  36. }

测试类:

  1. @RunWith(PowerMockRunner.class)
  2. public class TestUtil
  3. {
  4. @Test
  5. public void test(){
  6. Employee ee1=new Employee("A",21,"it");
  7. Employee ee2=new Employee("B",23,"account");
  8. User user=new User();
  9. BeanUtil.copyProperties(ee1, user);
  10. System.out.println(user);
  11. System.out.println("-------------分割线--------------");
  12. List<User> output=new ArrayList<>();
  13. List<Employee> source= Arrays.asList(ee1,ee2);
  14. output=BeanUtil.convertList2List(source,User.class);
  15. for (User str:output) {
  16. System.out.println(str);
  17. }
  18. }
  19. }

2021-07-21-21-47-24-092229.png