版權(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中職(汽車運(yùn)用與維修)汽車漆面修復(fù)試題及解析
- 2025年高職(冷鏈物流技術(shù))冷鏈運(yùn)輸管理試題及解析
- 2025年大學(xué)(中西醫(yī)臨床醫(yī)學(xué))精準(zhǔn)中西醫(yī)結(jié)合醫(yī)學(xué)試題及答案
- 2025年中職機(jī)電技術(shù)應(yīng)用(電工儀表使用)試題及答案
- 2025年大學(xué)(預(yù)防醫(yī)學(xué))流行病學(xué)階段測(cè)試題及解析
- 2025年大學(xué)植物保護(hù)(植物保護(hù))試題及答案
- 2025年高職托育基礎(chǔ)(托育基礎(chǔ))試題及答案
- 2025年高職通信技術(shù)(5G技術(shù)應(yīng)用)試題及答案
- 2025年中職藝術(shù)(藝術(shù)基礎(chǔ)應(yīng)用)試題及答案
- 2026年河南農(nóng)業(yè)職業(yè)學(xué)院?jiǎn)握新殬I(yè)技能筆試參考題庫帶答案解析
- 智慧林業(yè)云平臺(tái)信息化建設(shè)詳細(xì)規(guī)劃
- 培養(yǎng)方案修訂情況匯報(bào)
- 監(jiān)控綜合維保方案(3篇)
- 犢牛獸醫(yī)工作總結(jié)
- JJF(陜) 125-2025 醫(yī)用移動(dòng)式 C 形臂 X 射線輻射源校準(zhǔn)規(guī)范
- T/CCOA 33-2020平房倉氣密改造操作規(guī)范
- 自行車購車協(xié)議合同
- 2025萍鄉(xiāng)市湘東區(qū)輔警考試試卷真題
- 幼兒基本律動(dòng)培訓(xùn)
- 認(rèn)知障礙門診管理制度
- 農(nóng)村經(jīng)濟(jì)統(tǒng)計(jì)培訓(xùn)
評(píng)論
0/150
提交評(píng)論