mybatis源码解析

一、Mapper接口动态代理的创建以及增删改查的执行

1.生成Configuration

可通过多种方式读取解析mybatis-cofig.xml文件来生成Configuration

//解析配置文件
public void testConfiguration() throws IOException {
   
    Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
    // 创建XMLConfigBuilder实例
    XMLConfigBuilder builder = new XMLConfigBuilder(reader);
    // 调用XMLConfigBuilder.parse()方法,解析XML创建Configuration对象
    Configuration conf = builder.parse();
}
//会生成configuration类
private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {
   
    super(new Configuration());
    ErrorContext.instance().resource("SQL Mapper Configuration");
    this.configuration.setVariables(props);
    this.parsed = false;
    this.environment = environment;
    this.parser = parser;
}

//解析Config Resource
public Configuration parse() {
   
    // 防止parse()方法被同一个实例多次调用
    if (parsed) {
   //一个实例创建一次
      throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    parsed = true;
    // 调用XPathParser.evalNode()方法,创建表示configuration节点的XNode对象。
    // 调用parseConfiguration()方法对XNode进行处理
    parseConfiguration(parser.evalNode("/configuration"));
    return configuration;
}
//解析 configuration节点
private void parseConfiguration(XNode root) {
   
    try {
   
      //configuration标签的子节点,每一个都有单独方法解析
      propertiesElement(root.evalNode("properties"));
      Properties settings = settingsAsProperties(root.evalNode("settings"));//可以指定日志实现或者默认的Executor
      loadCustomVfs(settings);
      typeAliasesElement(root.evalNode("typeAliases"));
      pluginElement(root.evalNode("plugins"));
      objectFactoryElement(root.evalNode("objectFactory"));
      objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
      reflectorFactoryElement(root.evalNode("reflectorFactory"));
      settingsElement(settings);
      // read it after objectFactory and objectWrapperFactory issue #631
      environmentsElement(root.evalNode("environments"));//配置数据库连接环境,可配置多个
      databaseIdProviderElement(root.evalNode("databaseIdProvider"));//配置数据库厂商信息
      typeHandlerElement(root.evalNode("typeHandlers"));//配置typeHandler
      mapperElement(root.evalNode("mappers"));//解析mapper资源
    } catch (Exception e) {
   
      throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
  }
//解析mappers节点
private void mapperElement(XNode parent) throws Exception {
   
    if (parent != null) {
   
      for (XNode child : parent.getChildren()) {
   
        // 通过<package>标签指定包名
        if ("package".equals(child.getName())) {
   
          String mapperPackage = child.getStringAttribute("name");
          configuration.addMappers(mapperPackage);//扫描整个包的mapper
        } else {
   
          String resource = child.getStringAttribute("resource");
          String url = child.getStringAttribute("url");
          String mapperClass = child.getStringAttribute("class");
          // 通过resource属性指定XML文件路径
          if (resource != null && url == null && mapperClass == null) {
   
            ErrorContext.instance().resource(resource);
            InputStream inputStream = Resources.getResourceAsStream(resource);
            XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
            mapperParser.parse();
          } else if (resource == null && url != null && mapperClass == null) {
   
            // 通过url属性指定XML文件路径
            ErrorContext.instance().resource(url);
            InputStream inputStream = Resources.getUrlAsStream(url);
            XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
            mapperParser.parse();
          } else if (resource == null && url == null && mapperClass != null) {
   
            // 通过class属性指定接口的完全限定名 通过java配置的sql解析
            Class<?> mapperInterface = Resources.classForName(mapperClass);
            configuration.addMapper(mapperInterface);
          } else {
   
            throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
          }
        }
      }
    }
}

//解析xml文件定义的 如UserMapper.xml,在此方法的bindMapperForNamespace()生成代理工厂
public void parse() {
   
    if (!configuration.isResourceLoaded(resource)) {
   
        // 调用XPathParser的evalNode()方法获取根节点对应的XNode对象
        configurationElement(parser.evalNode("/mapper"));
        // 將资源路径添加到Configuration对象中
        configuration.addLoadedResource(resource);//资源路径如:<mapper resource="com/blog4java/mybatis/example/mapper/UserMapper.xml"/>
        bindMapperForNamespace();
    }
    // 继续解析之前解析出现异常的ResultMap对象
    parsePendingResultMaps();
    // 继续解析之前解析出现异常的CacheRef对象
    parsePendingCacheRefs();
    // 继续解析之前解析出现异常<select|update|delete|insert>标签配置
    parsePendingStatements();
}
//解析mapper节点 
private void configurationElement(XNode context) {
   
    try {
   
        // 获取命名空间
        String namespace = context.getStringAttribute("namespace");
        if (namespace == null || namespace.equals("")) {
   
            throw new BuilderException("Mapper's namespace cannot be empty");
        }
        // 设置当前正在解析的Mapper配置的命名空间
        builderAssistant.setCurrentNamespace(namespace);
        // 解析<cache-ref>标签
        cacheRefElement(context.evalNode("cache-ref"));
        // 解析<cache>标签
        cacheElement(context.evalNode("cache"));//一个mapper 一个二级cache实例
        // 解析所有的<parameterMap>标签
        parameterMapElement(context.evalNodes("/mapper/parameterMap"));
        // 解析所有的<resultMap>标签
        resultMapElements(context.evalNodes("/mapper/resultMap"));
        // 解析所有的<sql>标签
        sqlElement(context.evalNodes("/mapper/sql"));
        // 解析所有的<select|insert|update|delete>标签
        //创建MappedStatement,一条sql对应一个MappedStatement,这些MappedStatement共享二级cache实例
        buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
    } catch (Exception e) {
   
        throw new BuilderException("Error parsing Mapper XML. The XML location is '" + resource + "'. Cause: " + e, e);
    }
}
//解析mapper节点下的方法集合
private void buildStatementFromContext(List<XNode> list) {
   
    if (configuration.getDatabaseId() != null) {
   
        buildStatementFromContext(list, configuration.getDatabaseId());
    }
    buildStatementFromContext(list, null);
}
//解析mapper节点下的方法集合
private void buildStatementFromContext(List<XNode> list, String requiredDatabaseId) {
   
    for (XNode context : list) {
   
        // 通过XMLStatementBuilder对象,对<select|update|insert|delete>标签进行解析
        final XMLStatementBuilder statementParser = new XMLStatementBuilder(configuration, builderAssistant, context, requiredDatabaseId);
        try {
   
            // 调用parseStatementNode()方法解析
            statementParser.parseStatementNode();
        } catch (IncompleteElementException e) {
   
            configuration.addIncompleteStatement(statementParser);
        }
    }
}
//解析mapper下的单个方法
public void parseStatementNode() {
   
    System.out.println("解析mapperxml中的sql");
    String id = context.getStringAttribute("id");
    String databaseId = context.getStringAttribute("databaseId");

    if (!databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) {
   
        return;
    }
    // 解析<select|update|delete|insert>标签属性
    Integer fetchSize = context.getIntAttribute("fetchSize");
    Integer timeout = context.getIntAttribute("timeout");
    String parameterMap = context.getStringAttribute("parameterMap");
    String parameterType = context.getStringAttribute("parameterType");
    Class<?> parameterTypeClass = resolveClass(parameterType);
    String resultMap = context.getStringAttribute("resultMap");
    String resultType = context.getStringAttribute("resultType");
    // 获取LanguageDriver对象
    String lang = context.getStringAttribute("lang");
    LanguageDriver langDriver = getLanguageDriver(lang);
    // 获取Mapper返回结果类型Class对象
    Class<?> resultTypeClass = resolveClass(resultType);
    String resultSetType = context.getStringAttribute("resultSetType");
    // 默认Statement类型为PREPARED
    StatementType statementType = StatementType.valueOf(context.getStringAttribute("statementType",
                                                                                   StatementType.PREPARED.toString()));
    ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType);

    String nodeName = context.getNode().getNodeName();
    SqlCommandType sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase(Locale.ENGLISH));
    boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
    boolean flushCache = context.getBooleanAttribute("flushCache", !isSelect);
    boolean useCache = context.getBooleanAttribute("useCache", isSelect);
    boolean resultOrdered = context.getBooleanAttribute("resultOrdered", false);

    // 將<include>标签内容,替换为<sql>标签定义的SQL片段
    XMLIncludeTransformer includeParser = new XMLIncludeTransformer(configuration, builderAssistant);
    includeParser.applyIncludes(context.getNode());

    // 解析<selectKey>标签
    processSelectKeyNodes(id, parameterTypeClass, langDriver);

    // 通过LanguageDriver解析SQL内容,生成SqlSource对象
    SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass);
    String resultSets = context.getStringAttribute("resultSets");
    String keyProperty = context.getStringAttribute("keyProperty");
    String keyColumn = context.getStringAttribute("keyColumn");
    KeyGenerator keyGenerator;
    String keyStatementId = id + SelectKeyGenerator.SELECT_KEY_SUFFIX;
    keyStatementId = builderAssistant.applyCurrentNamespace(keyStatementId, true);
    // 获取主键生成策略
    if (configuration.hasKeyGenerator(keyStatementId)) {
   
        keyGenerator = configuration.getKeyGenerator(keyStatementId);
    } else {
   
        keyGenerator = context.getBooleanAttribute("useGeneratedKeys",
                                                   configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType))
            ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
    }
    //解析好以后生成MappedStatement
    builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType,
                                        fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass,
                                        resultSetTypeEnum, flushCache, useCache, resultOrdered, 
                                        keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets);
}

