当前位置: 首页 > news >正文

使用mybatis的statementHander拦截器监控表和字段并发送钉钉消息

新建mybatis的statementHander拦截器拦截器 类

面试题:

2.实现

解析Sql时引入JSqlParser

JSqlParser 是一个 SQL 语句解析器。 它将 SQL转换为可遍历的 Java 类层次结构。 

<dependency><groupId>com.github.jsqlparser</groupId><artifactId>jsqlparser</artifactId><version>4.6</version></dependency>

 添加拦截器代码:

package com.yy.config;import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.smart.model.LocalUser;
import cn.smart.util.ThreadlocalUtil;
import com.yy.entity.BaseEntity;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.update.UpdateSet;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@Component
@Intercepts({// 指定要拦截的方法签名,这里是拦截Executor的update方法@Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),// 可以添加更多要拦截的方法签名...
})
public class StatementHanderInterceptor implements Interceptor {private static List<String> tables = CollUtil.newArrayList("product","category");private static Map<String,List<String>> tableColumns = new HashMap<>();static {tableColumns.put("product",CollUtil.newArrayList("name","price"));}@Overridepublic Object intercept(Invocation invocation) throws Throwable {StatementHandler statementHandler =(StatementHandler)invocation.getTarget();//要想监控表,首先要从拦截器中拿到sql语句,看sql语句干了什么,对那些表和那些字段做了什么BoundSql boundSql = statementHandler.getBoundSql();String sql = boundSql.getSql();
//        使用JSqlParser解析器解析sql语句net.sf.jsqlparser.statement.Statement statement = CCJSqlParserUtil.parse(sql);if(statement instanceof Update){Update update = (Update)statement;String name = update.getTable().getName();if(tables.contains(name)){List<String> updataSets = tableColumns.get(name);ArrayList<UpdateSet> updateSets = update.getUpdateSets();for (UpdateSet updateSet : updateSets) {String columnName = updateSet.getColumns().get(0).getColumnName();if(updataSets.contains(columnName)){sendMessage(   "修改了字段"+updateSet,name);}}}}else if(statement instanceof Insert){Table table = ((Insert) statement).getTable();String name = table.getName();if(tables.contains(name)){sendMessage("添加了数据",name);}}return invocation.proceed();}private void sendMessage(String option,String tableName){String url= "https://oapi.dingtalk.com/robot/send?access_token=1cfb9a7b20e849a26b572a8ff98f62ee2a220028cd1452901a486e131435621f";JSONArray array = new JSONArray();array.add("18236435312");JSONObject msg = new JSONObject();msg.set("msgtype","text");msg.set("text",new JSONObject().set("content","警告:"+ThreadlocalUtil.get().getNickName()+"-> "+option+" ,表名是:"+tableName));msg.set("at",new JSONObject().set("atMobiles",array));String json = JSONUtil.toJsonStr(msg);String body = HttpRequest.post(url).body(json).execute().body();}}

遇到的问题:

因为这个pagehelper是从stater中继承过来的,可以在stater中也加一个4.6版本的jsqlparser依赖

 

http://www.lryc.cn/news/401049.html

相关文章:

  • 信贷系统——基础信贷概念
  • 分页查询及其拓展应用案例
  • 【UE5.1】NPC人工智能——02 NPC移动到指定位置
  • 有关电力电子技术的一些相关仿真和分析:⑤交-直-交全桥逆变+全波整流结构电路(MATLAB/Siumlink仿真)
  • 记录一次Android推流、录像踩坑过程
  • VsCode 与远程服务器 ssh免密登录
  • 7/13 - 7/15
  • 烟雾监测与太阳能源:实验装置在其中的作用
  • QT下,如何获取控制台输入
  • mybatis动态传入参数 pgsql 日期 Interval ,day,minute
  • 常见CSS属性
  • WSL-Ubuntu20.04训练环境配置
  • 运维检查:mysql表自增id是否快要用完
  • 深入理解FFmpeg--libavformat接口使用(一)
  • 坚持日更的意义何在?
  • 内容长度不同的div如何自动对齐展示
  • Qt中https的使用,报错TLS initialization failed和不能打开ssl.lib问题解决
  • P2p网络性能测度及监测系统模型
  • zookeeper相关总结
  • 【openwrt】Openwrt系统新增普通用户指南
  • 【GD32】从零开始学GD32单片机 | WDGT看门狗定时器+独立看门狗和窗口看门狗例程(GD32F470ZGT6)
  • 详解曼达拉升级:如何用网络拓扑结构扩容BSV区块链
  • 编译打包自己的云手机(redroid)镜像
  • 自动驾驶的规划控制简介
  • java配置nginx网络安全,防止国外ip访问,自动添加黑名单,需手动重新加载nginx
  • ARP协议
  • Qt程序图标更改以及程序打包
  • 普通人还有必要学习 Python 之类的编程语言吗?
  • 「Python」基于Gunicorn、Flask和Docker的高并发部署
  • 在攻防演练中遇到的一个“有马蜂的蜜罐”