Java字段更新日志,记录每个字段更新细节

原文:https://blog.csdn.net/printsky/article/details/82226729原文链接

/**
 * 比较新老对象的差别
 *
 * @author tuxuchen
 * @date 2022/1/10 16:20
 */
public class ClassBase {

  public final static String ADD = "新增";

  public final static String UPDATE = "更新";

  public final static String DELETE = "删除";

  public static void main(String[] args) {
    CompTest oldObj = new CompTest(1, "张三", 12);
    CompTest newObj = new CompTest(1, "李四", 14);
    try {
      String message = ClassBase.comparatorObject("更新", oldObj, newObj);
      System.out.println(message);
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
  }

  /**
   * 比较新老对象的差别
   * @param unityOperate 类型
   * @param oldObj 旧对象
   * @param newObj 新对象
   * @return matter 哪个字段有更新 content 内容更新成什么
   */
  public static String comparatorObject(String unityOperate, Object oldObj, Object newObj) throws IllegalAccessException {
    StringBuilder matter = new StringBuilder();
    StringBuilder content = new StringBuilder();
    if (oldObj != null && UPDATE.equals(unityOperate)) {
      Map<String, Object> oldMap = changeValueToMap(oldObj);
      Map<String, Object> newMap = changeValueToMap(newObj);
      if (oldMap != null && !oldMap.isEmpty()) {
        for (Map.Entry<String, Object> entry : oldMap.entrySet()) {
          Object oldValue = entry.getValue();
          Object newValue = newMap.get(entry.getKey());
          if (!oldValue.equals(newValue)) {
            matter.append("[").append(entry.getKey()).append("]");
            content.append("[").append(oldValue).append("->").append(newValue).append("]");
          }
        }
      }
    } else {
      matter.append("-");
      content.append("-");
    }
    return matter.toString() + ">>>>>>" +content.toString();
  }

  /**
   * 将类对象转换成Map
   * @param entity 原对象
   * @throws IllegalAccessException 类型转换时报错
   * @return
   */
  private static Map changeValueToMap(Object entity) throws IllegalAccessException {
    Map resultMap = new HashMap<>();
    Field[] fields = entity.getClass().getDeclaredFields();
    for (Field field : fields) {
      field.setAccessible(true);
      String name = field.getName();
      resultMap.put(name, field.get(entity));
    }
    return resultMap;
  }

  /**
   * 测试对象
   */
  public static class CompTest {
    private int id;
    private String name;
    private int age;

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

    public int getId() {
      return id;
    }

    public String getName() {
      return name;
    }

    public int getAge() {
      return age;
    }

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

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

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值