BeanUtils.copyProperties拷贝结构不同类测试

点点圈 提交于 2020-12-02 05:59:47

一 先定义两个不同的类

这两个类的结构是不同的。

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)

从结果来看,只拷贝字段名称相同的字段。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!