如何记录下数据库数据的变更日志
auditLog-reporter能帮助你轻松实现,它对springboot项目无侵入、可拔插,兼容mysql、Oracle、Postgres等多种关系型数据库,支持复杂sql(比如update join,delete join等操作),支持统计分析,支持操作溯源,项目中更是只需一个注解即可实现记录数据库数据字段更新前后的信息,让数据尽在掌控!
演示示例
- 变更前数据
- java代码
@AuditLog
@Transactional(rollbackFor = Exception.class)
public Object update() throws SQLException {
Test test = new Test();
test.setC1("11");
test.setC3("11");
test.setId("1");
// 示例代码使用了mybatisplus插件
tbService.updateById(test);
Test test2 = new Test();
test2.setC1("22");
test2.setC2("22");
test2.setId("2");
tbService.updateById(test2);
Test test3 = new Test();
test3.setC1("3");
test3.setC3("3");
test3.setId("3");
tbService.save(test3);
return "ok";
}
- 变更后数据
- 输出日志
{
"tableChangeLogs": [
{
"rowChangeLogs": [
{
"fieldChangeLogs": [
{
"oldValue": "1",
"newValue": "11",
"name": "C1"
},
{
"oldValue": "1",
"newValue": "11",
"name": "C3"
}
],
"keyMap": {
"ID": "1"
},
"sqlType": "UPDATE"
},
{
"fieldChangeLogs": [
{
"oldValue": "2",
"newValue": "22",
"name": "C1"
},
{
"oldValue": "2",
"newValue": "22",
"name": "C2"
}
],
"keyMap": {
"ID": "2"
},
"sqlType": "UPDATE"
},
{
"fieldChangeLogs": [
{
"newValue": "3",
"name": "ID"
},
{
"newValue": "3",
"name": "C1"
},
{
"newValue": "3",
"name": "C3"
}
],
"keyMap": {
"ID": "3"
},
"sqlType": "INSERT"
}
],
"tableName": "TEST.TEST"
}
],
"userId": "-1",
"userName": "系统默认",
"operationId": "1694551842220519424",
"statistician": {
"sqlQuta": {
"UPDATE test.test SET c1=?,\n\nc3=? WHERE id=?": {
"executeCount": 1,
"executeTimes": 5735400,
"executeMaxTime": 5735400,
"executeMinTime": 5735400
},
"UPDATE test.test SET c1=?,\nc2=? WHERE id=?": {
"executeCount": 1,
"executeTimes": 2631600,
"executeMaxTime": 2631600,
"executeMinTime": 2631600
},
"INSERT INTO test.test ( id,\nc1,\n\nc3 ) VALUES ( ?,\n?,\n\n? )": {
"executeCount": 1,
"executeTimes": 7346400,
"executeMaxTime": 7346400,
"executeMinTime": 7346400
}
}
},
"metaData": {},
"operationName": ""
}
项目地址
更多功能期待你的探索!
点击跳转到项目码云地址