上一篇 <<<Mybatis的整體執(zhí)行原理圖解
下一篇 >>>SqlSession的創(chuàng)建過程
1.初始化XMLConfigBuilder類暮刃,并裝載XPathParser解析器
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
2.配置文件解析parser.parse()
configuration
|--- properties
|--- settings
|--- typeAliases
|--- typeHandlers
|--- objectFactory
|--- plugins
|--- environments
|--- |--- environment
|--- |--- |--- transactionManager
|--- |--- |__ dataSource
|__ mappers
XMLConfigBuilder.parsed = true; --設(shè)置全局變量,只允許裝載一次
return XMLConfigBuilder.configuration;--結(jié)果返回
核心代碼:
//XMLConfigBuilder解析從配置文件根目錄(/configuration)開始
this.parseConfiguration(this.parser.evalNode("/configuration"));
//設(shè)置configuration的環(huán)境,包含有id唆途、事務(wù)及數(shù)據(jù)源信息
this.environmentsElement(root.evalNode("environments"));
this.configuration.setEnvironment(environmentBuilder.build());
class Environment {
String id;
TransactionFactory transactionFactory;
DataSource dataSource;
}
//支持resource壮虫、package等多種方式
this.mapperElement(root.evalNode("mappers"));
resource:mappers/UserMapper.xml inputStream為具體的流內(nèi)容
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, this.configuration, resource, this.configuration.getSqlFragments());
mapperParser.parse();
解析mapper文件組裝sql語句并加入到configuration中
this.configurationElement(this.parser.evalNode("/mapper"));
--this.buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
XMLStatementBuilder statementParser = new XMLStatementBuilder(this.configuration, this.builderAssistant, context, requiredDatabaseId);
statementParser.parseStatementNode();--組裝sql語句
this.configuration.addMappedStatement(statement);--加入到configuration中
//設(shè)置資源文件已解析
this.configuration.loadedResources.add("mappers/UserMapper.xml");
//設(shè)置mapperRegistry
this.bindMapperForNamespace();
this.configuration.mapperRegistry.hasMapper(接口類:com.jgspx.mapper.UserMapper);
this.configuration.loadedResources.add("namespace:" + namespace);
1.BaseBuilder:所有解析器的父類咪啡,包含配置文件實(shí)例夯到,為解析文件提供的一些通用的方法峦萎;
2.XMLConfigBuilder: 主要負(fù)責(zé)解析 mybatis-config.xml茂契;
3.XMLMapperBuilder: 主要負(fù)責(zé)解析映射配置 Mapper.xml 文件蝶桶;
4.XMLStatementBuilder: 主要負(fù)責(zé)解析映射配置文件中的 SQL 節(jié)點(diǎn);
3.初始化并返回默認(rèn)的sessionfactory
return new DefaultSqlSessionFactory(config);
推薦閱讀:
<<<Mybatis的整體執(zhí)行原理圖解
<<<SqlSession的創(chuàng)建過程
<<<sqlSession如何獲得具體的Mapper接口信息
<<<userMapper.getUser(1);底層實(shí)現(xiàn)原理
<<<sqlSession.selectOne底層實(shí)現(xiàn)原理
<<<Mybatis一級(jí)緩存知識(shí)匯總
<<<Mybatis二級(jí)緩存知識(shí)匯總
<<<Springboot整合Mybatis二級(jí)緩存
<<<Mybatis常見面試題
<<<Mybatis攔截器原理及實(shí)例