java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > MyBatis-Plus输出完整SQL

MyBatis-Plus输出完整SQL(带参数)的三种方案

作者:( •̀∀•́ )920

当我们使用 mybatis-plus 时,可能会遇到SQL 不能直接执行,调试也不方便的情况,那么,如何打印完整 SQL(带参数)呢?本篇文章将介绍 3 种实现方式,并对比它们的优缺点,需要的朋友可以参考下

为什么 MyBatis Plus 默认 SQL 日志没有参数?

当你使用 mybatis-plus 时,可能会遇到这样的情况:

Preparing: SELECT id, name, `desc` FROM author WHERE name = ?
Parameters: 刘禹锡(String)

这导致 SQL 不能直接执行,调试也不方便。那么,如何打印完整 SQL(带参数)呢?本篇文章将介绍 3 种实现方式,并对比它们的优缺点。

方案 1:使用 SqlLogInterceptor(推荐)

从 MyBatis Plus 3.5.3 版本 开始,官方提供了 SqlLogInterceptor,可以自动替换 SQL 语句中的 ? 为实际参数。

配置方式:

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.SqlLogInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new SqlLogInterceptor()); // ✅ 让 SQL 带参数
        return interceptor;
    }
}

效果:

Executing SQL: SELECT id, name, `desc` FROM author WHERE name = '刘禹锡'

优点:

缺点:

方案 2:自定义 MyBatis Interceptor

如果你使用的是 原生 MyBatis,或者想要更灵活的日志格式,可以 自定义 Interceptor

编写 Interceptor:

@Intercepts({
    @Signature(type = Executor.class, method = "query", args = {
        MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
    @Signature(type = Executor.class, method = "update", args = {
        MappedStatement.class, Object.class})
})
@Slf4j
public class SqlInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object proceed = invocation.proceed();
        long endTime = System.currentTimeMillis();

        String sql = generateSql(invocation);
        log.info("\n 执行SQL耗时:{}ms \n 执行SQL:{}", endTime - startTime, sql);
        return proceed;
    }

    private static String generateSql(Invocation invocation) {
        MappedStatement statement = (MappedStatement) invocation.getArgs()[0];
        Object parameter = invocation.getArgs().length > 1 ? invocation.getArgs()[1] : null;
        BoundSql boundSql = statement.getBoundSql(parameter);

        String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
        for (ParameterMapping param : boundSql.getParameterMappings()) {
            Object value = boundSql.getAdditionalParameter(param.getProperty());
            sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(getParameterValue(value)));
        }
        return sql;
    }

    private static String getParameterValue(Object object) {
        return object instanceof String ? "'" + object + "'" : String.valueOf(object);
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }
}

优点:

缺点:

方案 3:开启 MyBatis 日志(手动拼接 SQL)

如果只是 临时调试,可以在 application.yml 配置 MyBatis 日志:

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

然后,手动拼接 SQL 进行调试。

优点:

缺点:

最佳方案对比与选择

方案适用场景侵入性性能开销适用框架
SqlLogInterceptorMyBatis PlusMyBatis Plus
自定义 Interceptor需要特殊格式中等MyBatis & MyBatis Plus
MyBatis 日志临时调试MyBatis & MyBatis Plus

结论:哪种方式适合你?

到此这篇关于MyBatis-Plus输出完整SQL(带参数)的三种方案的文章就介绍到这了,更多相关MyBatis-Plus输出完整SQL内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文