一 先定义两个不同的类
这两个类的结构是不同的。
1 SysUser
package toolutils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SysUser implements Serializable {
private String id;
private String username;
private Integer age;
private String password;
private Integer status;
private List<SysRole> roles;
private Date createDate;
private static final long serialVersionUID = 1L;
}
2 SysUserVO
package toolutils;
import lombok.Data;
import java.util.Date;
import java.util.List;
@Data
public class SysUserVO {
private String id;
private String username;
private Integer age;
private Integer status;
private List<SysRole> roles;
private Date createDate;
private List<SysResource> resources;
}
二 BeanUtils.copyProperties拷贝测试
/**
* Copyright (C), 2020-2020, 软件公司
* FileName: BeanUtilsTest
* Author: cakin
* Date: 2020/5/8
* Description: 不同结构类拷贝
*/
package toolutils;
import org.springframework.beans.BeanUtils;
/**
* @ClassName: BeanUtilsTest
* @Description: 不同结构类拷贝
* @Date: 2020/5/8
* @Author: cakin
*/
public class BeanUtilsTest {
public static void main( String[] args ) {
SysUser v = new SysUser();
v.setId("1258674238592294913");
v.setUsername("cakin");
v.setAge(38);
v.setPassword("75fbae06dcf2eecade95992fbb41eaa2");
v.setStatus(1);
SysUserVO vo = new SysUserVO();
BeanUtils.copyProperties(v, vo);
System.out.println(vo);
}
}
三 测试结果
SysUserVO(id=1258674238592294913, username=cakin, age=38, status=1, roles=null, createDate=null, resources=null)
从结果来看,只拷贝字段名称相同的字段。
来源:oschina
链接:https://my.oschina.net/u/4383081/blog/4271497