mybatis通过拦截器获取sql语句,操作类型,数据库链接URL,库名,表名,处理字段名

@Intercepts({
        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
})
public class AcesInterceptor implements Interceptor {
    private Properties properties = new Properties();

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        try {
            MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
            // 获取操作类型
            String sqlCommandType = mappedStatement.getSqlCommandType().name();
            if ("INSERT".equals(sqlCommandType)) {
                Object parameterObject = invocation.getArgs().length > 1 ? invocation.getArgs()[1] : null;
                // 获取 SQL 语句
                BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
                String sql = boundSql.getSql();
                // 获取数据库连接信息
                Connection connection = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();
                String url = connection.getMetaData().getURL();
                String catalog = connection.getCatalog();
                String tableName = extractTableName(sql);
                List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
                for (ParameterMapping p:parameterMappings) {
                    if (p.getTypeHandler() instanceof AcesCipherTextHandle){
                        MetaDataInfo metaDataInfo = new MetaDataInfo();
                        metaDataInfo.setDbHost(url);
                        metaDataInfo.setCatalogName(catalog);
                        metaDataInfo.setBoundSql(sql);
                        metaDataInfo.setTableName(tableName);
                        metaDataInfo.setColumnName(p.getProperty());
                        MqEventWork.getIns().addMQ(metaDataInfo);
                    }
                }
            }
        }catch (Exception e){


        }

        // 继续执行拦截链中的下一个拦截器
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        //获取代理权
        if ((properties.getProperty("enable").equals("true")) && target instanceof Executor) {
            //如果是Executor(执行增删改查操作),否则拦截下来
            return Plugin.wrap(target, this);
        } else {
            return target;
        }
    }

    @Override
    public void setProperties(Properties properties) {
        // 接收配置属性
        this.properties = properties;

    }

    // 提取表名的逻辑
    private String extractTableName(String sql) {
        int startIndex = sql.toLowerCase().indexOf("insert into") + 11;
        int endIndex = sql.indexOf("(", startIndex);
        return sql.substring(startIndex, endIndex).trim();
    }

}
<?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>


    <plugins>
        <plugin interceptor=".interceptor.AcesInterceptor">
            <property name="enable" value="false"/>
        </plugin>

    </plugins>

</configuration>
  • 22
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值