引入spring-beans的依赖,其版本为5.3.3
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.3</version>
</dependency>
第一种使用用法: BeanUtils.copyProperties(A, B, new String[]{"id","age"})
将A的属性全部拷贝到B中,但是属性字段id和age将会被忽略掉,不进行属性值拷贝
构建的实体类:
import lombok.Data;
import java.util.Date;
@Data
public class TbUser{
private Integer id;
private String name;
private Integer age;
private Date createTime;
public TbUser(Integer id, String name, Integer age, Date createTime) {
this.id = id;
this.name = name;
this.age = age;
this.createTime = createTime;
}
}
构建的VO传输对象:
import lombok.Data;
@Data
public class UserVo {
private Integer id;
private String name;
private Integer age;
}
测试类:
TbUser user = new TbUser(2,"Morris", 5, new Date());
UserVo userVo=new UserVo();
BeanUtils.copyProperties(user,userVo,"id","age");
System.out.println(userVo);
输出结果:
UserVo(id=null, name=Morris, age=null)