java对比两个对象生成修改记录demo

        今天产品给了个需求,五六十个字段的修改表单记录下修改记录,于是有了这个demo。

先来注解

@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ContrastName {
    String value();
}
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ContrastType {
    ConType type() default ConType.DEFAULT;
}

ConType

public enum ConType {
    DEFAULT,
    ENUM,
    IS_CHANGE
}
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ContrastEnum {
    int[] keys();
    String[] vals();
}

测试的对象

public class People {
    @ContrastName("年龄")
    private Integer age;

    @ContrastName("姓名")
    private String name;

    @ContrastName("性别")
    @ContrastType(type = ConType.ENUM)
    @ContrastEnum(keys = {1,2},vals = {"男","女"})
    private Integer sex;

    @ContrastName("电话")
    @ContrastType(type = ConType.IS_CHANGE)
    private String mobile;

    public People() {
    }

    public People(Integer age, String name, Integer sex, String mobile) {
        this.age = age;
        this.name = name;
        this.sex = sex;
        this.mobile = mobile;
    }

    public Integer getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

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

主要代码

public class Utils {
    private List<String> checkList;

    public String contrast(Object item, Object entity) {
        try {
            this.checkList = new ArrayList<>();
            StringBuffer sb = new StringBuffer();

            if (!item.getClass().equals(entity.getClass()))
                return null;
            Class<?> clazz = item.getClass();
            Field[] fields = clazz.getDeclaredFields();
            Map<String, Object> itemMap = Obj2Map(item);
            Map<String, Object> entityMap = Obj2Map(entity);
            for (Field field : fields) {
                String fieldName = field.getName();
                if (itemMap.get(fieldName).equals(entityMap.get(fieldName))) {
                    continue;
                }
                ContrastName contrastName = field.getAnnotation(ContrastName.class);
                ContrastType contrastType = field.getAnnotation(ContrastType.class);
                if (contrastType != null && contrastType.type().equals(ConType.IS_CHANGE)) {
                    this.checkList.add(contrastName.value());
                    continue;
                }
                sb.append(contrastName.value()).append(":");
                if (contrastType != null && contrastType.type().equals(ConType.ENUM)) {
                    ContrastEnum anEnum = field.getAnnotation(ContrastEnum.class);
                    int[] keys = anEnum.keys();
                    String[] vals = anEnum.vals();
                    Map<Integer, String> map = new HashMap<>();
                    for (int i = 0; i < keys.length; i++) {
                        map.put(keys[i], vals[i]);
                    }
                    sb.append("【").append(map.get(itemMap.get(fieldName))).append("】")
                            .append("更变为")
                            .append("【").append(map.get(entityMap.get(fieldName))).append("】")
                            .append("\r\n");
                    continue;
                }
                sb.append("【").append(itemMap.get(fieldName)).append("】")
                        .append("更变为")
                        .append("【").append(entityMap.get(fieldName)).append("】")
                        .append("\r\n");
            }

            if (checkList.size() > 0) {
                sb.append("更变了信息:");
                for (String check : checkList) {
                    sb.append("【").append(check).append("】");
                }
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public Map<String, Object> Obj2Map(Object obj) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            map.put(field.getName(), field.get(obj));
        }
        return map;
    }
}

测试方法

public static void main(String[] args) {
        People item = new People(18, "张三", 1, "123");
        People entity = new People(18, "李四", 2, "456");
        String contrast = new Utils().contrast(item, entity);
        System.out.println("res ===> " + contrast);
    }

结果

说明

该例子只是demo,不作为最终成品。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值