MyBatis源码(六)之动态Sql解析运行阶段BoundSql

上篇博文讲述的是dao方法参数是如何传递到Executor的MyBatis源码(五)之动态Sql解析运行阶段参数处理 一定要有这个基础,在进行本博文你将看到一条Sql是如何拼装出来的。

Executor的query方法

我把运行时信息打印出来,主要是在顺便回顾下之前的讲解读取mapper过程,解析select insert 等标签的时候有一行代码是获取SqlSouce的,解析的时候有一个动态标签 if foreach where 等,这个sql就是DynamicSqlSouce。

一条Sql根据内部标签 include where foreach等拆分成不同的sqlNode,图中DynamicSqlSource中的成员rootSqlNode就是MixedSqlNode包含了所有的SqlNode如右图的,每个sqlNode都是递归的!!!
可以回顾下MyBatis源码(四)之mapper文件解析和动态Sql解析启动阶段

上面这一张截图,获取BoundSql就是本篇的全部,包含sql的最终定义。

public class DynamicContext {

  public static final String PARAMETER_OBJECT_KEY = "_parameter";
  public static final String DATABASE_ID_KEY = "_databaseId";
  private final ContextMap bindings;
  private final StringBuilder sqlBuilder = new StringBuilder();
  private int uniqueNumber = 0;

  public DynamicContext(Configuration configuration, Object parameterObject) {
    if (parameterObject != null && !(parameterObject instanceof Map)) {
      MetaObject metaObject = configuration.newMetaObject(parameterObject);
      bindings = new ContextMap(metaObject);
    } else {
      bindings = new ContextMap(null);
    }
    bindings.put(PARAMETER_OBJECT_KEY, parameterObject);
    bindings.put(DATABASE_ID_KEY, configuration.getDatabaseId());
  }
 }

猜你喜欢

转载自blog.csdn.net/mayongzhan_csdn/article/details/78623670