使用org.springframework.beans.BeanUtils..copyProperties(sourse, target)方法复制属性

使用org.springframework.beans.BeanUtils..copyProperties()方法进行copy两个类的属性。

注意:

1.源类Sourse中的属性需有get方法;

2.目标类Target中的属性需有set方法;

3.复制期间只会复制名字相同的属性值;

 

Sourse:

package com.yoyo.yotils.yoyo.copyProperties;

/**
 *
 * @author yoyo
 * @version $Id: Sourse.java, v 0.1 2019年06月17日 12:13 yoyo Exp $
 */
public class Sourse {
    private String name;

    private String sex;

    private int age;

    public Sourse(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    /**
     * Getter method for property <tt>name</tt>.
     *
     * @return property value of name
     */
    public String getName() {
        return name;
    }

    /**
     * Getter method for property <tt>sex</tt>.
     *
     * @return property value of sex
     */
    public String getSex() {
        return sex;
    }

    /**
     * Getter method for property <tt>age</tt>.
     *
     * @return property value of age
     */
    public int getAge() {
        return age;
    }
}

Target类:

package com.yoyo.yotils.yoyo.copyProperties;

import org.springframework.beans.BeanUtils;

/**
 *
 * @author yoyo
 * @version $Id: Target.java, v 0.1 2019年06月17日 12:14 yoyo Exp $
 */
public class Target {
    private String name;

    private String sex;

    private int age;

    private String address;

    Target copyFromSourse(Sourse sourse){
        BeanUtils.copyProperties(sourse, this);
        return this;
    }

    @Override
    public String toString() {
        return "Target{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }

    /**
     * Setter method for property <tt>name</tt>.
     *
     * @param name value to be assigned to property name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Setter method for property <tt>sex</tt>.
     *
     * @param sex value to be assigned to property sex
     */
    public void setSex(String sex) {
        this.sex = sex;
    }

    /**
     * Setter method for property <tt>age</tt>.
     *
     * @param age value to be assigned to property age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * Setter method for property <tt>address</tt>.
     *
     * @param address value to be assigned to property address
     */
    public void setAddress(String address) {
        this.address = address;
    }
}

测试类:

package com.yoyo.yotils.yoyo.copyProperties;

/**
 *
 * @author yoyo
 * @version $Id: Test.java, v 0.1 2019年06月17日 12:15 yoyo Exp $
 */
public class Test {
    public static void main(String[] args) {
        Sourse sourse = new Sourse("tom", "male", 13);
        Target target = new Target().copyFromSourse(sourse);
        System.out.println(target.toString());
    }
}

结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值