mybatis启动加载过程

1.spring 配置文件


		
		
		



		
		

2.启动加载

SqlSessionFactoryBean 实现 InitializingBean 接口,bean 加载后会执行 afterPropertiesSet()方法。

public class SqlSessionFactoryBean implements FactoryBean, InitializingBean, ApplicationListener {


/**
* 
*/
public void afterPropertiesSet() throws Exception {
        Assert.notNull(this.dataSource, "Property 'dataSource' is required");
        Assert.notNull(this.sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");
        this.sqlSessionFactory = this.buildSqlSessionFactory();
}


protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
        XMLConfigBuilder xmlConfigBuilder = null;
        
        return this.sqlSessionFactoryBuilder.build(configuration);
    }

// 解析 mapper.xml 文件
if (!ObjectUtils.isEmpty(this.mapperLocations)) {
            Resource[] arr$ = this.mapperLocations;
            len$ = arr$.length;

            for(i$ = 0; i$ < len$; ++i$) {
                Resource mapperLocation = arr$[i$];
                if (mapperLocation != null) {
                    try {
                        XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(), configuration, mapperLocation.toString(), configuration.getSqlFragments());

                        xmlMapperBuilder.parse();
                    } catch (Exception var20) {
                        throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", var20);
                    } finally {
                        ErrorContext.instance().reset();
                    }

                    if (this.logger.isDebugEnabled()) {
                        this.logger.debug("Parsed mapper file: '" + mapperLocation + "'");
                    }
                }
            }
        } else if (this.logger.isDebugEnabled()) {
            this.logger.debug("Property 'mapperLocations' was not specified or no matching resources found");
        }

}
public void parse() {
        if (!this.configuration.isResourceLoaded(this.resource)) {

            //解析mapper.xml元素
            this.configurationElement(this.parser.evalNode("/mapper"));
            this.configuration.addLoadedResource(this.resource);
            this.bindMapperForNamespace();
        }

        this.parsePendingResultMaps();
        this.parsePendingChacheRefs();
        this.parsePendingStatements();
 }


private void configurationElement(XNode context) {
        try {
            String namespace = context.getStringAttribute("namespace");
            if (namespace.equals("")) {
                throw new BuilderException("Mapper's namespace cannot be empty");
            } else {
                this.builderAssistant.setCurrentNamespace(namespace);
                this.cacheRefElement(context.evalNode("cache-ref"));
                this.cacheElement(context.evalNode("cache"));
                //解析
               this.parameterMapElement(context.evalNodes("/mapper/parameterMap"));
                //解析,将resultMap 放到Configuration类的 
                //resultMaps 中 id = mapper类全路径 + ResultMap id属性
                //protected final Map resultMaps;
                this.resultMapElements(context.evalNodes("/mapper/resultMap"));
                this.sqlElement(context.evalNodes("/mapper/sql"));
                this.buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
            }
        } catch (Exception var3) {
            throw new BuilderException("Error parsing Mapper XML. Cause: " + var3, var3);
        }
    }

 

你可能感兴趣的:(mybatis启动加载过程)