实体字段比较器(对比两组对象找出发生变化的字段工具类开发)
支持枚举映射
支持时间
支持显示对应字段中文描述
支持嵌套list等场景
下载地址:
Java-对比两组对象找出发生变化的字段工具-支持枚举映射-支持时间-支持显示对应字段中文描述-嵌套list等场景-超越其他工具.rar
最新版本更新
https://code.jiangjiesheng.cn/article/366?from=csdn
package cn.jiangjiesheng.annoation.entitycomparator.demo;import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import cn.jiangjiesheng.service.impl.common.entitycomparator.demo.bizentity.SubTask;
import cn.jiangjiesheng.service.impl.common.entitycomparator.demo.bizentity.Task;
import cn.jiangjiesheng.service.impl.common.entitycomparator.demo.bizenum.Enum1Priority;
import cn.jiangjiesheng.service.impl.common.entitycomparator.demo.bizenum.Enum2Status;
import cn.jiangjiesheng.service.impl.common.entitycomparator.demo.bizenum.Enum3Type;
import cn.jiangjiesheng.service.impl.common.entitycomparator.impl.EntityComparator;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;/*** 对比两组对象找出发生变化的字段工具类 测试验证*/
public class EntityComparatorDemo {public static void main(String[] args) throws Exception {String dateStr = "2025-03-04 12";DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");// 解析字符串为LocalDateTime对象LocalDateTime localDateTime = LocalDateTime.parse(dateStr, formatter);// 如果需要转换为旧的Date类型Date date = java.sql.Timestamp.valueOf(localDateTime);SubTask oldSubTask = new SubTask("SubTaskOld", date, Enum2Status.PENDING, "2025-01-01");SubTask newSubTask = new SubTask("SubTaskNew", DateUtil.beginOfWeek(new Date()), Enum2Status.COMPLETED, "2025-01-02");List<SubTask> oldSubTasks = new ArrayList<>();oldSubTasks.add(oldSubTask);List<SubTask> newSubTasks = new ArrayList<>();newSubTasks.add(newSubTask);//再来一组,老的是空newSubTasks.add(newSubTask);Task oldTask = new Task(Enum1Priority.LOW, Enum2Status.PENDING, Enum3Type.TYPE_A.getCode(), oldSubTasks, oldSubTask, "2025-01-01");Task newTask = new Task(Enum1Priority.HIGH, Enum2Status.COMPLETED, Enum3Type.TYPE_B.getCode(), newSubTasks, newSubTask, "2025-01-02");List<EntityComparator.ChangeInfo> changes = EntityComparator.compareEntities(oldTask, newTask);// for (ChangeInfo change : changes) {// System.out.println("Field: " + change.getFieldName());// System.out.println("Description: " + change.getFieldDesc());// System.out.println("Old Value: " + change.getOldValue());// System.out.println("New Value: " + change.getNewValue());// System.out.println();// }//这个保持了顺序,但是json结构混乱System.out.println("保持了顺序,但是json结构混乱:");System.out.println(EntityComparator.ChangeInfo.toOrderedJson(changes));System.out.println();// 这个未保持顺序,但是json结构清晰System.out.println("未保持顺序,但是json结构清晰:");System.err.println(JSONUtil.toJsonPrettyStr(changes));}
}
对比结果示例:
[ {"fieldDesc" : "任务优先级","fieldName" : "enum1Priority","newValue" : "高优先级","oldValue" : "低优先级"
},{"fieldDesc" : "任务状态","fieldName" : "enum2Status","newValue" : "已完成","oldValue" : "待处理"
},{"fieldDesc" : "任务类型","fieldName" : "taskType","newValue" : "类型B","oldValue" : "类型A"
},{"child" : [
[ {"fieldName" : "subTasks[0].name","fieldDesc" : "子任务名称","oldValue" : "","newValue" : "SubTaskNew"},{"fieldName" : "subTasks[0].dateTime","fieldDesc" : "时间日期类型","oldValue" : "","newValue" : "2025-04-07 00"},{"fieldName" : "subTasks[0].enum2Status","fieldDesc" : "子任务状态","oldValue" : "","newValue" : "已完成"},{"fieldName" : "subTasks[0].createdAt","fieldDesc" : "创建时间","oldValue" : "","newValue" : "2025-01-02"} ],
[ {"fieldName" : "subTasks[1].name","fieldDesc" : "子任务名称","oldValue" : "","newValue" : "SubTaskNew"},{"fieldName" : "subTasks[1].dateTime","fieldDesc" : "时间日期类型","oldValue" : "","newValue" : "2025-04-07 00"},{"fieldName" : "subTasks[1].enum2Status","fieldDesc" : "子任务状态","oldValue" : "","newValue" : "已完成"},{"fieldName" : "subTasks[1].createdAt","fieldDesc" : "创建时间","oldValue" : "","newValue" : "2025-01-02"} ],
[ {"fieldName" : "subTasks[2].name","fieldDesc" : "子任务名称","oldValue" : "","newValue" : "SubTaskNew"},{"fieldName" : "subTasks[2].dateTime","fieldDesc" : "时间日期类型","oldValue" : "","newValue" : "2025-04-07 00"},{"fieldName" : "subTasks[2].enum2Status","fieldDesc" : "子任务状态","oldValue" : "","newValue" : "已完成"},{"fieldName" : "subTasks[2].createdAt","fieldDesc" : "创建时间","oldValue" : "","newValue" : "2025-01-02"}]],"fieldDesc" : "子任务列表","fieldName" : "subTasks"
},{"fieldDesc" : "子任务名称","fieldName" : "subTask.name","newValue" : "SubTaskNew","oldValue" : "SubTaskOld"
},{"fieldDesc" : "时间日期类型","fieldName" : "subTask.dateTime","newValue" : "2025-04-07 00","oldValue" : "2025-03-04 12"
},{"fieldDesc" : "子任务状态","fieldName" : "subTask.enum2Status","newValue" : "已完成","oldValue" : "待处理"
},{"fieldDesc" : "创建时间","fieldName" : "subTask.createdAt","newValue" : "2025-01-02","oldValue" : "2025-01-01"
},{"fieldDesc" : "创建时间","fieldName" : "createdAt","newValue" : "2025-01-02","oldValue" : "2025-01-01"
} ]
最新版本更新
https://code.jiangjiesheng.cn/article/366?from=csdn