MyBatis之初始化

2021-09-24 11:36:19 字數 3886 閱讀 3822

mybatis初始化的主要工作是載入並解析mybatis-config.xml配置檔案、對映配置檔案以及相關的註解資訊。

mybatis初始化的入口是 sqlsessionfactorybuilder#build(reader reader, string environment, properties properties)

public sqlsessionfactory build(reader reader, string environment, properties properties)  catch (exception e)  finally  catch (ioexception e) 

}}

1.configuration是mybatis初始化過程的核心物件,幾乎全部的配置資訊會儲存到configuration物件中,而且是唯一的。

2.xmlconfigbuilder 的主要職責是負責解析 mybatis-config.xml 配置檔案

public class xmlconfigbuilder extends basebuilder
核心方法是 parseconfiguration,負責解析節點資訊

private void parseconfiguration(xnode root)  catch (exception e) 

}

public void parse() 

parsependingresultmaps();

parsependingchacherefs();

parsependingstatements();

}

private void configurationelement(xnode context) 

builderassistant.setcurrentnamespace(namespace);

cacherefelement(context.evalnode("cache-ref"));

cacheelement(context.evalnode("cache"));

buildstatementfromcontext(context.evalnodes("select|insert|update|delete"));

} catch (exception e)

}

4.xmlstatementbuilder主要負責解析sql節點

parsestatementnode是解析sql節點的入口:

public void parsestatementnode() 

integer fetchsize = context.getintattribute("fetchsize");

integer timeout = context.getintattribute("timeout");

string parametermap = context.getstringattribute("parametermap");

string parametertype = context.getstringattribute("parametertype");

class<?> parametertypeclass = resolveclass(parametertype);

string resultmap = context.getstringattribute("resultmap");

string resulttype = context.getstringattribute("resulttype");

string lang = context.getstringattribute("lang");

languagedriver langdriver = getlanguagedriver(lang);

class<?> resulttypeclass = resolveclass(resulttype);

string resultsettype = context.getstringattribute("resultsettype");

statementtype statementtype = statementtype.valueof(context.getstringattribute("statementtype", statementtype.prepared.tostring()));

resultsettype resultsettypeenum = resolveresultsettype(resultsettype);

string nodename = context.getnode().getnodename();

sqlcommandtype sqlcommandtype = sqlcommandtype.valueof(nodename.touppercase(locale.english));

boolean isselect = sqlcommandtype == sqlcommandtype.select;

boolean flushcache = context.getbooleanattribute("flushcache", !isselect);

boolean usecache = context.getbooleanattribute("usecache", isselect);

boolean resultordered = context.getbooleanattribute("resultordered", false);

// include fragments before parsing

xmlincludetransformer includeparser = new xmlincludetransformer(configuration, builderassistant);

// parse selectkey after includes and remove them.

processselectkeynodes(id, parametertypeclass, langdriver);

// parse the sql (pre: and were parsed and removed)

sqlsource sqlsource = langdriver.createsqlsource(configuration, context, parametertypeclass);

string resultsets = context.getstringattribute("resultsets");

string keyproperty = context.getstringattribute("keyproperty");

string keycolumn = context.getstringattribute("keycolumn");

keygenerator keygenerator;

string keystatementid = id + selectkeygenerator.select_key_suffix;

if (configuration.haskeygenerator(keystatementid)) else

fetchsize, timeout, parametermap, parametertypeclass, resultmap, resulttypeclass,

resultsettypeenum, flushcache, usecache, resultordered,

keygenerator, keyproperty, keycolumn, databaseid, langdriver, resultsets);

}

mybatis 原始碼分析之初始化

mybatis 的配置檔案解析又xmlconfigbuilder的parseconfiguration方法來完成,解析結果都存在configuration這個類中 private void parseconfiguration xnode root catch exception e 別名解析 pr...

筆記 初始化列表之初始化順序

一 前面已經介紹了初始化列表的好處,尤其對於類型別的變數來說,會提高效率,下面來介紹必須放在初始化列表的情況 無關效率問題 1 常量成員,因為常量 const 只能初始化不能賦值 雖說不可以賦值,但是可以通過指標的方式來訪問修改該值 所以必須放在初始化列表裡面。2 引用型別,引用必須在定義的時候初始...

初始化 MyBatis初始化之載入初始化

在mybatis初始化過程中,大致會有以下幾個步驟 1.建立configuration全域性配置物件,會往typealiasregistry別名註冊中心新增mybatis需要用到的相關類,並設定預設的語言驅動類為xmllanguagedriver 3.構建defaultsqlsessionfacto...