通过注解和反射,比较两个对象,需要校验某些字段是否相同,如果不同,中文字段名称和属性值对比结果(直接上代码)


实际开发中,存在比较两个对象,需要校验某些字段是否相同,如果不同,需返回前端页面名称和属性值
比较笨的办法就是,对两个对象需要判断的属性一一进行对比,如果只有几个属性值需要对比,可以满足,但是字段值过多就不使用了,也没有通用性

下面介绍利用反射机制,判断两个对象属性值是否相同


package
com.fehorizon.hxtms.order.vo.demandOrder.request; import com.fehorizon.hxtms.order.config.GetParamName; public class Student { @GetParamName(value = "姓名") private String name; @GetParamName(value = "性别") private String gender; @GetParamName(value = "手机号") private String phone; @GetParamName(value = "地址") private String addr; public Student(String name, String gender, String phone, String addr) { this.name = name; this.gender = gender; this.phone = phone; this.addr = addr; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } @Override public String toString() { return "Student"; } }
public static void main(String[] args) throws IllegalAccessException {
    Student student = new Student("小明", "男", "15011112222", "重庆");
    User user = new User("小", "admin", "15811112222", "北京");

    StringBuffer stringBuffer = compareTwoObject(student, user);
    System.out.println(stringBuffer);
    // 运行结果 {compare_object: =Student vs User, Student_field:phone=15011112222, User_field:phone=15811112222}
}

public static StringBuffer compareTwoObject(Object obj1, Object obj2) throws IllegalAccessException {
    StringBuffer stringBuffer = new StringBuffer();
    Class<?> clazz1 = obj1.getClass();
    Class<?> clazz2 = obj2.getClass();
    Field[] fields1 = clazz1.getDeclaredFields();
    Field[] fields2 = clazz2.getDeclaredFields();
    for (Field field1 : fields1) {
        for (Field field2 : fields2) {
            if (!field1.isAnnotationPresent(GetParamName.class)) {
                continue;
            }
            if (field1.getName().equals(field2.getName())) {
                field1.setAccessible(true);
                field2.setAccessible(true);
                if (!compareTwoObject1(field1.get(obj1), field2.get(obj2))) {
                    GetParamName annotation = field1.getAnnotation(GetParamName.class);
                    if (annotation != null) {
                        String s = ("").equals(annotation.value()) ? null : annotation.value();
                        stringBuffer.append("『"+s+"』字段:<"+field1.get(obj1).toString()+">修改为<"+field2.get(obj2).toString()+">。");
                    }
                }
            }
        }
    }
    return stringBuffer;
}
public static boolean compareTwoObject1(Object object1, Object object2) {
    if (object1 == null && object2 == null) {
        return true;
    }
    if (object1 == null && object2 != null){
        return false;
    }
    // 假如还有别的类型需要特殊判断 比如 BigDecimal, 演示,只写BigDecimal示例,其他都相似
    if (object1 instanceof BigDecimal && object2 instanceof BigDecimal) {
        if (object1 == null && object2 == null) {
            return true;
        }
        if (object1 == null ^ object2 == null) {
            return false;
        }
        return ((BigDecimal) object1).compareTo((BigDecimal) object2) == 0;
    }

    if (object1.equals(object2)) {
        return true;
    }
    return false;
}
package com.fehorizon.hxtms.order.config;

import java.lang.annotation.*;

@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
/**
 * description:
 * create time: 10:34 2020/12/2
 *
 * @return 
 */
public @interface GetParamName {
    String value();
}
想要体面生活,又觉得打拼辛苦;想要健康身体,又无法坚持运动。人最失败的,莫过于对自己不负责任,连答应自己的事都办不到,又何必抱怨这个世界都和你作对?人生的道理很简单,你想要什么,就去付出足够的努力。
原文地址:https://www.cnblogs.com/potentPrince/p/14074119.html