java 两个对象之间进行数据合并

  1. 实体类
package com.zp.demo.test;

public class Student {
    private String name;
    private int age;
    private String sex;

    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 getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

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

    public Student() {
    }

}

  2.合并方法

/*
以第一个实体类为主,如果第一个的实体类某个字段为空,则会吧第二个实体类的值取过来进行赋值,
如果不为空的则不作改变
*/
  
//针对所用对象
private static Object combineSydwCore(Object sourceBean, Object targetBean) { Class sourceBeanClass = sourceBean.getClass(); Class targetBeanClass = targetBean.getClass(); Field[] sourceFields = sourceBeanClass.getDeclaredFields(); Field[] targetFields = sourceBeanClass.getDeclaredFields(); for (int i = 0; i < sourceFields.length; i++) { Field sourceField = sourceFields[i]; Field targetField = targetFields[i]; sourceField.setAccessible(true); targetField.setAccessible(true); try { if (!(sourceField.get(sourceBean) == null)) { targetField.set(targetBean, sourceField.get(sourceBean)); } } catch (IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } } return targetBean; }

//针对单一对象,返回类型是对于实体类

 /*
    以第一个实体类为主,如果第一个的实体类某个字段为空,则会吧第二个实体类的值取过来进行赋值,
    如果不为空的则不作改变
     */
    private static Student combineSydwCore(Student sourceBean, Student targetBean) {
        Class sourceBeanClass = sourceBean.getClass();
        Class targetBeanClass = targetBean.getClass();

        Field[] sourceFields = sourceBeanClass.getDeclaredFields();
        Field[] targetFields = targetBeanClass.getDeclaredFields();
        for (int i = 0; i < sourceFields.length; i++) {
            Field sourceField = sourceFields[i];
            if (Modifier.isStatic(sourceField.getModifiers())) {
                continue;
            }
            Field targetField = targetFields[i];
            if (Modifier.isStatic(targetField.getModifiers())) {
                continue;
            }
            sourceField.setAccessible(true);
            targetField.setAccessible(true);
            try {
                if (!(sourceField.get(sourceBean) == null) && !"serialVersionUID".equals(sourceField.getName().toString())) {
                    targetField.set(targetBean, sourceField.get(sourceBean));
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return targetBean;
    }

 

 

3.测试

public static void main(String[] args) {
 
        Student student = new Student();
        student.setAge(1);
        student.setName("李四");
        Student student1 = new Student();
        student1.setSex("男");
        Student student2 = (Student) EntityUtil.combineSydwCore1(student,student1);//类型转换
        System.out.println(student2);

    }

 

4.截图

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值