Java对象之间的属性赋值

对象之间的属性赋值

主要通过org.springframework.beans.BeanUtils.copyProperties方法进行对象之间的属性赋值。

赋值前提 -- 两个对象要赋值的属性名需要保持一致

对象赋值

# 将source对象赋值给target对象
BeanUtils.copyProperties(source, target);

List之间的对象赋值

/**
 * @param input 输入集合
 * @param clazz  输出集合类型
 * @param <E>   输入集合类型
 * @param <T>   输出集合类型
 * @return 返回集合
 */
public static <E, T> List<T> convertListToList(List<E> input, Class<T> clazz) {
    List<T> output = Lists.newArrayList();
    if (CollectionUtils.isNotEmpty(input)) {
        for (E source : input) {
            T target = BeanUtils.instantiate(clazz);
            BeanUtils.copyProperties(source, target);
            output.add(target);
        }
    }
    return output;
}

示例

有以下三个类,User、Cust、Student:

@Data
public class User {
    private String name;
    private String password;
    private String email;
}

@Data
public class Cust {
    private String name;
    private String password;
    private String email;
    private String phone;
}

@Data
public class Student {
    private String name;
    private String password;
}

工具类:

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * @Description: 对象之间进行赋值
 * @Date: 2020/9/25 9:55
 */
public class AssignmentObject {

    /**
     * 对象属性拷贝
     * 将源对象的属性拷贝到目标对象
     * @param source 源对象
     * @param target 目标对象
     */
    public static void copyProperties(Object source, Object target) {
        BeanUtils.copyProperties(source, target);
    }

    /**
     * @param input 输入集合
     * @param clazz  输出集合类型
     * @param <E>   输入集合类型
     * @param <T>   输出集合类型
     * @return 返回集合
     */
    public static <E, T> List<T> convertListToList(List<E> input, Class<T> clazz) {
        List<T> output = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(input)) {
            for (E source : input) {
                T target = BeanUtils.instantiate(clazz);
                BeanUtils.copyProperties(source, target);
                output.add(target);
            }
        }
        return output;
    }

}

测试类:分别测试对象之间的属性赋值与List之间的属性赋值

public class Demo {

    public static void main(String[] args) {

        User user = new User();
        user.setName("admin");
        user.setPassword("admin");
        user.setEmail("123@qq.com");
        System.out.println("user:" + user);
        // 将user的属性分别赋给cust和student
        Cust cust = new Cust();
        AssignmentObject.copyProperties(user, cust);
        System.out.println("cust:" + cust);

        Student student = new Student();
        AssignmentObject.copyProperties(user, student);
        System.out.println("student:" + student);

        User user1 = new User();
        user1.setName("demo");
        user1.setPassword("demo");
        user1.setEmail("demo@qq.com");
        System.out.println("user1:" + user);

        // 将users赋值给List<Cust>和List<Student>
        List<User> users = new ArrayList<>();
        users.add(user);
        users.add(user1);
        System.out.println("users:" + users);
        
        List<Cust> custs = AssignmentObject.convertListToList(users, Cust.class);
        System.out.println("custs:" + custs);
        List<Student> students = AssignmentObject.convertListToList(users, Student.class);
        System.out.println("students:" + students);

    }

}

参考文章:
CSDN:使用BeanUtils.copyProperties进行对象之间的属性赋值

posted @ 2020-09-25 10:29  缘木与鱼  阅读(4547)  评论(0编辑  收藏  举报