学习记录

java 对比两个实体类字段变化(学习记录)

2021-06-20  本文已影响0人  zt_sole

对比实现

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 实体对比
 */
public class ComparedEntityUtil {
    public static void main(String[] args) {
        UserEntity u1=new UserEntity();
        u1.setName("李师1");
        u1.setAge(0);

        UserEntity u2=new UserEntity();
        u2.setName("李师");
        u2.setAge(0);

        System.out.println( compareTwoObject(u1,u2).toString());
    }

    /**
     *  对比两个对象修改值 (字段只支持字符串、数值)
     * @param oldObject
     * @param newObject
     * @return
     */
    public static Object compareTwoObject(Object oldObject,Object newObject) {
        List<ComparedResult> ret=new ArrayList<>();
        Class clazz =oldObject.getClass();

        try {
            // 获取所有字段
            Field[] Fields = clazz.getDeclaredFields();
            for (Field field : Fields) {
                String fieldName=field.getName();// 字段名
                FieldAnon fieldAnon =field.getAnnotation(FieldAnon.class);
                String filedAnoName="";
                if(null!= fieldAnon){
                    filedAnoName= fieldAnon.name(); // 字段中文名称
                }


                PropertyDescriptor pd=new PropertyDescriptor(field.getName(),clazz);
                Method getMethod=pd.getReadMethod();

                // 在oldObject上调用get方法等同于获得oldObject的属性值
                Object oldValue = getMethod.invoke(oldObject);
                // 在newObject上调用get方法等同于获得newObject的属性值
                Object newValue = getMethod.invoke(newObject);

                // 对比两个值是否一致
                if(!oldValue.equals(newValue)){
                    ComparedResult result=new ComparedResult();
                    result.setField(fieldName);
                    result.setFieldName(filedAnoName);
                    result.setOldValue(oldValue);
                    result.setNewValue(newValue);
                    result.setRemark(oldValue+"->"+newValue);
                    ret.add(result);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return ret;
    }
}
image.png

UserEntity

import java.io.Serializable;

public class UserEntity implements Serializable {
    @FieldAnon(name="姓名",filed="name")
    private String name;
    @FieldAnon(name="年龄",filed="age")
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

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

ComparedResult

import java.io.Serializable;

/**
 * 对比结果
 */
public class ComparedResult implements Serializable {
    /**
     * 对比字段
     */
    private String field;
    /**
     * 对比字段名称(说明)
     */
    private String fieldName;
    /**
     * 原来的值
     */
    private Object oldValue;
    /**
     * 修改后的值
     */
    private Object newValue;
    /**
     * 备注
     */
    private String remark;

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public Object getOldValue() {
        return oldValue;
    }

    public void setOldValue(Object oldValue) {
        this.oldValue = oldValue;
    }

    public Object getNewValue() {
        return newValue;
    }

    public void setNewValue(Object newValue) {
        this.newValue = newValue;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @Override
    public String toString() {
        return "{" +
                "field='" + field + '\'' +
                ", fieldName='" + fieldName + '\'' +
                ", oldValue=" + oldValue +
                ", newValue=" + newValue +
                ", remark='" + remark + '\'' +
                '}';
    }
}

FieldAnon

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldAnon {
    String filed();
    String name() default "";
}
上一篇下一篇

猜你喜欢

热点阅读