Java對比兩個實體類字段的值差異
記錄這個方法的原因:
需求要求記錄一條資料的變更日志,但是由于資料的字段過多,而且我也隻想到了通過實體類的對比來判斷,是以我在網上查了一下類的比較的方法
網上找到的方法
@SuppressWarnings("rawtypes")
public static Map> compareFields(Object obj1, Object obj2, List ignoreList) {
try {
Map> map = new HashMap>();
// 隻有兩個對象都是同一類型的才有可比性
if (obj1.getClass() == obj2.getClass()) {
Class claz = obj1.getClass();
// 擷取object的屬性描述
PropertyDescriptor[] pds = Introspector.getBeanInfo(claz,
Object.class).getPropertyDescriptors();
// 這裡就是所有的屬性了
for (PropertyDescriptor pd : pds) {
// 屬性名
String name = pd.getName();
// 如果目前屬性選擇忽略比較,跳到下一次循環
if (ignoreList != null && ignoreList.contains(name)) {
continue;
}
// get方法
Method readMethod = pd.getReadMethod();
// 在obj1上調用get方法等同于獲得obj1的屬性值
Object o1 = readMethod.invoke(obj1);
// 在obj2上調用get方法等同于獲得obj2的屬性值
Object o2 = readMethod.invoke(obj2);
if (o1 instanceof Timestamp) {
o1 = new Date(((Timestamp) o1).getTime());
}
if (o2 instanceof Timestamp) {
o2 = new Date(((Timestamp) o2).getTime());
}
if (o1 == null && o2 == null) {
continue;
} else if (o1 == null && o2 != null) {
List list = new ArrayList();
list.add(o1);
list.add(o2);
map.put(name, list);
continue;
}
// 比較這兩個值是否相等,不等就可以放入map了
if (!o1.equals(o2)) {
List list = new ArrayList();
list.add(o1);
list.add(o2);
map.put(name, list);
}
}
}
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
使用的一下确實可以,但是實體類中有些資料不需要對比,那我就需要利用傳參,傳一個好長的List,太繁瑣了, 是以我想通過注解去識别忽略的屬性
忽略屬性的注解
import java.lang.annotation.*;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface IgnoreCompare {
boolean ignore() default true;
}
然後在擷取ignoreList 的時候隻需要在比較的前邊 擷取一下就可以了
private static List getCompareIgnoreFields(Class objectClass) {
// 擷取所有的字段
Field[] fields = objectClass.getDeclaredFields();
List map = new ArrayList<>();
for (Field f : fields) {
// 判斷字段注解是否存在
boolean annotationPresent2 = f.isAnnotationPresent(IgnoreCompare.class);
if (annotationPresent2) {
IgnoreCompare ignoreCompare = f.getAnnotation(IgnoreCompare.class);
// 擷取注解值
boolean ignore = ignoreCompare.ignore();
if (ignore) {
map.add(f.getName());
}
}
}
return map;
}
附一個完整的比較方法
@SuppressWarnings("rawtypes")
public static Map> compareFields(Object obj1, Object obj2, Class clazz) {
try {
List ignoreList = getCompareIgnoreFields(clazz);
Map> map = new HashMap>();
// 隻有兩個對象都是同一類型的才有可比性
if (obj1.getClass() == obj2.getClass()) {
Class claz = obj1.getClass();
// 擷取object的屬性描述
PropertyDescriptor[] pds = Introspector.getBeanInfo(claz,
Object.class).getPropertyDescriptors();
// 這裡就是所有的屬性了
for (PropertyDescriptor pd : pds) {
// 屬性名
String name = pd.getName();
// 如果目前屬性選擇忽略比較,跳到下一次循環
if (ignoreList != null && ignoreList.contains(name)) {
continue;
}
// get方法
Method readMethod = pd.getReadMethod();
// 在obj1上調用get方法等同于獲得obj1的屬性值
Object o1 = readMethod.invoke(obj1);
// 在obj2上調用get方法等同于獲得obj2的屬性值
Object o2 = readMethod.invoke(obj2);
if (o1 instanceof Timestamp) {
o1 = new Date(((Timestamp) o1).getTime());
}
if (o2 instanceof Timestamp) {
o2 = new Date(((Timestamp) o2).getTime());
}
if (o1 == null && o2 == null) {
continue;
} else if (o1 == null && o2 != null) {
List list = new ArrayList();
list.add(o1);
list.add(o2);
map.put(name, list);
continue;
}
// 比較這兩個值是否相等,不等就可以放入map了
if (!o1.equals(o2)) {
List list = new ArrayList();
list.add(o1);
list.add(o2);
map.put(name, list);
}
}
}
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
在最後,記錄變更值的時候需要把字段轉為對應解釋記錄起來,我就用了swaager注解來提取的map
public static Map getFieldSwaggerValue(Class clazz) {
// 擷取所有的字段
Field[] fields = clazz.getDeclaredFields();
Map map = new HashMap<>();
for (Field f : fields) {
// 判斷字段注解是否存在
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;
}