本文主要说明SqlSessionFactory的构建过程
mysql配置文件如下:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:/conf/mybatis-config.xml"/> <property name="mapperLocations" value="classpath:mappers/*.xml"/> <property name="typeAliasesPackage" value="me.ele.vas.cpt.server.model"/> </bean>
在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); }