这是一个java对象比较的方法。可以返回一句话,修改了对象的哪个字段,修改前是什么,修改后是什么
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.*;/*** 对象比较并得出比较结果的方法*/
public class ObjectCompareUtils {private static Logger logger = LoggerFactory.getLogger(ObjectCompareUtils.class);private static Set<String> ignores = new TreeSet<>();static {ignores.add("serialVersionUID");ignores.add("id");ignores.add("creator");ignores.add("createTime");ignores.add("modifier");ignores.add("modifyTime");}static class MyAttr {String attrName;String attrValue;String attrDesc;}private static Map<String, MyAttr> parseObj(Object obj) {Map<String, MyAttr> rsMap = new HashMap<>();//得到classClass cls = obj.getClass();//得到所有属性Field[] fields = cls.getDeclaredFields();for (int i=0;i<fields.length;i++){try {//得到属性Field field = fields[i];//打开私有访问field.setAccessible(true);//获取属性String name = field.getName();if (ignores.contains(name)) {continue;}logger.info("字段 -- {}", name);//获取属性值Object value = field.get(obj);Annotation[] an = field.getDeclaredAnnotations();ApiModelProperty myFieldAnnotation = field.getAnnotation(ApiModelProperty.class);MyAttr myAttr = new MyAttr();myAttr.attrName = name;myAttr.attrValue = (value == null ? "" : value.toString());if (myFieldAnnotation != null) {myAttr.attrDesc = myFieldAnnotation.value();}rsMap.put(name, myAttr);} catch (IllegalAccessException e) {e.printStackTrace();}}return rsMap;}/*** 比较2个对象,需要对象的所属类标记了swagger的属性注解《ApiModelProperty》* @param newObj 新对象* @param oldObj 旧对象* @param style 比较结果的样式* @return*/public static String compareObj(Object newObj, Object oldObj, RESULT_STYLE style) {Map<String, MyAttr> newAttrMap = parseObj(newObj);Map<String, MyAttr> oldAttrMap = parseObj(oldObj);Iterator<Map.Entry<String, MyAttr>> newIt = newAttrMap.entrySet().iterator();String oldValue = "";String newValue = "";StringBuilder sb = new StringBuilder();while (newIt.hasNext()) {Map.Entry<String, MyAttr> entry = newIt.next();newValue = entry.getValue().attrValue;oldValue = oldAttrMap.get(entry.getKey()).attrValue;if (!newValue.equals(oldValue)) {sb.append(renderResutl(entry.getValue().attrDesc, oldValue, newValue, style));}}return sb.toString();}/*** 返回结果渲染* @param desc* @param oldValue* @param newValue* @param style* @return*/private static String renderResutl(String desc, String oldValue, String newValue, RESULT_STYLE style) {if (style == RESULT_STYLE.NONE) {return "将 " + desc + " 从 " + oldValue+ " 修改为 " + newValue;} else if (style == RESULT_STYLE.RED_BLUE){return "将 <font color=\"#50A6FF\">" + desc + "</font> 从 <font color=\"#FF9900\">" + oldValue+ "</font> 修改为 <font color=\"#FF9900\">" + newValue + "</font><br/>";} else {return "将 \"" + desc + "\" 从 \"" + oldValue+ "\" 修改为 \">" + newValue + "\"<br/>";}}/*** 比对结果样式枚举*/public enum RESULT_STYLE {/*** 无样式*/NONE(0,"无样式"),/*** 红蓝色样式*/RED_BLUE(1,"红蓝色样式"),/*** 双引号样式*/DOUBLE_QUOTES(2,"双引号样式");RESULT_STYLE(Integer key, String value) {this.key = key;this.value = value;}private Integer key;private String value;public Integer getKey() {return key;}public void setKey(Integer key) {this.key = key;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}}public static void main(String[] args) {CompanyUpdateVo update = new CompanyUpdateVo();update.setCompanyName("新公司");update.setCompanyCode("10002");CompanyEntity old = new CompanyEntity();old.setCompanyName("旧公司");String text = ObjectCompareUtils.compareObj(update, old, RESULT_STYLE.RED_BLUE);System.out.println(text);}
}