SoFunction
Updated on 2025-04-05

Three solutions for MyBatis-Plus to output complete SQL (with parameters)

Why does MyBatis Plus have no parameters in the default SQL log?

When you usemybatis-plusWhen you are  , you may encounter such a situation:

Preparing: SELECT id, name, `desc` FROM author WHERE name = ?
Parameters: Liu Yuxi(String)

This causes SQL to be executed directly and debugging is not convenient. So, how to print complete SQL (with parameters)? This article will introduce 3 implementation methods and compare their advantages and disadvantages.

Solution 1: Use SqlLogInterceptor (recommended)

Starting from MyBatis Plus version 3.5.3, the official provides SqlLogInterceptor, which can automatically replace ? in SQL statements with actual parameters.

Configuration method:

import ;
import ;
import ;
import ;

@Configuration
public class MyBatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        (new SqlLogInterceptor()); // ✅ Let SQL take parameters        return interceptor;
    }
}

Effect:

Executing SQL: SELECT id, name, `desc` FROM author WHERE name = 'Liu Yuxi'

advantage:

  • Official support, non-invasive, simple and easy to use
  • Low performance overhead, only affects log output
  • For MyBatis Plus

shortcoming:

  • Applicable onlyMyBatis Plus(Ordinary MyBatis requires solution 2)

Solution 2: Customize MyBatis Interceptor

If you are usingNative MyBatis, or if you want a more flexible log format, you canCustomInterceptor

Write Interceptor:

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

        String sql = generateSql(invocation);
        ("\n implementSQLtime consuming:{}ms \n implementSQL:{}", endTime - startTime, sql);
        return proceed;
    }

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

        String sql = ().replaceAll("[\\s]+", " ");
        for (ParameterMapping param : ()) {
            Object value = (());
            sql = ("\\?", (getParameterValue(value)));
        }
        return sql;
    }

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

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

advantage:

  • Suitable forNative MyBatisandMyBatis Plus
  • Customize log format and control output content

shortcoming:

  • Need to be written manually, slightly more complicated
  • A little overhead(Requires parsing SQL and replacing parameters)

Solution 3: Turn on MyBatis logs (manually splicing SQL)

If it's justTemporary debugging, can be found inConfigure MyBatis logs:

mybatis-plus:
  configuration:
    log-impl: 

Then, manually splice SQL for debugging.

advantage:

  • No code changes, suitable for temporary debugging

shortcoming:

  • StillPreparing: xxxandParameters: xxxSeparate format
  • Cannot be copied and executed directly

Best Solution Comparison and Choice

plan Applicable scenarios Invasive Performance overhead Applicable framework
SqlLogInterceptor MyBatis Plus none Low MyBatis Plus
Custom Interceptor Requires a special format medium middle MyBatis & MyBatis Plus
MyBatis log Temporary debugging none Low MyBatis & MyBatis Plus

Conclusion: Which method is right for you?

  • recommend:useSqlLogInterceptor, simple, non-invasive, suitable forMyBatis Plus
  • Advanced: CustomInterceptor, suitable forMyBatis, customize log format.
  • Temporary debugging: Turn on MyBatis log, but not automatically replace?

This is the end of this article about the three solutions for MyBatis-Plus to output full SQL (with parameters). For more related content related to MyBatis-Plus to output full SQL, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!