通过反射机制处理,比较修改前后部分字段是否有更改
private boolean compareUserNewAndOldValue(User old_data, User new_data) {
String[] method_name = {
"getAge",
"getGrade",
"getHeight"
};
for (String method : method_name) {
try {
BigDecimal old_num = (BigDecimal) old_data.getClass().getMethod(method, new Class[]{}).invoke(old_data, new Object[]{});
BigDecimal new_num = (BigDecimal) new_data.getClass().getMethod(method, new Class[]{}).invoke(new_data, new Object[]{});
//表示有更改
if (old_num.compareTo(new_num) != 0) {
return true;
}
} catch (Exception e) {
return true;
}
}
return false;
}