xkbeancomparator 0.0.2 版本发布

栏目: 软件资讯 · 发布时间: 5年前

内容简介:xkbeancomparator 是一个 java bean 对比修改并输出差异的工具。 适用场景:用户编辑提交时,需要记录修改内容,修改前后的值对比,生成操作记录;可以选择记录的字段和字段说明,自定义操作记录。 0.0.2 版本: ...

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

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

0.0.2 版本:

  • 支持基本类型的对比,比如 boolean 类型时,get 方法会以 is 开头进行了特殊处理。

软件说明

0.依赖:

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

1.常见用处:

(1)对修改过的对象进行对比生成修改日志;
(2)对比部分字段修改,根据字段注释输出日志。

2.主要功能类和用法:

主要类名称 Class:BeanComparator.java
主要方法为 Method :

public static String compareBean(Object source, Object target)
public static CompareResult getCompareResult(Object source, Object target)

3 实例 xkbeancomparator-samples ( https://github.com/xkzhangsan/xkbeancomparator-samples )

(1)添加pom依赖

<dependency>  
  <groupId>com.github.xkzhangsan</groupId>    
  <artifactId>xkbeancomparator</artifactId>       
  <version>0.0.2</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)说明

上面是推荐用法,使用辅助日志类能统一维护一个 java bean 的注释 map,简化调用。

欢迎提建议!


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Everything Store

The Everything Store

Brad Stone / Little, Brown and Company / 2013-10-22 / USD 28.00

The definitive story of Amazon.com, one of the most successful companies in the world, and of its driven, brilliant founder, Jeff Bezos. Amazon.com started off delivering books through the mail. Bu......一起来看看 《The Everything Store》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具