MyBatis源码通~SqlSource

SqlSource

构建动态SQL

//XMLStatementBuilder.parseStatementNode()
SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass);

涉及类

  • LanguageDriverRegistry+LanguageDriver
  • XMLStatementBuilder
  • SqlSource+SqlNode+SqlSourceBuilder
  • MappedStatement

0、 解析Statement中一些重要点

  • SqlSource如何构建?
  • Sql语句中如“#{xxx}”和“${xxx}‘如何处理?
  • SqlNode是如何创建的?

1、LanguageDriverRegistry+LanguageDriver

在初始化Configuration时,会预先初始化MyBatis提供的两个LanguageDriver的实现类并注册到LanguageDriverRegistry中:RawLanguageDriverXMLLanguageDriver,默认为XMLLanguageDriver

  • LanguageDriverRegistry中用LANGUAGE_DRIVER_MAP来缓存LanguageDriver
    • key:LanguageDriver的Class
    • value:LanguageDriver的实例对象(newInstance())

MyBatis源码通~SqlSource_第1张图片

1.1、XMLLanguageDriver

作用于解析select|update|insert|delete节点为完整的SQL语句,对应XML格式的配置文件,创建SqlSourceParameterhandler

  • 主要方法1:createSqlSource(...):调用XMLScriptBuilder创建SqlSource
  • 主要方法2:createParameterHandler(...): 创建DefaultParameterHandler
//☆☆--XMLLanguageDriver
public class XMLLanguageDriver implements LanguageDriver {
  @Override
  public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
    //NOTE: DefaultParameterHandler
    return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
  }

  @Override
  public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
    //NOTE: 通过XMLScriptBuilder构建SqlSource
    XMLScriptBuilder builder = new XMLScriptBuilder(configuration, script, parameterType);
    return builder.parseScriptNode();
  }

  @Override
  public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
    //NOTE: 传入"
                    
                    

你可能感兴趣的:(MyBatis,SqlSource,MyBatis源码通,MyBatis源码通)