引言
現(xiàn)在內(nèi)卷越來越嚴(yán)重,關(guān)于常用的ORM框架Mybatis,小編準(zhǔn)備了三篇文章,分別將介紹SqlSessionFactory初始化原理、SqlSession執(zhí)行流程,Mybatis代理模式運(yùn)行方式與最終總結(jié),這是第一篇,感興趣的朋友可以持續(xù)關(guān)注。
SqlSessionFactory
每個(gè)基于 MyBatis 的應(yīng)用都是以一個(gè) SqlSessionFactory 的實(shí)例為核心的。SqlSessionFactory 的實(shí)例可以通過 SqlSessionFactoryBuilder 獲得。而 SqlSessionFactoryBuilder 則可以從 XML 配置文件或一個(gè)預(yù)先配置的 Configuration 實(shí)例來構(gòu)建出 SqlSessionFactory 實(shí)例。
從 XML 文件中構(gòu)建 SqlSessionFactory 的實(shí)例非常簡(jiǎn)單,建議使用類路徑下的資源文件進(jìn)行配置。 但也可以使用任意的輸入流(InputStream)實(shí)例,比如用文件路徑字符串或 file:// URL 構(gòu)造的輸入流。MyBatis 包含一個(gè)名叫 Resources 的工具類,它包含一些實(shí)用方法,使得從類路徑或其它位置加載資源文件更加容易。
1
2
3
|
String resource = "org/mybatis/example/mybatis-config.xml" ; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); |
XML 配置文件中包含了對(duì) MyBatis 系統(tǒng)的核心設(shè)置,包括獲取數(shù)據(jù)庫連接實(shí)例的數(shù)據(jù)源(DataSource)以及決定事務(wù)作用域和控制方式的事務(wù)管理器(TransactionManager)。后面會(huì)再探討 XML 配置文件的詳細(xì)內(nèi)容,這里先給出一個(gè)簡(jiǎn)單的示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE configuration 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)建配置,或者想要?jiǎng)?chuàng)建你自己的配置建造器,MyBatis 也提供了完整的配置類,提供了所有與 XML 文件等價(jià)的配置項(xiàng)。
1
2
3
4
5
6
|
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 添加了一個(gè)映射器類(mapper class)。映射器類是 Java 類,它們包含 SQL 映射注解從而避免依賴 XML 文件。不過,由于 Java 注解的一些限制以及某些 MyBatis 映射的復(fù)雜性,要使用大多數(shù)高級(jí)映射(比如:嵌套聯(lián)合映射),仍然需要使用 XML 配置。有鑒于此,如果存在一個(gè)同名 XML 配置文件,MyBatis 會(huì)自動(dòng)查找并加載它(在這個(gè)例子中,基于類路徑和 BlogMapper.class 的類名,會(huì)加載 BlogMapper.xml)。具體細(xì)節(jié)稍后討論。
SqlSessionFactoryBuilder
1
2
3
|
String resource = "mybatis-config.xml" ; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); |
build 方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// 1.我們最初調(diào)用的build public 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 對(duì)象 return build(parser.parse()); } catch (Exception e) { //··· } } |
parser.parse()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
// 解析每個(gè)節(jié)點(diǎn) 這里每個(gè)方法進(jìn)去都會(huì)有很多配置,這里就不一一解析,大家感興趣可以看看, // 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); } } // 獲取mapper private 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();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
// 這里我們先看一下 mapperParser.parse();方法 懂得原理,都是類似的 public void parse() { if (!configuration.isResourceLoaded(resource)) { // 加載 mapper所有子節(jié)點(diǎn) configurationElement(parser.evalNode( "/mapper" )); configuration.addLoadedResource(resource); // 綁定 Namespace bindMapperForNamespace(); } // 構(gòu)建ResultMap parsePendingResultMaps(); parsePendingCacheRefs(); parsePendingStatements(); } // 這里將解析整個(gè) 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)于注解的方式的parse public 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對(duì)象,解析映射配置文件到MappedStatement對(duì)象,并保存在Configuration的對(duì)應(yīng)Map中
- 創(chuàng)建了DefaultSqlSessionFactory返回
通過上面的代碼分析,總結(jié)了一下使用的重要的類,通過下圖的裝配,最終返回SqlSessionFactory,而SqlSessionFactory的最終實(shí)現(xiàn)是 DefaultSqlSessionFactory,關(guān)于DefaultSqlSessionFactory的介紹我們將放在下篇文章進(jìn)行講解,感興趣的小伙伴可以持續(xù)關(guān)注!
拓展
看到這里很多人就會(huì)有個(gè)疑問,這是通過配置文件的方式在進(jìn)行配置,但是SpringBoot 沒有這樣的配置文件,是怎么做到的呢?其實(shí)SpringBoot是通過自定配置完成;
1
2
3
4
5
6
7
8
|
@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 {} |
到此這篇關(guān)于解析Mybatis SqlSessionFactory初始化原理的文章就介紹到這了,更多相關(guān)Mybatis SqlSessionFactory初始化 內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/weixin_38937840/article/details/118542134