//解析方法完成以后,生成MappedStatement
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) {
   

    if (unresolvedCacheRef) {
   
        throw new IncompleteElementException("Cache-ref not yet resolved");
    }

    id = applyCurrentNamespace(id, false);
    boolean isSelect = sqlCommandType == SqlCommandType.SELECT;

    MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, id, sqlSource, sqlCommandType)
        .resource(resource)
        .fetchSize(fetchSize)
        .timeout(timeout)
        .statementType(statementType)
        .keyGenerator(keyGenerator)
        .keyProperty(keyProperty)
        .keyColumn(keyColumn)
        .databaseId(databaseId)
        .lang(lang)
        .resultOrdered(resultOrdered)
        .resultSets(resultSets)
        .resultMaps(getStatementResultMaps(resultMap, resultType, id))
        .resultSetType(resultSetType)
        .flushCacheRequired(valueOrDefault(flushCache, !isSelect))
        .useCache(valueOrDefault(useCache, isSelect))
        .cache(currentCache);//关联解析cache标签时生成的二级缓存

    ParameterMap statementParameterMap = getStatementParameterMap(parameterMap, parameterType, id);
    if (statementParameterMap != null) {
   
        statementBuilder.parameterMap(statementParameterMap);
    }

    MappedStatement statement = statementBuilder.build();
    configuration.addMappedStatement(statement);//注册到configuration
    return statement;
}
//将MappedStatement注册到Configuration
 public void addMappedStatement(MappedStatement ms) {
   
    mappedStatements.put(ms.getId(), ms);
  }


