一、作用
用来做对象间的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