【Mybatis】调试查看执行的 SQL 语句-CSDN博客
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
1. 问题场景
记录日常开发过程中 Mybatis 调试 SQL 语句想要查看Mybatis 中执行的 SQL语句导致定位问题困难
2. 解决方式
双击shift找到mybatis源码中的 MappedStatement的getBoundSql()方法
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了