xkbeancomparator是一个javabean对比修改并输出差异的工具。
适用场景:用户编辑提交时,需要记录修改内容,修改前后的值对比,生成操作记录;可以选择记录的字段和字段说明,自定义操作记录。
0.依赖dependency:<dependency><groupId>com.github.xkzhangsan</groupId><artifactId>xkbeancomparator</artifactId><version>0.0.1</version></dependency>1.常见用处:对修改过的对象进行对比生成修改日志;对比部分字段修改,根据字段注释输出日志。2.主要功能类和用法:主要类名称:BeanComparator.java主要方法为:publicstaticStringcompareBean(Objectsource,Objecttarget)publicstaticCompareResultgetCompareResult(Objectsource,Objecttarget)3实例xkbeancomparator-samples( https://github.com/xkzhangsan/xkbeancomparator-samples )
(1)添加pom依赖
<dependency><groupId>com.github.xkzhangsan</groupId><artifactId>xkbeancomparator</artifactId><version>0.0.1</version></dependency>(2)增加辅助日志类
UserLogimportjava.util.HashMap;importjava.util.Map;importcom.xkzhangsan.xkbeancomparator.BeanComparator;importcom.xkzhangsan.xkbeancomparator.CompareResult;publicclassUserLog{privatestaticfinalMap<String,String>propertyTranslationMap=newHashMap<>();static{propertyTranslationMap.put("name","用户名");propertyTranslationMap.put("point","积分");}publicstaticCompareResultgetCompareResult(Objectsource,Objecttarget){returnBeanComparator.getCompareResult(source,target,propertyTranslationMap);}}(3)使用
@Testpublicvoidtest1(){Useru1=newUser();u1.setId(1);u1.setName("aa");u1.setPoint(newBigDecimal("111111111111.12"));Useru2=newUser();u2.setId(1);u2.setName("aa2");u2.setPoint(newBigDecimal("111111111111.15"));CompareResultcompareResult=UserLog.getCompareResult(u1,u2);if(compareResult.isChanged()){System.out.println(compareResult.getChangeContent());}}(4)说明
上面是推荐用法,使用辅助日志类UserLog能统一维护一个javabean的注释map说明,简化调用。 其中日期格式为:"yyyy-MM-ddHH
评论