package com.rg.spd.common.utils;
import com.rg.spd.common.annotations.ForUpdate;
import com.rg.spd.common.mode.ChangeProperties;
import com.rg.spd.common.mode.Compare;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class UpdateBeanUtils {
public static <T> List<ChangeProperties> getChangedFields(T newBean, T oldBean) {
Field[] fields = newBean.getClass().getDeclaredFields();
List<ChangeProperties> changePropertiesList = new ArrayList<>();
String updTime = null;
for (Field field : fields) {
field.setAccessible(true);
if (field.getName().equals("updTime")) {
try {
Object o = field.get(newBean);
updTime = String.valueOf(o);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
if (field.isAnnotationPresent(ForUpdate.class)) {
try {
Object newValue = field.get(newBean);
Object oldValue = field.get(oldBean);
if (!Objects.equals(newValue, oldValue)) {
String fieldName = field.getAnnotation(ForUpdate.class).fieldName();
ChangeProperties changeProperties = new ChangeProperties(updTime, fieldName, newValue, oldValue);
changePropertiesList.add(changeProperties);
}
} catch (Exception e) {
System.out.println(e);
}
}
}
return changePropertiesList;
}
public static <T> List<ChangeProperties> getChangedFields(T newBean, T oldBean, List<Compare> compareList,Boolean whetherToGetTheTime) {
List<String> CompareFields = compareList.stream().map(Compare::getCompareFields).collect(Collectors.toList());
Field[] fields = newBean.getClass().getDeclaredFields();
List<ChangeProperties> changePropertiesList = new ArrayList<>();
String updTime = null;
for (Field field : fields) {
field.setAccessible(true);
if (whetherToGetTheTime) {
if (field.getName().equals("updTime")) {
try {
LocalDateTime updateTime = (LocalDateTime) field.get(newBean);
updTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(updateTime);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
if (CompareFields.contains(field.getName())) {
try {
Object newValue = field.get(newBean);
Object oldValue = field.get(oldBean);
if (!Objects.equals(newValue, oldValue)) {
String fieldName = field.getName();
for (Compare compare : compareList) {
if (compare.getCompareFields().equals(field.getName())) {
fieldName = compare.getCompareFieldsN();
}
}
if(field.getType().isEnum()){
newValue = getEnumValue(field, newBean);
oldValue = getEnumValue(field, oldBean);
}
ChangeProperties changeProperties = new ChangeProperties(updTime, fieldName, newValue, oldValue);
changePropertiesList.add(changeProperties);
}
} catch (Exception e) {
System.out.println(e);
}
}
}
return changePropertiesList;
}
public static String getEnumValue(Field field, Object object) throws Exception {
Class<Enum> type = (Class<Enum>) field.getType();
Enum[] enumConstants = type.getEnumConstants();
Method getValue = type.getMethod("getValue");
Method getValueCh = type.getMethod("getValueCh");
for (Enum v : enumConstants) {
String theActualEnumValueInTheClass = String.valueOf(field.get(object));
String value = String.valueOf(getValue.invoke(v));
if (value.equals(theActualEnumValueInTheClass)) {
String valueCh = String.valueOf(getValueCh.invoke(v));
return valueCh;
}
}
return null;
}
}
第一个是注解方法 自定义注解(注解放在类中的属性上即可)
package com.rg.spd.common.annotations;
import java.lang.annotation.*;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ForUpdate {
String fieldName() default "";
}
第二种方法 把字段一个个放到数据库中 需要时查询出来,其中有分组概念, 可以把不同类中的字段分开,可以手动在数据库中开启是否比较
数据表结构
/*
Navicat MySQL Data Transfer
Source Server : 221
Source Server Type : MySQL
Source Server Version : 50727
Source Host : 192.168.2.221:3306
Source Schema : spd
Target Server Type : MySQL
Target Server Version : 50727
File Encoding : 65001
Date: 10/03/2022 17:15:51
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_compare
-- ----------------------------
DROP TABLE IF EXISTS `t_compare`;
CREATE TABLE `t_compare` (
`rid` bigint(20) NOT NULL,
`verno` int(11) NULL DEFAULT 1 COMMENT '版本号',
`add_account` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '添加者',
`add_time` datetime(0) NULL DEFAULT NULL COMMENT '添加时间',
`upd_account` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '更新者',
`upd_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
`compare_fields` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '比较的字段名称',
`compare_fields_n` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '比较字段中文',
`status_flag` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '是否有效',
`field_grouping` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '字段分组',
PRIMARY KEY (`rid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
|