【操作日志】如何在一个SpringBoot+Mybatis的项目中设计一个自定义ChangeLo ...

打印 上一主题 下一主题

主题 911|帖子 911|积分 2733

  设计一个业务改动信息时的自定义记录,例如新增、修改、删除数据等。并且记录的规则可以通过配置的方式控制。大家需要根据各自业务场景参考,欢迎讨论。伪代码如下:
实体类:
  1. @TableName("tbl_user")
  2. User{
  3.     String id
  4.     String name
  5.     Integer age
  6.     String addr
  7. }
复制代码
 


DAO层:
  1. UserDao{
  2.     getById(Long id);
  3.     list(Wrapper wrapper);
  4. }
复制代码
 
 
自定义注解:
  1. MybatisPropAnno{
  2.   String value
  3.   Class<?> daoClazz
  4. }
复制代码
 


Http接口请求参数:
  1. SaveReq{
  2.     @MybatisPropAnno(value="name", daoClazz = UserDao.class)
  3.     String userName;
  4.     @MybatisPropAnno(value="age", daoClazz = UserDao.class)
  5.     Integer userAge;
  6. }
  7. UpdateReq{
  8.     @MybatisPropAnno(value="id", daoClazz = UserDao.class)
  9.     String userNo;
  10.     @MybatisPropAnno(value="name", daoClazz = UserDao.class)
  11.     String userName;
  12.     @MybatisPropAnno(value="age", daoClazz = UserDao.class)
  13.     Integer userAge;
  14.     @MybatisPropAnno(value="id", daoClazz = DepartDao.class)
  15.     String departId;
  16.     String departAddress;
  17. }
复制代码
 


Http接口:
  1. TestController{
  2.     public void save(SaveReq saveReq){
  3.         operTypeInheritedThreadLocal.set(Enum."添加数据");
  4.         sthInheritedThreadLocal.set(saveReq);
  5.     };
  6.     public void update(UpdateReq updateReq){
  7.         operTypeInheritedThreadLocal.set(Enum."更新数据");
  8.         sthInheritedThreadLocal.set(updateReq);
  9.     };
  10.     @MybatisLogAnno("删除数据")
  11.     public void delete(){};
  12.     @MybatisLogAnno("查询数据")
  13.     public void query(){};
  14.     public void other(){};
  15. }
复制代码
 


SQL拦截器:
  1. SqlInterceptor{
  2.     public Object intercept(Mybatis param){   
  3.         OperType operType = operTypeInheritedThreadLocal.get
  4.         if(operType == null){
  5.             return ;
  6.         }
  7.         
  8.         // insert/update/delete/select
  9.         String type = param.getSqlType();
  10.         if(operType == select){
  11.             return ;
  12.         }
  13.         
  14.         String sql = param.getSql();
  15.         
  16.         String tableName = sql.getTableName();
  17.         List<Field> fields = Reflect.getFields(sthInheritedThreadLocal.get());
  18.         for(Field field : fields){
  19.             MybatisPropAnno propAnno = field.getAnnotation(MybatisPropAnno);
  20.             if(propAnno  == null){
  21.                 continue;
  22.             }
  23.             if(propAnno.getDaoClazz.getEntity.getTableName.equals(tableName )){
  24.                 User oldUser = new JSONObject();
  25.                 User newUser = new JSONObject();
  26.                 if(type == insert){
  27.                     oldUser = new JSONObject();
  28.                     newUser = getInsertInfo(sql);
  29.                 }
  30.                 if(type == update){
  31.                     oldUser = SpringContext.get(daoClazz).getById(field.value());
  32.                     newUser = getUpdateInfo(sql);
  33.                     insertChangeLog(new JSONObject(), JSON.toJson(newUser));
  34.                 }
  35.                 if(type == delete){
  36.                     oldUser = SpringContext.get(daoClazz).getById(field.value());
  37.                     newUser = new JSONObject();
  38.                 }
  39.                 insertChangeLog(operType, traceId, oldUser, newUser);
  40.                 break;
  41.             }
  42.         }
  43.     }
  44. }
复制代码
 


Spring切面:
  1. SpringAop {
  2.     public Object doAround(){
  3.         operTypeInheritedThreadLocal.remove
  4.     }
  5. }
复制代码
 


数据库设计:
Table:dc_change_log
id    changeTime    changeType    traceId    oldInfo        newInfo        httpMethod        table        operator    changeDesc
1    2020-01-01    添加用户        123    {}        {"name":"lil"}    com.xx.TestCtrl.saveUser    tbl_user        admin    descxxxxxxxxxxxx
2    2020-01-01    修改用户        124    {"name":"lil"}    {"name":"a"}    com.xx.TestCtrl.updateUser    tbl_user        admin    descxxxxxxxxxxxx
3    2020-01-01    修改用户        124    {"addr":"南山"}    {"addr":"福田"}    com.xx.TestCtrl.updateUser    tbl_department    admin    descxxxxxxxxxxxx

Table:dc_change_rule
id    httpMethod        table        showRule
1    com.xx.TestCtrl.saveUser    tbl_user        [{"propKey":"name", "propDesc":"name detail info"}]
1    com.xx.TestCtrl.updateUser    tbl_user        [{"propKey":"name", "propDesc":"name detail info"}]
1    com.xx.TestCtrl.updateUser    tbl_department    [{"propKey":"addr", "propDesc":"addr detail info"}]

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

慢吞云雾缓吐愁

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表