网站开发时如何兼容,云南网app下载,镇江网站定制,最近十大新闻新建mybatis的statementHander拦截器拦截器 类 面试题#xff1a;
2.实现
解析Sql时引入JSqlParser
JSqlParser 是一个 SQL 语句解析器。 它将 SQL转换为可遍历的 Java 类层次结构。
dependencygroupIdcom.github.jsqlparser/groupIdartifac…新建mybatis的statementHander拦截器拦截器 类 面试题
2.实现
解析Sql时引入JSqlParser
JSqlParser 是一个 SQL 语句解析器。 它将 SQL转换为可遍历的 Java 类层次结构。
dependencygroupIdcom.github.jsqlparser/groupIdartifactIdjsqlparser/artifactIdversion4.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 ListString tables CollUtil.newArrayList(product,category);private static MapString,ListString 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)){ListString updataSets tableColumns.get(name);ArrayListUpdateSet 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_token1cfb9a7b20e849a26b572a8ff98f62ee2a220028cd1452901a486e131435621f;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依赖