解析Mybatis SqlSessionFactory初始化原理_第1頁
解析Mybatis SqlSessionFactory初始化原理_第2頁
解析Mybatis SqlSessionFactory初始化原理_第3頁
解析Mybatis SqlSessionFactory初始化原理_第4頁
解析Mybatis SqlSessionFactory初始化原理_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第解析MybatisSqlSessionFactory初始化原理Stringresource="mybatis-config.xml";

InputStreaminputStream=Resources.getResourceAsStream(resource);

SqlSessionFactorysqlSessionFactory=newSqlSessionFactoryBuilder().build(inputStream);

build方法:

//1.我們最初調(diào)用的build

publicSqlSessionFactorybuild(InputStreaminputStream){

//調(diào)用了重載方法

returnbuild(inputStream,null,null);

//2.調(diào)用的重載方法

publicSqlSessionFactorybuild(InputStreaminputStream,Stringenvironment,Propertiesproperties){

try{

//創(chuàng)建XMLConfigBuilder,XMLConfigBuilder是專門解析mybatis的配置文件的類

XMLConfigBuilderparser=newXMLConfigBuilder(inputStream,environment,properties);

//執(zhí)行XML解析

//創(chuàng)建DefaultSqlSessionFactory對(duì)象

returnbuild(parser.parse());

}catch(Exceptione){

//···

parser.parse()

publicConfigurationparse(){

if(parsed){

thrownewBuilderException("EachXMLConfigBuildercanonlybeusedonce.");

//標(biāo)記已解析

parsed=true;

//parser.evalNode("/configuration"),

//通過xpath讀取配置文件的節(jié)點(diǎn),將讀取出配置文件的所以節(jié)點(diǎn)

//configuration

//environmentsdefault="development"

///environments

//configuration

parseConfiguration(parser.evalNode("/configuration"));

returnconfiguration;

parseConfiguration(XNoderoot)

//解析每個(gè)節(jié)點(diǎn)這里每個(gè)方法進(jìn)去都會(huì)有很多配置,這里就不一一解析,大家感興趣可以看看,

//settingsElement(settings);mapperElement(root.evalNode("mappers"));

privatevoidparseConfiguration(XNoderoot){

try{

//issue#117readpropertiesfirst

//解析properties/標(biāo)簽

propertiesElement(root.evalNode("properties"));

//解析settings/標(biāo)簽

Propertiessettings=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);

//readitafterobjectFactoryandobjectWrapperFactoryissue#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(Exceptione){

thrownewBuilderException("ErrorparsingSQLMapperConfiguration.Cause:"+e,e);

//獲取mapper

privatevoidmapperElement(XNodeparent)throwsException{

if(parent!=null){

for(XNodechild:parent.getChildren()){

//如果是包將在這里進(jìn)行渲染

if("package".equals(child.getName())){

StringmapperPackage=child.getStringAttribute("name");

configuration.addMappers(mapperPackage);

}else{

//讀取resource標(biāo)簽

Stringresource=child.getStringAttribute("resource");

//讀取url標(biāo)簽

Stringurl=child.getStringAttribute("url");

//讀取注解

StringmapperClass=child.getStringAttribute("class");

//根據(jù)不同的方式完成

if(resource!=nullurl==nullmapperClass==null){

ErrorContext.instance().resource(resource);

InputStreaminputStream=Resources.getResourceAsStream(resource);

XMLMapperBuildermapperParser=newXMLMapperBuilder(inputStream,configuration,resource,configuration.getSqlFragments());

mapperParser.parse();

}elseif(resource==nullurl!=nullmapperClass==null){

ErrorContext.instance().resource(url);

InputStreaminputStream=Resources.getUrlAsStream(url);

XMLMapperBuildermapperParser=newXMLMapperBuilder(inputStream,configuration,url,configuration.getSqlFragments());

mapperParser.parse();

}elseif(resource==nullurl==nullmapperClass!=null){

ClassmapperInterface=Resources.classForName(mapperClass);

configuration.addMapper(mapperInterface);

}else{

thrownewBuilderException("Amapperelementmayonlyspecifyaurl,resourceorclass,butnotmorethanone.");

privatevoidsettingsElement(Propertiesprops){

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();方法懂得原理,都是類似的

publicvoidparse(){

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文件

privatevoidconfigurationElement(XNodecontext){

try{

Stringnamespace=context.getStringAttribute("namespace");

if(namespace==null||namespace.isEmpty()){

thrownewBuilderException("Mapper'snamespacecannotbeempty");

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(Exceptione){

thrownewBuilderException("ErrorparsingMapperXML.TheXMLlocationis'"+resource+"'.Cause:"+e,e);

//關(guān)于注解的方式的parse

publicvoidparse(){

Stringresource=type.toString();

if(!configuration.isResourceLoaded(resource)){

loadXmlResource();

configuration.addLoadedResource(resource);

assistant.setCurrentNamespace(type.getName());

parseCache();

parseCacheRef();

for(Methodmethod: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(IncompleteElementExceptione){

configuration.addIncompleteMethod(newMethodResolver(this,method));

parsePendingMethods();

到此Mybatis的初始化工作就完畢了,主要做了兩件大事

解析核心配置文件到Configuration對(duì)象,解析映射配置文件到Mappe

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論