mybatis在应用启动加载config.xml生成MappedStatement,并对每一个mapper接口都生成了代理工厂,代理工厂的生成在xml方式是在

//解析xml文件定义的 如UserMapper.xml,在此方法的bindMapperForNamespace()生成代理工厂
public void parse() {
   
    if (!configuration.isResourceLoaded(resource)) {
   
        // 调用XPathParser的evalNode()方法获取根节点对应的XNode对象
        configurationElement(parser.evalNode("/mapper"));
        // 將资源路径添加到Configuration对象中
        configuration.addLoadedResource(resource);//资源路径如:<mapper resource="com/blog4java/mybatis/example/mapper/UserMapper.xml"/>
        bindMapperForNamespace();
    }
    // 继续解析之前解析出现异常的ResultMap对象
    parsePendingResultMaps();
    // 继续解析之前解析出现异常的CacheRef对象
    parsePendingCacheRefs();
    // 继续解析之前解析出现异常<select|update|delete|insert>标签配置
    parsePendingStatements();
}

private void bindMapperForNamespace() {
   
    String namespace = builderAssistant.getCurrentNamespace();//namespace何时设置?解析mapper节点时
    if (namespace != null) {
   
        Class<?> boundType = null;
        try {
   
            boundType = Resources.classForName(namespace);
        } catch (ClassNotFoundException e) {
   
            //ignore, bound type is not required
        }
        if (boundType != null) {
   
            if (!configuration.hasMapper(boundType)) {
   
                // Spring may not know the real resource name so we set a flag
                // to prevent loading again this resource from the mapper interface
                // look at MapperAnnotationBuilder#loadXmlResource
                configuration.addLoadedResource("namespace:" + namespace);
                configuration.addMapper(boundType);
            }
        }
    }
}
//最终到configuration的MapperRegistry对象
public class MapperRegistry {
   
  // Configuration对象引用
  private final Configuration config;
  // 用于注册Mapper接口Class对象,和MapperProxyFactory对象对应关系 MapperProxyFactory用于创建Mapper动态对象
  private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>, MapperProxyFactory<?>>();
}

public <T> void addMapper(Class<T> type) {
   
    if (type.isInterface()) {
   
        if (hasMapper(type)) {
   
            throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
        }
        boolean loadCompleted = false;
        try {
   
            //对当前mapper接口,创建代理工厂
            knownMappers.put(type, new MapperProxyFactory<T>(type));
            // It's important that the type is added before the parser is run
            // otherwise the binding may automatically be attempted by the
            // mapper parser. If the type is already known, it won't try.
            MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
            parser.parse();
            loadCompleted = true;
        } finally {
   
            if (!loadCompleted) {
   
                knownMappers.remove(type);
            }
        }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半岛铁盒_7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值