// User object entity class @Data @EqualsAndHashCode(callSuper = false) @ApiModel(value="user object", description="user table") public class User { @ApiModelProperty(value = "user id") private int id; @ApiModelProperty(value = "name") private String name; @ApiModelProperty(value = "age") private int age; @ApiModelProperty(value = "Address") private String address; }
// User modify log class
@Data @EqualsAndHashCode(callSuper = false) @ApiModel(value = "userUpdateLog object", description = "user update log table") public class UserUpdateLog { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "主键ID") private Long userLogId; @ApiModelProperty(value = "Modify Type") private String modifyField; @ApiModelProperty(value = "Content before modification") private String beforeModification; @ApiModelProperty(value = "modified content") private String afterModification; }
// Formal code test class @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @RunWith(SpringRunner.class) public class demo1 { /** * JDK introspection class library: * PropertyDescriptor class: (Property Descriptor) * The PropertyDescriptor class means that the JavaBean class exports a property through memory. Main method: * 1. getPropertyType(), get the Class object of the property; * 2. getReadMethod(), get the method used to read the attribute value; * 3. getWriteMethod(), get the method used to write the attribute value; * 4. hashCode(), get the hash value of the object; * 5. setReadMethod(Method readMethod), set the method used to read the attribute value; * 6. setWriteMethod(Method writeMethod), set the method used to write the attribute value */ @Test public void test1(){ User beforeUser=new User(); beforeUser.setId(1); beforeUser.setName("小明"); beforeUser.setAge(20); beforeUser.setAddress("北京"); User afterUser=new User(); afterUser.setId(2); afterUser.setName("小黑"); afterUser.setAge(30); afterUser.setAddress("上海"); List<UserUpdateLog> userUpdateLogs = contrastObj(afterUser, beforeUser); System.out.println(userUpdateLogs); } public static List<UserUpdateLog> contrastObj(Object afterObj, Object beforeObj) { User afterUser = (User) afterObj; User beforeUser = (User) beforeObj; List<UserUpdateLog> updateLogList = Lists.newArrayList(); try { Class clazz = afterUser.getClass(); Field[] fields = afterUser.getClass().getDeclaredFields(); for (Field field : fields) { UserUpdateLog userUpdateLog = new UserUpdateLog(); if("serialVersionUID".equals(field.getName())){ continue; } //Get the property descriptor of propertyName in the clazz type PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz); // Get the method to read the attribute value getReadMethod() Method getMethod = pd.getReadMethod(); Object o1 = getMethod.invoke(afterUser); Object o2 = getMethod.invoke(beforeUser); String s1 = o1 == null? "": o1.toString();//Avoid null pointer exception String s2 = o2 == null? "": o2.toString();//Avoid null pointer exception //Think about the line commented below: there will be bugs, although the program is try catch, the program did not report an error, but the result is not what we want // todo my understanding o1 o2 is object type //if (!o1.toString().equals(o2.toString())) { if (!s1.equals(s2)) { userUpdateLog.setModifyField(field.getAnnotation(ApiModelProperty.class).value()); userUpdateLog.setBeforeModification(s2); userUpdateLog.setAfterModification(s1); updateLogList.add(userUpdateLog); } } } catch (Exception e) { System.out.println(e.getMessage()); } return updateLogList; }
// console output result
[UserUpdateLog(userLogId=null, modifyField=用户id, beforeModification=1, afterModification=2), UserUpdateLog(userLogId=null, modifyField=姓名, beforeModification=小明, afterModification=小黑), UserUpdateLog(userLogId=null, modifyField=年龄, beforeModification=20, afterModification=30), UserUpdateLog(userLogId=null, modifyField=地址, beforeModification=北京, afterModification=上海)]