• 使用xkbeancomparator对比javabean,生成操作记录


    xkbeancomparator 是一个 java bean 对比修改并输出差异的工具。github地址

    适用场景:用户编辑提交时,需要记录修改内容,修改前后的值对比,生成操作记录;可以选择记录的字段和字段说明,自定义操作记录。

    特点:

    jdk 1.7+

    不依赖第三方jar,大小非常小

    使用反射,调用get方法对比字段值。

    使用实例:xkbeancomparator-samples

    (1)添加pom依赖

    <dependency>  
      <groupId>com.github.xkzhangsan</groupId>    
      <artifactId>xkbeancomparator</artifactId>       
      <version>0.0.1</version>    
    </dependency>    
    

    (2)java bean类 User

    import java.math.BigDecimal;
    
    public class User {
        Integer id;
        String name;
        private BigDecimal point;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public BigDecimal getPoint() {
            return point;
        }
        public void setPoint(BigDecimal point) {
            this.point = point;
        }
        
    
    }

    (3)增加辅助日志类 UserLog

    import java.util.HashMap;
    import java.util.Map;

    import com.xkzhangsan.xkbeancomparator.BeanComparator;
    import com.xkzhangsan.xkbeancomparator.CompareResult;

    public class UserLog{

    private static final Map<String, String> propertyTranslationMap = new HashMap<>();
    
    static {
    	propertyTranslationMap.put("name", "用户名");
    	propertyTranslationMap.put("point", "积分");
    }
    
    public static CompareResult getCompareResult(Object source, Object target){
    	return BeanComparator.getCompareResult(source, target, propertyTranslationMap);
    }
    

    }

    (4) 使用

    @Test
    public void test1() {
    	User u1 = new User();
    	u1.setId(1);
    	u1.setName("aa");
    	u1.setPoint(new BigDecimal("111111111111.12"));
    
    	User u2 = new User();
    	u2.setId(1);
    	u2.setName("aa2");
    	u2.setPoint(new BigDecimal("111111111111.15"));
    	CompareResult compareResult = UserLog.getCompareResult(u1, u2);
    	if (compareResult.isChanged()) {
    		System.out.println(compareResult.getChangeContent());
    	}
    }
    

    (5)输出结果

    用户名:aa->aa2,积分:111111111111.12->111111111111.15,

    (6)说明 instructions

    上面是推荐用法,使用辅助日志类能统一维护一个java bean的注释map,简化调用。 The recommended usage, above, is to use secondary logging classes to uniformly maintain an annotated map of a Java bean, simplifying invocation.

    欢迎提建议 Suggestions are welcome!

    寻找撬动地球的支点(解决问题的方案),杠杆(Java等编程语言)已经有了。xkzhangsan
  • 相关阅读:
    同花顺黄金分割线及斐波那契数列分析网格(主图公式)
    走在城市计算的路上
    206. Reverse Linked List
    237. Delete Node in a Linked List
    876. Middle of the Linked List
    83. Remove Duplicates from Sorted List
    21. Merge Two Sorted List
    33. Search in Rotated Sorted Array
    852. Peak Index in a Mountain Array
    744. Find Smallest Letter Greater Than Target
  • 原文地址:https://www.cnblogs.com/xkzhangsanx/p/11762063.html
Copyright © 2020-2023  润新知