Home > Back-end > How will be the same entity class two all attributes compared one by one
How will be the same entity class two all attributes compared one by one
Time:05-09
Have two instance variables, storing information is modified before and after modification, For example, LibraryDTO old=new LibraryDTO (); LibraryDTO new=new LibraryDTO ();
I now need to look at all these two entity class attribute's value to compare, and will be listed there, divided into two fields into a table, divided into content after the modification of content and modify before How do I achieve?
CodePudding user response:
Handwritten words cycle judgment is a little hard coded Or each object obtained by reflecting all of the fields to compare
CodePudding user response:
Public static Map CompareTom (Tom before and after) Tom throws the Exception { List FieldList=new ArrayList<> (a); FieldList. AddAll (arrays.aslist (before getClass () getDeclaredFields ())); The Object old; The Object now; For (Field Field: fieldList) { field.setAccessible(true); Old=field. The get (before); Now=field. The get (after); If (JSON. ToJSONString (old) equals (JSON. ToJSONString (now))) { Field. The set (before, null); Field. The set (after, null); } } Map CompareResult=new HashMap<> (a); CompareResult. Put (" beforeModify ", JSON. ToJSONString (before)); CompareResult. Put (" afterModify ", JSON. ToJSONString (after)); Return compareResult; }