首先是mybatis对 xml文件 进行解析,那么这个解析的关键配置就是php
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations"> <value>classpath*:com/zzy/xml/*Mapper.xml</value> </property> </bean>
以上配置org.mybatis.spring.SqlSessionFactoryBean这个类是解析xml文件的核心类,这个类中有不少属性,也就是property,其中mapperLocations对xml文件扫描的属性类引用,其余属性在源码中进一步分析!spring
若是您使用的是eclipse,那么Ctrl+鼠标左键【提早是你已经下载了mybatis相关源码】进入SqlSessionFactoryBean,以下图:sql
这个类实现了InitializingBean接口mybatis
题外话:app
这个核心类在mybatis那个jar中呢,以下图:eclipse
能够看到在mybatis-spring这个jar的最外层!ide
这个类后期还会在spring源码分析中细讲,这里只说明这个类的做用。源码分析
做用:spring中bean实例化和依赖注入完成之后,作一些后续的初始化工做,实现IntializingBean这个接口,必须重写其中的afterPropertiesSet这个方法!以下图简单看看这个接口的内容:
ui
回到spring的源码分析上,上文说了SqlSessionFactoryBean实现了InitializingBean的afterPropertiesSet,咱们在SqlSessionFactoryBean中找下这个方法,Ctrl+O:this
点击调到该方法
其中咱们须要关注的代码段是:
this.sqlSessionFactory = buildSqlSessionFactory(); notNull(dataSource, "Property 'dataSource' is required"); notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required"); state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null), "Property 'configuration' and 'configLocation' can not specified with together");
这些都是对必须配置属性的异常处理。
其中buildSqlSessionFactory()这个方法是咱们研究的核心!
SqlSessionFactoryBean属性也就是
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations"> <value>classpath*:com/zzy/xml/*Mapper.xml</value> </property> </bean>
下可配置的property 有哪些呢?以下源码(其中,mapperLocations和 dataSource常常用到的,其余有些事ibatis原有,为了保持兼容性,不少基本不用了),下节对经常使用属性作简单介绍
private Resource configLocation; private Configuration configuration; private Resource[] mapperLocations; private DataSource dataSource; private TransactionFactory transactionFactory; private Properties configurationProperties; private SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); private SqlSessionFactory sqlSessionFactory; //EnvironmentAware requires spring 3.1 private String environment = SqlSessionFactoryBean.class.getSimpleName(); private boolean failFast; private Interceptor[] plugins; private TypeHandler<?>[] typeHandlers; private String typeHandlersPackage; private Class<?>[] typeAliases; private String typeAliasesPackage; private Class<?> typeAliasesSuperType; //issue #19. No default provider. private DatabaseIdProvider databaseIdProvider; private Class<? extends VFS> vfs; private Cache cache; private ObjectFactory objectFactory; private ObjectWrapperFactory objectWrapperFactory;
以上配置的属性最终都会被3点buildSqlSessionFactory()方法读取到
,而后对相关属性的配置处理!截图简单看看,怎么处理这些属性呢?举例看下mapperLocations和 dataSource
以上截图来自buildSqlSessionFactory这个方法,下节源码分析该方法以及相关配置属性的说明!