【Mybatis】调试查看执行的 SQL 语句

1. 问题场景:

记录日常开发过程中 Mybatis 调试 SQL 语句,想要查看Mybatis 中执行的 SQL语句,导致定位问题困难

2. 解决方式

双击shift找到mybatis源码中的 MappedStatementgetBoundSql()方法

public BoundSql getBoundSql(Object parameterObject) {
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    if (parameterMappings == null || parameterMappings.size() <= 0) {
      boundSql = new BoundSql(configuration, boundSql.getSql(), parameterMap.getParameterMappings(), parameterObject);
    }

    // check for nested result maps in parameter mappings (issue #30)
    for (ParameterMapping pm : boundSql.getParameterMappings()) {
      String rmId = pm.getResultMapId();
      if (rmId != null) {
        ResultMap rm = configuration.getResultMap(rmId);
        if (rm != null) {
          hasNestedResultMaps |= rm.hasNestedResultMaps();
        }
      }
    }

    return boundSql;
  }

Mybatis 的底层都会把 Mapper.xml 配置文件中的SQL 标签转化为基于 JDBC 执行的语句, boundSql 变量可以看到完整的 SQL 语句

因此,可以在最后一行的return boundSql;打一个断点。执行 SQL 查询语句,就能查看到执行的sql了
【Mybatis】调试查看执行的 SQL 语句_第1张图片

你可能感兴趣的:(mybatis,sql,java)