BeanUtils.copyProperties作用

一、作用

用来做对象间的copy。

二、举例

(1)新建两个实体类

package org.example;

import java.util.Date;

public class Student {


    private String name;
    private String age;
    private String tel;
    private Boolean isno;
    private Date time;

    public Student() {
    }

    public Student(String name, String age, String tel, Boolean isno, Date time) {
        this.name = name;
        this.age = age;
        this.tel = tel;
        this.isno = isno;
        this.time = time;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public Boolean getIsno() {
        return isno;
    }

    public void setIsno(Boolean isno) {
        this.isno = isno;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", tel='" + tel + '\'' +
                ", isno=" + isno +
                ", time=" + time +
                '}';
    }
}

package org.example;

import java.util.Date;
import java.util.Objects;

public class Person {

    private String name;
    private int age;
    private String phone;
    private Boolean isno;
    private Date time;


    public Person() {
    }


    public Person(String name, int age, String phone, Boolean isno, Date time) {
        this.name = name;
        this.age = age;
        this.phone = phone;
        this.isno = isno;
        this.time = time;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Boolean getIsno() {
        return isno;
    }

    public void setIsno(Boolean isno) {
        this.isno = isno;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(name, person.name) && Objects.equals(age, person.age) && Objects.equals(phone, person.phone);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, phone);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", phone='" + phone + '\'' +
                ", isno=" + isno +
                ", time=" + time +
                '}';
    }
}

(2)pom引入依赖

因为BeanUtils依赖于spring-beans的依赖包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.22</version>
        </dependency>

(3)main方法测试

    public static void main(String[] args) {
        Student student = new Student("zhangsan", "23", "2324234",true,new Date());
        Person person = new Person("LISI", 12, "876543",false,new Date(2017,9,9));
        BeanUtils.copyProperties(student, person);
        System.out.println(student);
        System.out.println(person);
    }

(4)结果输出

Student{name='zhangsan', age=23, tel='2324234', isno=true, time=Mon May 06 17:49:42 CST 2024}
Person{name='zhangsan', age=23, phone='876543', isno=true, time=Mon May 06 17:49:42 CST 2024}

(5)结论

只要两个实体的属性名称和类型都相同,就可以做到拷贝
BeanUtils.copyProperties(A,B);
把A拷贝到B

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值