其他分享
首页 > 其他分享> > Javers 比较两个类的差异

Javers 比较两个类的差异

作者:互联网

Javers

在开发过程中遇到需求,比较数据库中的原数据与新修改要写入库中的数据。这个实体类是比较复杂的。例如有基本类型,BigDecimal类型,自定义类型,Date类型,List集合,Set集合,Map集合等。在比较复杂的类的情况下,使用Objects的equals是不适用的。所以为了解决这个问题,可以使用Javers库中的compare方法来解决该问题

引入

 <!-- https://mvnrepository.com/artifact/org.javers/javers-core -->
        <dependency>
            <groupId>org.javers</groupId>
            <artifactId>javers-core</artifactId>
            <version>6.6.2</version>
        </dependency>

方法:

Javers javers = JaversBuilder.javers().build();
Diff diff = javers.compare(oldObject,newObject)
boolean flag = diff.hasChange();
//diff会在控制台打印出两个类之间的异同,相同不打印数据,不同会展示出修改前后的数据
//diff.hasChange(); 如果两个类完全相同,返回值为flase,两个类不同,返回值为true

示例

public static void main(String[] args) {
        Javers build = JaversBuilder.javers().build();
	    //A系统的类
    	//数据库中的数据
        PromotionDetail promotionDetail = new PromotionDetail();
        promotionDetail.setCanEditFlag(true);
        promotionDetail.setAllGoodsCountType(1);
        promotionDetail.setName("name1");
        PromotionGoods promotionGoods = new PromotionGoods();
        promotionGoods.setCount(1);
        promotionGoods.setDeptName("deptname1");
        PromotionGoods promotionGoods1 = new PromotionGoods();
        promotionGoods1.setCount(2);
        promotionGoods1.setDeptName("deptname2");
        List<PromotionGoods> list = new ArrayList<>();
        list.add(promotionGoods);
        list.add(promotionGoods1);
        promotionDetail.setGoods(list);

		//新数据
        PromotionDetail promotionDetail1 = new PromotionDetail();
        promotionDetail1.setCanEditFlag(true);
        promotionDetail1.setAllGoodsCountType(1);
        promotionDetail1.setName("name2");
        PromotionGoods promotionGoods2 = new PromotionGoods();
        promotionGoods2.setCount(5);
        promotionGoods2.setDeptName("deptname3");
        PromotionGoods promotionGoods3 = new PromotionGoods();
        promotionGoods3.setCount(4);
        promotionGoods3.setDeptName("deptname3");
        List<PromotionGoods> list0 = new ArrayList<>();
        list0.add(promotionGoods2);
        list0.add(promotionGoods3);
        promotionDetail1.setGoods(list0);



        Diff compare = build.compare(promotionDetail, promotionDetail1);
        boolean b = compare.hasChanges();
        System.out.println(compare);
        System.out.println(b);
    }
//控制台输出
Diff:
* changes on com.ef.admin.data.controller.promotion.np.param.PromotionDetail/ :
  - 'goods/0.count' changed: '1' -> '5'
  - 'goods/0.deptName' changed: 'deptname1' -> 'deptname3'
  - 'goods/1.count' changed: '2' -> '4'
  - 'goods/1.deptName' changed: 'deptname2' -> 'deptname3'
  - 'name' changed: 'name1' -> 'name2'
      
true   ====》为true说明有改变

遇到的问题:

Javers javers = JaversBuilder.javers()
    .registerValue(BigDecimal.class,new BigDecimalComparatorWithFixedEquals())
    .registerValue(BigDecimal.class,new BigDecimalComparatorWithFixedEquals())
    .build();

相关注解

在比较两个类时,可能需要只比较部分字段或不比较部分字段,可以考虑使用以下两个注解

//在一个类中,在字段上添加该注解,该类中只比较有注解的字段
@DiffInclude

//在一个类中,在字段上添加该注解,该类中忽略比较有注解的字段
@DiffIgnore

//PS:两个注解不可以同时出现在一个类中

如果有没说明白的地方可以留言

标签:PromotionGoods,compare,BigDecimal,差异,javers,new,Javers,比较
来源: https://www.cnblogs.com/Monkey9527/p/15932558.html