对比两个对象之间的属性变化


简单说明

正好工作用到,就记录一下,方便以后使用,该功能主要是对比两个对象间通过注解指定属性的属性值变化。

对比方法

/**
 * 对比两个对象之间的变化
 */
public static Map<String, Object> verifyObjectDifferent(Object oldBean, Object newBean) throwsException {
    if (oldBean != null && newBean != null) {
        Map<String, Object> modifyValue = new HashMap<>();
        try {
            Class<?> clazz = oldBean.getClass();
            List<Field> fields = new ArrayList<>();
            fields.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
            // 父类属性
            fields.addAll(new ArrayList<>(Arrays.asList(clazz.getSuperclass().getDeclaredField())));
            for (Field field : fields) {
                if ("serialVersionUID".equals(field.getName())) {
                    continue;
                }
                if (field.isAnnotationPresent(VerifyAnno.class)) {
                    PropertyDescriptor pd = new PropertyDescriptor(field.getName(), oldBeangetClass());
                    Method getMethod = pd.getReadMethod();
                    Object o1 = getMethod.invoke(oldBean);
                    Object o2 = getMethod.invoke(newBean);
                    if (o1 == null || o2 == null) {
                        continue;
                    }
                    if (!o1.toString().equals(o2.toString())) {
                        VerifyAnno verifyAnno = field.getAnnotation(VerifyAnno.class);
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("old", o1);
                        jsonObject.put("new", o2);
                        modifyValue.put(verifyAnno.value(), jsonObject);
                    }
                }
            }
            return modifyValue;
        } catch (Exception e) {
            throw e;
        }
    } else {
        return null;
    }
}

标识需要对比的属性注解

/**
 * @description: 检查对象是否重复
 */
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface VerifyAnno {
    String value() default "";
}

测试对象

public class Teacher implements Serializable {

    @VerifyAnno("编号")
    private Integer id;
    @VerifyAnno("老师Id")
    private Long teacherId;
    @VerifyAnno("老师姓名")
    private String teacherName;
    @VerifyAnno("课程名")
    private String courseTitle;

    private static final long serialVersionUID = 1L;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCourseTitle() {
        return courseTitle;
    }

    public void setCourseTitle(String courseTitle) {
        this.courseTitle = courseTitle;
    }

    public Long getTeacherId() {
        return teacherId;
    }

    public void setTeacherId(Long teacherId) {
        this.teacherId = teacherId;
    }

    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

}

使用示例

public static void main(String[] args) {
    try {
        Teacher oldBean = new Teacher();
        oldBean.setId(0);
        oldBean.setCourseTitle("JVM");
        oldBean.setTeacherId(1L);
        oldBean.setTeacherName("cchenjc");
        Teacher newBean = new Teacher();
        newBean.setId(0);
        newBean.setCourseTitle("Java");
        newBean.setTeacherId(2L);
        newBean.setTeacherName("Baymax");
        Map<String, Object> map = verifyObjectDifferent(oldBean, newBean);
        if (!CollectionUtils.isEmpty(map)) {
            System.out.println(JSON.toJSON(map));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

输出结果

返回格式可以自己随便改

{
    "课程名": {
        "new": "Java", 
        "old": "JVM"
    }, 
    "老师姓名": {
        "new": "Baymax", 
        "old": "cchenjc"
    }, 
    "老师Id": {
        "new": 2, 
        "old": 1
    }
}

文章作者: Baymax
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Baymax !
评论
  目录