Mybatis解析——SqlSessionFactoryBean

本文主要说明SqlSessionFactory的构建过程

mysql配置文件如下:

id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    name="dataSource" ref="dataSource"/>
    name="configLocation" value="classpath:/conf/mybatis-config.xml"/>
    name="mapperLocations"
              value="classpath:mappers/*.xml"/>
    name="typeAliasesPackage" value="me.ele.vas.cpt.server.model"/>

在SqlSessionFactoryBean里面通过buildSqlSessionFactory()进行构建SqlSessionFactory。

首先通过XmlConfigBuilder加载mybatis的config信息。从而创建Configuration对象。(具体加载流程下篇说明)

xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), (String)null, this.configurationProperties);
configuration = xmlConfigBuilder.getConfiguration();

SqlSessionFactory是通过工具类SqlSessionFactoryBuilder来创建的

return this.sqlSessionFactoryBuilder.build(configuration);

最终创建DefaultSqlSessionFactory实例

public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
}

你可能感兴趣的:(Mybatis)