MyBatis整合Spring的实现(18)

例子

同一个命名空间,但是有2个配置文件,A配置文件依赖于B配置文件信息,但是在加载时,先加载B配置文件的话,那么就会报错,前面章节已经捕获了此异常并把相应的配置添加到Configuration(全局配置类)中,这里就是在加载A配置文件后,再次加载B配置文件,也就把B配置信息添加到Configuration(全局配置类)里,下面就来看一下代码吧。

1 方法parsePendingResultMaps

1private void parsePendingResultMaps() { 2    Collection<ResultMapResolver> incompleteResultMaps = configuration.getIncompleteResultMaps(); 3    synchronized (incompleteResultMaps) { 4      Iterator<ResultMapResolver> iter = incompleteResultMaps.iterator(); 5      while (iter.hasNext()) { 6        try { 7          iter.next().resolve(); 8          iter.remove(); 9        } catch (IncompleteElementException e) { 10          // ResultMap is still missing a resource... 11        } 12      } 13    } 14}

2 方法parsePendingChacheRefs

1private void parsePendingChacheRefs() { 2  Collection<CacheRefResolver> incompleteCacheRefs = configuration.getIncompleteCacheRefs(); 3  synchronized (incompleteCacheRefs) { 4      Iterator<CacheRefResolver> iter = incompleteCacheRefs.iterator(); 5      while (iter.hasNext()) { 6          try { 7              iter.next().resolveCacheRef(); 8              iter.remove(); 9          } catch (IncompleteElementException e) { 10              // Cache ref is still missing a resource... 11          } 12      } 13  } 14}

3 方法parsePendingStatements

1private void parsePendingStatements() { 2  Collection<XMLStatementBuilder> incompleteStatements = configuration.getIncompleteStatements(); 3  synchronized (incompleteStatements) { 4      Iterator<XMLStatementBuilder> iter = incompleteStatements.iterator(); 5      while (iter.hasNext()) { 6          try { 7              iter.next().parseStatementNode(); 8              iter.remove(); 9          } catch (IncompleteElementException e) { 10              // Statement is still missing a resource... 11          } 12      } 13  } 14}

****以上3个方法就是因为加载顺序错误的重复处理逻辑,加载的代码与前面一样,这里不再分析。


总结

以上文章就是MyBatis对配置文件的初始化,这里至少分析了60%,对理解MyBatis已经够了。MyBatis也就剩最后一步了,建立SqlSessionFactory工厂,这个工厂也是唯一的,MyBatis默认使用的是org.apache.ibatis.session.defaults.DefaultSqlSessionFactory类。这里以后在分析。

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Element 'configuration' cannot have character [children], because the type's content type is element-only.

整合springbootmybatis时copy网上的配置,然后报错了。<configuration       <!mybatis用于生成代码的配置文件  <configurationFilesrc/main/resources/gener

Dubbo 扩展点加载机制:从 Java SPI 到 Dubbo SPI

!(https://oscimg.oschina.net/oscnet/up1aa4ada0efc8a144d35d25b3443d951c7e3.JPEG)SPI全称为ServiceProviderInterface,是一种服务发现机制。当程序运行调用接口时,会根据配置文件或默认规则信息加载对应的实现类。所以在程序中并没有直接指定使用接口

Spring boot 配置文件,配置注解详解 (properties 和yml )

从其他框架来看我们都有自己的配置文件,hibernate有hbm,mybatis有properties,同样,Springboot也有全局配置文件。Springboot使用一个全局的配置文件,而且配置文件的名字是固定的。有两种application.propertiesapplication.yml spring

SpringMVC与SpringBoot配置文件的加载区别

一、SpringMVC:配置文件在classpath下。在web.xml中配置加载。以下项目为示例其中引用关系为1. applicationContextdao.xml引用了mybatis文件夹中的配置文件2. applicationContextshiro.xml引用了shiro文件夹中的配置文件3. sprin

Spring整合activiti配置processEngine

配置xml数据时,可以直接在配置文件中填写,也可以采用properties配置文件的方式加载。采用配置文件的方式需要使用到${参数}的方式获取。引用配置文件的方式:<context:propertyplaceholderlocation"classpath:properties文件目录"/applic