BeanUtils.copyProperties()参数的赋值顺序说明

时间:2022-03-06 02:37:13

 

BeanUtils.copyProperties()参数的赋值顺序

BeanUtils.copyProperties(x,y)有两个不同的jar包,引入不同的包,赋值的顺序不一样。

分别是:

org.springframework.beans.BeanUtils
org.apache.commons.beanutils.BeanUtils

引入 1 的话,是将 x 赋值给 y

引入 2 的话,是将 y 赋值给 x

 

BeanUtils.copyProperties初体验,及其参数含义解释

 

用处

BeanUtils.copyProperties是org.springframework.beans.BeanUtils;包下得一个用于对象间得属性赋值,避免了常规操作得一个个get再set属性值,非常好用

 

案例: 创建一个源类:source

 private String username;
    private String password;
    private String email;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "book{" +
                "username="" + username + """ +
                ", password="" + password + """ +
                ", email="" + email + """ +
                "}";
    }
}

 

创建一个目标target源类

 private String username;
    private String password;
    private String email;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "book{" +
                "username="" + username + """ +
                ", password="" + password + """ +
                ", email="" + email + """ +
                "}";
    }
}

 

创建测试类test

import com.sdqj.system_integration.domain.book;
import com.sdqj.system_integration.domain.book2;
import org.springframework.beans.BeanUtils;

public class test {
    public static void main(String[] args) {
        book bo=new book();
        bo.setEmail("1111");
        bo.setPassword("222");
        bo.setUsername("333");
        book2 bo2=new book2();
        BeanUtils.copyProperties(bo,bo2);
        System.out.println(bo.toString());
        System.out.println(bo2.toString());
    }
}

结果展示:

BeanUtils.copyProperties()参数的赋值顺序说明

 

ignoreProperties参数

ignoreProperties参数意思是忽略某个参数,使其不被复制,可写多个被忽略得参数

 

案例

import com.sdqj.system_integration.domain.book;
import com.sdqj.system_integration.domain.book2;
import org.springframework.beans.BeanUtils;

public class test {
    public static void main(String[] args) {
        book bo=new book();
        bo.setEmail("1111");
        bo.setPassword("222");
        bo.setUsername("333");
        book2 bo2=new book2();
        BeanUtils.copyProperties(bo,bo2,"username","password");
        System.out.println(bo.toString());
        System.out.println(bo2.toString());
    }

结果展示:

BeanUtils.copyProperties()参数的赋值顺序说明

还有源代码中得Class<?> editable参数,不太了解其中得含义,但只能放target(目标类)

 

案例测试

 public static void main(String[] args) {
        book bo=new book();
        bo.setEmail("1111");
        bo.setPassword("222");
        bo.setUsername("333");
        book2 bo2=new book2();
        BeanUtils.copyProperties(bo,bo2,book2.class);
        System.out.println(bo.toString());
        System.out.println(bo2.toString());
    }

结果如下:

BeanUtils.copyProperties()参数的赋值顺序说明

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qiangzai725/article/details/91981960