SpringBoot对比字段更新

设置自定义接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 设置字段展示顺序
* 设置字段展示名称
* @author sup
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FieldDisplay {

/**设置字段展示顺序*/
public String order() default "";

/**设置字段展示名称*/
public String name() default "";
}

变更内容实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
**
* 变更内容实体类
* @author uap
* @date 2021-09-15
*/
public class FieldDisplayResult {

/** 成员变量值 */
private String value;

/** 成员变量名称 */
private String name;

/** 成员量展示名称 */
private String displayName;

/** 变更flag(0:,1:) */
private String compareFlag;

/** 变更前内容 */
private String beforeValue;

/** 变更后内容 */
private String afterValue;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDisplayName() {
return displayName;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public String getCompareFlag() {
return compareFlag;
}

public void setCompareFlag(String compareFlag) {
this.compareFlag = compareFlag;
}

public String getBeforeValue() {
return beforeValue;
}

public void setBeforeValue(String beforeValue) {
this.beforeValue = beforeValue;
}

public String getAfterValue() {
return afterValue;
}

public void setAfterValue(String afterValue) {
this.afterValue = afterValue;
}

@Override
public String toString() {
return "FieldDisplayResult{" +
"value='" + value + '\'' +
", name='" + name + '\'' +
", displayName='" + displayName + '\'' +
", compareFlag='" + compareFlag + '\'' +
", beforeValue='" + beforeValue + '\'' +
", afterValue='" + afterValue + '\'' +
'}';
}
}

实体类比较工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* 实体类比较工具类
* @author uap
* @date 2021-09-15
*/
public class FieldDisplayUtils {

/**
* 对两个相同实体类类字段变更内容
*
* @param o 变更前
* @param n 变更后
*/
public static List<FieldDisplayResult> compareAndReturnDifferent(Object o, Object n) {
Map<Integer, FieldDisplayResult> oMap = FieldDisplayUtils.convertObj2SortMap(o);
Map<Integer, FieldDisplayResult> nMap = FieldDisplayUtils.convertObj2SortMap(n);
return compareSortMap(oMap, nMap);
}

/**
* 根据注解获取其字段信息,并按照设置的展示顺序排序
*
* @param obj 实体类对象
*/
public static Map<Integer, FieldDisplayResult> convertObj2SortMap(Object obj) {
//获取所有成员变量
Class<?> clazz = obj.getClass();
Field[] fields = clazz.getDeclaredFields();
TreeMap<Integer, FieldDisplayResult> sortMap = new TreeMap<>();

//遍历成员变量,记录含有@FieldDisplay注解的字段,以及注解上标注的内容
for (Field field : fields) {
//忽略序列化字段
if ("serialVersionUID".equals(field.getName())) {
continue;
}
//设置可读
field.setAccessible(true);

//获取注解,跳过未加注解字段
FieldDisplay annotation = field.getAnnotation(FieldDisplay.class);
if (annotation == null) {
continue;
}

//获取字段名
String displayName = annotation.name();
if (StringUtils.isBlank(annotation.name())) {
displayName = "";
}

Object value;
try {
//取得成员变量get方法,获取其内容
value = new PropertyDescriptor(field.getName(), clazz).getReadMethod().invoke(obj);
} catch (Exception e) {
e.printStackTrace();
continue;
}
if (value == null) {
value = "";
} else if (value instanceof Date) {
String dateFormat = "yyyy-MM-dd HH:mm:ss";

JsonFormat annotationDateJ = field.getAnnotation(JsonFormat.class);
if (null != annotationDateJ) {
dateFormat = annotationDateJ.pattern();
}
Excel annotationDate = field.getAnnotation(Excel.class);
if (null != annotationDate) {
dateFormat = annotationDate.dateFormat();
}
value = DateFormatUtils.format((Date) value, dateFormat);
} else if (value instanceof BigDecimal) {
value = new BigDecimal(value.toString()).toPlainString();
}

FieldDisplayResult result = new FieldDisplayResult();
result.setName(field.getName());
result.setValue(value.toString());
result.setDisplayName(displayName);
sortMap.put(Integer.parseInt(annotation.order()), result);
}
return sortMap;
}

/**
* 对比字段,判断是否产生变更
*
* @param beforeMap 变更前字段Map
* @param afterMap 变更后字段Map
*/
public static List<FieldDisplayResult> compareSortMap(Map<Integer, FieldDisplayResult> beforeMap, Map<Integer, FieldDisplayResult> afterMap) {

List<FieldDisplayResult> resultList = new ArrayList<>();
for (Map.Entry<Integer, FieldDisplayResult> beforeEntry : beforeMap.entrySet()) {
//获取变更前后对象
FieldDisplayResult r = new FieldDisplayResult();
FieldDisplayResult before = beforeEntry.getValue();
FieldDisplayResult after = afterMap.get(beforeEntry.getKey());

//检查内容是否修改
if (StrUtil.equals(before.getValue(), after.getValue())) {
//未修改
r.setCompareFlag("0");
} else {
//修改
r.setCompareFlag("1");
}

//成员变量名称
r.setName(before.getName());
//成员量展示名称
r.setDisplayName(before.getDisplayName());
//变更前内容
r.setBeforeValue(before.getValue());
//变更后内容
r.setAfterValue(after.getValue());
resultList.add(r);
}
return resultList;
}
}

使用示例

1
List<FieldDisplayResult> baseChangeList = FieldDisplayUtils.compareAndReturnDifferent(oldBase, newBase);