SpringBoot项目自定义拦截器打印SQL语句或者进行SQL日志收集

本文介绍了如何在MyBatis中使用拦截器来实现SQL日志的打印,包括查询和更新操作的拦截。通过创建`MyBatisSqlQuerySqlDebugPlugin`和`MyBatisSqlUpdateSqlDebugPlugin`两个拦截器类,分别处理查询和更新的SQL语句,并在配置类中注册这些拦截器,以便在开发环境中进行SQL日志的调试。
摘要由CSDN通过智能技术生成

拦截器的基本使用,本文不涉及,请看此篇文章介绍

定义SQL语句拦截器

查询SQL语句拦截器

/**
 * [ mybaits sql 拦截器 ]
 *
 * @author be_insighted
 */
@Intercepts({@Signature(type = org.apache.ibatis.executor.Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
@Slf4j
public class MyBatisSqlQuerySqlDebugPlugin implements Interceptor {
 
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        Object parameter = invocation.getArgs()[1];
        BoundSql boundSql = mappedStatement.getBoundSql(parameter);
        log.info("=====> SQL日志查询:\n" + boundSql.getSql() + "\r\n" + boundSql.getParameterMappings().toString());
        Object obj = invocation.proceed();
        return obj;
    }
 
    @Override
    public Object plugin(Object arg0) {
        return Plugin.wrap(arg0, this);
    }
 
    @Override
    public void setProperties(Properties arg0) {
 
    }
 
}

更新类SQL语句拦截器

@Intercepts({@Signature(type = org.apache.ibatis.executor.Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
@Slf4j
public class MyBatisSqlUpdateSqlDebugPlugin implements Interceptor {
 
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        Object parameter = invocation.getArgs()[1];
        BoundSql boundSql = mappedStatement.getBoundSql(parameter);
        log.info("=====> SQL日志Update操作:\n " + boundSql.getSql() + "\r\n" + boundSql.getParameterMappings().toString());
        Object obj = invocation.proceed();
        return obj;
    }
 
    @Override
    public Object plugin(Object arg0) {
        return Plugin.wrap(arg0, this);
    }
 
    @Override
    public void setProperties(Properties arg0) {
 
    }

在配置类中将拦截器纳入到Spring容器

@EnableTransactionManagement
@Configuration
@MapperScan(basePackages = {"com.iot.**.*.dao"})
public class IotMybatisPlusConfig {
 
    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        // left join的分页记录数不放大才能使用此优化 不然需要手工关闭优化配置 暂时无须此优化 先统一关闭了
        // 开启 count 的 join 优化,只针对 left join !!!
        // return new PaginationInterceptor().setCountSqlParser(new JsqlParserCountOptimize(true));
        return new PaginationInterceptor();
    }
 
    /**
     * 打印sql log
     *
     * @return
     */
    @Bean
    @Profile("dev")
    ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return configuration -> {
            configuration.addInterceptor(new MyBatisSqlUpdateSqlDebugPlugin());
            configuration.addInterceptor(new MyBatisSqlQuerySqlDebugPlugin());
        };
    }
}

以上便是拦截器的使用,打印日志,以及@Profile注解的应用。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值