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.截圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM