两个实体类对比字段的前后修改差异
目的
有时候修改表单需要看到前后修改差异,直观看到修改的地方
实体类
@ApiModel(value = "学生实体类")
@Data
public class SupplierInfo {
@TableId(type = IdType.AUTO)
private String id;
@ApiModelProperty(value = "学生名称", required = true)
@NotBlank(message = "学生名称不能为空")
private String name;
@ApiModelProperty(value = "学生QQ", required = false)
@TableField(exist = false)
private Integer QQ;
}
对比工具类
import io.swagger.annotations.ApiModelProperty;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Summer9977 on 2023-06-16
*/
public class ClassCompareUtil {
/**
* 比较两个实体属性值,返回一个boolean,true则表时两个对象中的属性值无差异
*
* @param oldObject 进行属性比较的对象1
* @param newObject 进行属性比较的对象2
* @return 属性差异比较结果boolean
*/
public static boolean compareObject(Object oldObject, Object newObject) {
Map<String, Map<String, Object>> resultMap = compareFields(oldObject, newObject);
if (resultMap.size() > 0) {
return true;
} else {
return false;
}
}
/**
* 比较两个实体属性值,返回一个map以有差异的属性名为key,value为一个Map分别存oldObject,newObject此属性名的值
*
* @param oldObject 进行属性比较的对象1
* @param newObject 进行属性比较的对象2
* @return 属性差异比较结果map
*/
@SuppressWarnings("rawtypes")
public static Map<String, Map<String, Object>> compareFields(Object oldObject, Object newObject) {
Map<String, Map<String, Object>> map = null;
try {
/**
* 只有两个对象都是同一类型的才有可比性
*/
if (oldObject.getClass() == newObject.getClass()) {
map = new HashMap<String, Map<String, Object>>();
Class clazz = oldObject.getClass();
Map<String,String> descsMap = getFieldSwaggerValue(clazz);
//获取object的所有属性
PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz, Object.class).getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
//遍历获取属性名
String name = pd.getName();
//获取字段名称
String label = descsMap.get(name);
//获取属性的get方法
Method readMethod = pd.getReadMethod();
// 在oldObject上调用get方法等同于获得oldObject的属性值
Object oldValue = readMethod.invoke(oldObject);
// 在newObject上调用get方法等同于获得newObject的属性值
Object newValue = readMethod.invoke(newObject);
if (oldValue instanceof List) {
continue;
}
if (newValue instanceof List) {
continue;
}
if (oldValue instanceof Timestamp) {
oldValue = new Date(((Timestamp) oldValue).getTime());
}
if (newValue instanceof Timestamp) {
newValue = new Date(((Timestamp) newValue).getTime());
}
if (oldValue == null && newValue == null) {
continue;
} else if (oldValue == null && newValue != null) {
Map<String, Object> valueMap = new HashMap<String, Object>();
valueMap.put("oldValue", oldValue);
valueMap.put("newValue", newValue);
valueMap.put("label", label);
map.put(name, valueMap);
continue;
}
if (!oldValue.equals(newValue)) {// 比较这两个值是否相等,不等就可以放入map了
Map<String, Object> valueMap = new HashMap<String, Object>();
valueMap.put("oldValue", oldValue);
valueMap.put("newValue", newValue);
valueMap.put("label", label);
map.put(name, valueMap);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* 获取实体类字段的注解
*
* @param clazz
* @return
*/
private static Map<String, String> getFieldSwaggerValue(Class clazz) {
Field[] fields = clazz.getDeclaredFields();
Map<String, String> map = new HashMap<>();
for (Field f : fields) {
//ApiModelProperty 为需要拿到的实体类注解
boolean annotationPresent2 = f.isAnnotationPresent(ApiModelProperty.class);
if (annotationPresent2) {
ApiModelProperty name = f.getAnnotation(ApiModelProperty.class);
String nameStr = name.value();
map.put(f.getName(), nameStr);
}
}
return map;
}
}
测试类
if (ClassCompareUtil.compareObject(oldSupplierInfo,supplierInfo)) {
UserRoleDto userInfo = TokenUtils.getLoginUserInfo();
Map<String, Object> params = new HashMap<>();
params.put("supplierName", supplierInfo.getName());
Map<String, Map<String, Object>> map = ClassCompareUtil.compareFields(oldSupplierInfo,supplierInfo);
map.keySet().forEach(s -> {
params.put(map.get(s).get("label").toString(), "[" +map.get(s).get("oldValue")+ "]" + "变更为[" + map.get(s).get("newValue")+ "]");
});
}