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();
示例
public static void main(String[] args) {
Javers build = JaversBuilder.javers().build();
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说明有改变
遇到的问题:
-
BigDecimal 在比较BigDecimal类型的不同数据时,会出现问题。由于javer.compare()底层是使用的objects的equals方法来判断两个数据是否相等,而BigDecimal类型比较时使用equals方法比较存在问题,应该使用BigDecimal类中的compareTo方法比较,详见https://blog.csdn.net/molihuaya/article/details/79139418 BigDecimalComparatorWithFixedEquals类实现了CustomValueComparator<>接口并重写equals方法 public class BigDecimalComparatorWithFixedEquals implements CustomValueComparator<BigDecimal> {
@Override
public boolean equals(BigDecimal a, BigDecimal b) {
return a.compareTo(b) == 0;
}
所以创建javers对象时: Javers javers = JaversBuilder.javers().registerValue(BigDecimal.class,new BigDecimalComparatorWithFixedEquals()).build();
-
Date 在比较Date类型的不同数据时,会出现问题。假设在数据中查出的日期类型为Date,新修改的数据为TimeStamp类型。此时使用javers.compare()比较时即使时间点相同,比较出来仍有问题。 数据库中的数据 2022-02-25 23:59:59.0
新修改的数据 Fri Feb 25 23:59:59 CST 2022
可以通过实现CustomValueComparator<>接口来重写equals方法 public class EfDateComparator implements CustomValueComparator<Date> {
@Override
public boolean equals(Date a, Date b) {
return a.getTime() == b.getTime();
}
所以创建javers对象时: Javers javers = JaversBuilder.javers().registerValue(BigDecimal.class,new BigDecimalComparatorWithFixedEquals()).build();
Javers javers = JaversBuilder.javers()
.registerValue(BigDecimal.class,new BigDecimalComparatorWithFixedEquals())
.registerValue(BigDecimal.class,new BigDecimalComparatorWithFixedEquals())
.build();
相关注解
在比较两个类时,可能需要只比较部分字段或不比较部分字段,可以考虑使用以下两个注解
@DiffInclude
@DiffIgnore
|