mybatis语句的存储

sqlSession --|》configuration --|》 MappedStatement --|》 SqlSource --|》BoundSql

XMLStatementBuilder 解析xml,解析qlSource等属性,生成MappedStatement

SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass);
根据xml解析,生成SqlSource

String resultSets = context.getStringAttribute("resultSets"); // context 是一个XNode ,就是一个类似这种
String keyProperty = context.getStringAttribute("keyProperty");
String keyColumn = context.getStringAttribute("keyColumn");
获取解析的resultSets、keyProperty、eyColumn

builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType,
fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass,
resultSetTypeEnum, flushCache, useCache, resultOrdered,
keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets);
把解析的这些属性,通过MapperBuilderAssistant添加一个MappedStatement到configuration中。

MapperBuilderAssistant

public MappedStatement addMappedStatement(
String id,
SqlSource sqlSource,
StatementType statementType,
SqlCommandType sqlCommandType,
Integer fetchSize,
Integer timeout,
String parameterMap,
Class<?> parameterType,
String resultMap,
Class<?> resultType,
ResultSetType resultSetType,
boolean flushCache,
boolean useCache,
boolean resultOrdered,
KeyGenerator keyGenerator,
String keyProperty,
String keyColumn,
String databaseId,
LanguageDriver lang,
String resultSets) {...}
把MappedStatement添加到configuration中。在XMLScriptBuilder的parseScriptNode()中调用了addMappedStatement这个方法

MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, id, sqlSource, sqlCommandType);
生成MappedStatement.Builder,传入了sqlSource。

MappedStatement statement = statementBuilder.build();
构造MappedStatement

configuration.addMappedStatement(statement);
添加到configuration

XMLLanguageDriver 创建SqlSource的工厂。

public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {...}
XMLScriptBuilder builder = new XMLScriptBuilder(configuration, script, parameterType);
return builder.parseScriptNode();

XMLScriptBuilder

public XMLScriptBuilder(Configuration configuration, XNode context, Class<?> parameterType) {...}
构造函数,传入XNode进去给它们解析

public SqlSource parseScriptNode() {...}
解析XNode生成SqlSource
List contents = parseDynamicTags(context);
解析XNode,获取当前XNode的所有SqlNode
MixedSqlNode rootSqlNode = new MixedSqlNode(contents);
sqlSource = new DynamicSqlSource(configuration, rootSqlNode);
return sqlSource;

private List parseDynamicTags(XNode node){...}

NodeHandler handler = nodeHandlers.get(nodeName);
handler.handleNode(child, contents);
根据nodeName获取相应类型的NodeHandler,去解析生成SQLNode // SQLNode是 trim、if、where 这些

StaticSqlSource SqlSource是一个工厂,用于生产BoundSql

public BoundSql getBoundSql(Object parameterObject) {...}
return new BoundSql(configuration, sql, parameterMappings, parameterObject);

原文地址:https://www.cnblogs.com/kltsee/p/15168465.html