自定义注解比较两个对象差异

1、自定义注解类

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ignoreCompare {
    boolean isCompare() default true;
    String name();
}

2、新建实体类,引用注解

public class Student {

    @ignoreCompare(name = "主键",isCompare = false)
    private String id;
    @ignoreCompare(name = "名字")
    private String name;
    @ignoreCompare(name = "年龄")
    private int age;
    @ignoreCompare(name = "城市")
    private String city;

    public Student(String id, String name, int age, String city) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.city = city;
    }

    public Student() {
    }

    public String getId() {
        return id;
    }

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

    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 getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

3、 新建工具类

public static StringBuffer compareObject(Object obj1, Object obj2){
        try {
            StringBuffer buffer = new StringBuffer();
            Class clazz1 = obj1.getClass();
            Class clazz2 = obj2.getClass();
            if (clazz1 == clazz2) { //只有两个对象都是同一类型才有可比性
                // 获取所有字段
                Field[] declaredFields = clazz1.getDeclaredFields();
                for (Field field:declaredFields) {
                    // 字段上是否有自定义注解
                    if (field.isAnnotationPresent(ignoreCompare.class)) {
                        // 如果有,获取注解的值
                        ignoreCompare annotation = field.getAnnotation(ignoreCompare.class);
                        // 字段需要比较
                        if (annotation.isCompare()) {
                            // 获取字段名
                            String fieldName = field.getName();
                            // 拼接get方法名
                            fieldName = fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
                            // 根据字段名获取值
                            Method method = clazz1.getMethod("get" + fieldName, new Class[] {});
                            // 值
                            Object name1 = method.invoke(obj1);
                            Object name2 = method.invoke(obj2);
                            if (!name1.equals(name2)) {
                                buffer.append(annotation.name()+":"+name1+"->"+name2+";");
                            }
                        }

                    }
                }
            }
            return buffer;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

4、 测试

public static void main(String[] args) {
        Student s1 = new Student("1", "test1", 13214, "shenzhen");
        Student s2 = new Student("2", "test2", 123, "珠海");
        StringBuffer buffer = compareObject(s1, s2);
        System.out.println(buffer);
    }
image.png

你可能感兴趣的:(自定义注解比较两个对象差异)