一、相关类及接口
-
SqlSource接口
-
DynamicSqlSource
主要是封装动态SQL标签解析之后的SQL语句和带有${}的SQL语句
-
RawSqlSource
主要封装带有#{}的SQL语句
-
StaticSqlSource
是BoundSql中要存储SQL语句的一个载体,上面两个SqlSource的SQL语句,最终都会存储到该SqlSource实现类中
-
SqlSourceBuilder
主要完成了两方面的操作,一方面是解析Sql中的#{}占位符定义的属性,如jdbcType、javaType(使用较少),一方面是把#{}占位符替换成?占位符
-
ParameterMappingTokenHandler
SqlSourceBuilder的一个内部类,该类是解析#{}的核心。
-
GenericTokenParser
分词解析器
二、流程图
三、流程分析
-
入口:MappedStatement#getBoundSql
public BoundSql getBoundSql(Object parameterObject) {
// 调用SqlSource获取BoundSql
BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
if (parameterMappings == null || parameterMappings.isEmpty()) {
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;
}
-
DynamicSqlSource#getBoundSql
@Override
public BoundSql