Mybatis SqlSessionFactory 初始化原理

點(diǎn)擊上方「Java有貨」關(guān)注我們

+
每個基于 MyBatis 的應(yīng)用都是以一個 SqlSessionFactory 的實(shí)例為核心的。SqlSessionFactory 的實(shí)例可以通過 SqlSessionFactoryBuilder 獲得。而 SqlSessionFactoryBuilder 則可以從 XML 配置文件或一個預(yù)先配置的 Configuration 實(shí)例來構(gòu)建出 SqlSessionFactory 實(shí)例。
從 XML 文件中構(gòu)建 SqlSessionFactory 的實(shí)例非常簡單,建議使用類路徑下的資源文件進(jìn)行配置。但也可以使用任意的輸入流(InputStream)實(shí)例,比如用文件路徑字符串或 file:// URL 構(gòu)造的輸入流。MyBatis 包含一個名叫 Resources 的工具類,它包含一些實(shí)用方法,使得從類路徑或其它位置加載資源文件更加容易。
String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
XML 配置文件中包含了對 MyBatis 系統(tǒng)的核心設(shè)置,包括獲取數(shù)據(jù)庫連接實(shí)例的數(shù)據(jù)源(DataSource)以及決定事務(wù)作用域和控制方式的事務(wù)管理器(TransactionManager)。后面會再探討 XML 配置文件的詳細(xì)內(nèi)容,這里先給出一個簡單的示例:
PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><mappers><mapper resource="org/mybatis/example/BlogMapper.xml"/></mappers></configuration>
當(dāng)然,還有很多可以在 XML 文件中配置的選項(xiàng),上面的示例僅羅列了最關(guān)鍵的部分。注意 XML 頭部的聲明,它用來驗(yàn)證 XML 文檔的正確性。environment 元素體中包含了事務(wù)管理和連接池的配置。mappers 元素則包含了一組映射器(mapper),這些映射器的 XML 映射文件包含了 SQL 代碼和映射定義信息。
不使用 XML 構(gòu)建 SqlSessionFactory
如果你更愿意直接從 Java 代碼而不是 XML 文件中創(chuàng)建配置,或者想要創(chuàng)建你自己的配置建造器,MyBatis 也提供了完整的配置類,提供了所有與 XML 文件等價的配置項(xiàng)。
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();TransactionFactory transactionFactory = new JdbcTransactionFactory();Environment environment = new Environment("development", transactionFactory, dataSource);Configuration configuration = new Configuration(environment);configuration.addMapper(BlogMapper.class);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
注意該例中,configuration 添加了一個映射器類(mapper class)。映射器類是 Java 類,它們包含 SQL 映射注解從而避免依賴 XML 文件。不過,由于 Java 注解的一些限制以及某些 MyBatis 映射的復(fù)雜性,要使用大多數(shù)高級映射(比如:嵌套聯(lián)合映射),仍然需要使用 XML 配置。有鑒于此,如果存在一個同名 XML 配置文件,MyBatis 會自動查找并加載它(在這個例子中,基于類路徑和 BlogMapper.class 的類名,會加載 BlogMapper.xml)。具體細(xì)節(jié)稍后討論。
SqlSessionFactoryBuilder
String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
build 方法:
// 1.我們最初調(diào)用的buildpublic SqlSessionFactory build(InputStream inputStream) {//調(diào)用了重載方法return build(inputStream, null, null);}// 2.調(diào)用的重載方法public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {try {// 創(chuàng)建 XMLConfigBuilder, XMLConfigBuilder是專門解析mybatis的配置文件的類XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);// 執(zhí)行 XML 解析// 創(chuàng)建 DefaultSqlSessionFactory 對象return build(parser.parse());} catch (Exception e) {//···}}
parser.parse()
public Configuration parse() {if (parsed) {throw new BuilderException("Each XMLConfigBuilder can only be used once.");}// 標(biāo)記已解析parsed = true;// parser.evalNode("/configuration"),// 通過xpath 讀取配置文件的節(jié)點(diǎn),將讀取出配置文件的所以節(jié)點(diǎn)//<configuration>// <environments default="development">// </environments>//<configuration>parseConfiguration(parser.evalNode("/configuration"));return configuration;}
parseConfiguration(XNode root)
// 解析每個節(jié)點(diǎn) 這里每個方法進(jìn)去都會有很多配置,這里就不一一解析,大家感興趣可以看看,// settingsElement(settings);mapperElement(root.evalNode("mappers"));private void parseConfiguration(XNode root) {try {//issue #117 read properties first// 解析 <properties /> 標(biāo)簽propertiesElement(root.evalNode("properties"));// 解析 <settings /> 標(biāo)簽Properties settings = settingsAsProperties(root.evalNode("settings"));// 加載自定義的 VFS 實(shí)現(xiàn)類loadCustomVfs(settings);// 解析 <typeAliases /> 標(biāo)簽typeAliasesElement(root.evalNode("typeAliases"));// 解析 <plugins /> 標(biāo)簽pluginElement(root.evalNode("plugins"));// 解析 <objectFactory /> 標(biāo)簽objectFactoryElement(root.evalNode("objectFactory"));// 解析 <objectWrapperFactory /> 標(biāo)簽objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));// 解析 <reflectorFactory /> 標(biāo)簽reflectorFactoryElement(root.evalNode("reflectorFactory"));// 賦值 <settings /> 到 Configuration 屬性settingsElement(settings);// read it after objectFactory and objectWrapperFactory issue #631// 解析 <environments /> 標(biāo)簽environmentsElement(root.evalNode("environments"));// 解析 <databaseIdProvider /> 標(biāo)簽databaseIdProviderElement(root.evalNode("databaseIdProvider"));// 解析 <typeHandlers /> 標(biāo)簽typeHandlerElement(root.evalNode("typeHandlers"));// 解析 <mappers /> 標(biāo)簽mapperElement(root.evalNode("mappers"));} catch (Exception e) {throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);}}// 獲取mapperprivate void mapperElement(XNode parent) throws Exception {if (parent != null) {for (XNode child : parent.getChildren()) {// 如果是 包將在這里進(jìn)行渲染if ("package".equals(child.getName())) {String mapperPackage = child.getStringAttribute("name");configuration.addMappers(mapperPackage);} else {// 讀取resource 標(biāo)簽String resource = child.getStringAttribute("resource");// 讀取url 標(biāo)簽String url = child.getStringAttribute("url");// 讀取注解String mapperClass = child.getStringAttribute("class");// 根據(jù)不同的方式完成if (resource != null && url == null && mapperClass == null) {ErrorContext.instance().resource(resource);InputStream inputStream = Resources.getResourceAsStream(resource);XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());mapperParser.parse();} else if (resource == null && url != null && mapperClass == null) {ErrorContext.instance().resource(url);InputStream inputStream = Resources.getUrlAsStream(url);XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());mapperParser.parse();} else if (resource == null && url == null && mapperClass != null) {Class<?> mapperInterface = Resources.classForName(mapperClass);configuration.addMapper(mapperInterface);} else {throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");}}}}}private void settingsElement(Properties props) {configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));configuration.setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.valueOf(props.getProperty("autoMappingUnknownColumnBehavior", "NONE")));configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));configuration.setProxyFactory((ProxyFactory) createInstance(props.getProperty("proxyFactory")));.....configuration.setShrinkWhitespacesInSql(booleanValueOf(props.getProperty("shrinkWhitespacesInSql"), false));}
mapperParser.parse()
// 這里我們先看一下 mapperParser.parse();方法 懂得原理,都是類似的public void parse() {if (!configuration.isResourceLoaded(resource)) {// 加載 mapper所有子節(jié)點(diǎn)configurationElement(parser.evalNode("/mapper"));configuration.addLoadedResource(resource);// 綁定 NamespacebindMapperForNamespace();}// 構(gòu)建ResultMapparsePendingResultMaps();parsePendingCacheRefs();parsePendingStatements();}// 這里將解析整個 xml文件private void configurationElement(XNode context) {try {String namespace = context.getStringAttribute("namespace");if (namespace == null || namespace.isEmpty()) {throw new BuilderException("Mapper's namespace cannot be empty");}builderAssistant.setCurrentNamespace(namespace);cacheRefElement(context.evalNode("cache-ref"));cacheElement(context.evalNode("cache"));parameterMapElement(context.evalNodes("/mapper/parameterMap"));resultMapElements(context.evalNodes("/mapper/resultMap"));sqlElement(context.evalNodes("/mapper/sql"));// 解析標(biāo)簽,buildStatementFromContext(context.evalNodes("select|insert|update|delete"));} catch (Exception e) {throw new BuilderException("Error parsing Mapper XML. The XML location is '" + resource + "'. Cause: " + e, e);}}// 關(guān)于注解的方式的parsepublic void parse() {String resource = type.toString();if (!configuration.isResourceLoaded(resource)) {loadXmlResource();configuration.addLoadedResource(resource);assistant.setCurrentNamespace(type.getName());parseCache();parseCacheRef();for (Method method : type.getMethods()) {if (!canHaveStatement(method)) {continue;}if (getAnnotationWrapper(method, false, Select.class, SelectProvider.class).isPresent()&& method.getAnnotation(ResultMap.class) == null) {parseResultMap(method);}try {parseStatement(method);} catch (IncompleteElementException e) {configuration.addIncompleteMethod(new MethodResolver(this, method));}}}parsePendingMethods();}
到此Mybatis的初始化工作就完畢了,主要做了兩件大事
解析核心配置文件到
Configuration對象,解析映射配置文件到MappedStatement對象,并保存在Configuration的對應(yīng)Map中創(chuàng)建了
DefaultSqlSessionFactory返回
通過上面的代碼分析,總結(jié)了一下使用的重要的類,通過下圖的裝配,最終返回SqlSessionFactory,而SqlSessionFactory的最終實(shí)現(xiàn)是 DefaultSqlSessionFactory,關(guān)于DefaultSqlSessionFactory的介紹我們將放在下篇文章進(jìn)行講解,感興趣的小伙伴可以持續(xù)關(guān)注!

拓展
看到這里很多人就會有個疑問,這是通過配置文件的方式在進(jìn)行配置,但是SpringBoot 沒有這樣的配置文件,是怎么做到的呢?其實(shí)SpringBoot是通過自定配置完成;
@Configuration// 實(shí)例化 SqlSessionFactory@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})@ConditionalOnSingleCandidate(DataSource.class)// MybatisProperties 我們常用的配置@EnableConfigurationProperties({MybatisProperties.class})@AutoConfigureAfter({DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class})public class MybatisAutoConfiguration implements InitializingBean {}
如果大家感興趣,可以看看SqlSessionFactoryBean 代碼,很多地方還是復(fù)用我們剛剛看到的代碼的!
