/**
* @param newValue 新值
* @param oldValue 旧值
* @return key 字段名 value 旧值
*/
public static Map<String, Object> compare(Object newValue, Object oldValue, List<String> filterList) {
Map<String, Object> compareMap = new HashMap<>();
try {
Field[] fields1 = newValue.getClass().getDeclaredFields();
Field[] fields2 = oldValue.getClass().getDeclaredFields();
for (int i = 0; i < fields1.length; i++) {
fields1[i].setAccessible(true);
fields2[i].setAccessible(true);
Object o = fields1[i].get(newValue);
Object o1 = fields2[i].get(oldValue);
String s = String.valueOf(o);
if (!Objects.equals(o, o1) && !filterList.contains(fields1[i].getName())
&& StringUtils.isNotEmpty(s) && Objects.nonNull(o)) {
if (Objects.isNull(o1)) {
compareMap.put(fields1[i].getName(), "");
} else {
compareMap.put(fields1[i].getName(), oldValue + "->" + newValue);
}
}
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
return compareMap;
}
比较两个对象相同对象不同值
于 2024-06-17 11:36:34 首次发布