1.在我们需要验证我们插入的内容是否插入成功,需要判断字段是否成功
public static boolean compareObjects(Object obj1, Object obj2) {
if (obj1 == obj2) {
return true;
}
if (obj1 == null || obj2 == null || obj1.getClass() != obj2.getClass()) {
return false;
}
Class<?> clazz = obj1.getClass();
Field[] fields = clazz.getDeclaredFields();
try {
for (Field field : fields) {
field.setAccessible(true);
Object value1 = field.get(obj1);
Object value2 = field.get(obj2);
if (value1 == null || value2 == null) {
continue;
}
if (!value1.equals(value2)) {
return false;
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
return false;
}
return true;
}