前面分析到SqlNode.apply()后,Sql还是个半成品。只处理了"${}"这种占位符,"#{}"这种占位符还没有处理,而且Sql执行时的参数也没有生成。
再来看DynamicSqlSource.getBoundSql()方法
public BoundSql getBoundSql(Object parameterObject) {
DynamicContext context = new DynamicContext(configuration, parameterObject);
//这里只处理了"${}"占位符
rootSqlNode.apply(context);
SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
//这里就是处理"#{}"占位符的地方
SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings());
//这个BoundSql就是数据库可执行的Sql,同时还包含了运行时的参数。
BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) {
boundSql.setAdditionalParameter(entry.getKey()