// BaseBuilder屬性
public abstract class BaseBuilder {
//需要配置,類型別名注冊,類型處理器注冊3個東西
protected final Configuration configuration;
protected final TypeAliasRegistry typeAliasRegistry;
protected final TypeHandlerRegistry typeHandlerRegistry;
}
public class XMLConfigBuilder extends BaseBuilder{},可見XMLConfigBuilder繼承自BaseBuilder
我們提供的服務有:網(wǎng)站設(shè)計制作、網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、蘆山ssl等。為上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術(shù)的蘆山網(wǎng)站制作公司
// XMLConfigBuilder屬性
public class XMLConfigBuilder extends BaseBuilder {
//是否已解析
private boolean parsed;
// XPath解析器
private XPathParser parser;
// 當前環(huán)境
private String environment;
}
SqlSessionFactoryBuilder調(diào)用XMLConfigBuilder的parse()函數(shù)
// <?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>
//解析配置
public Configuration parse() {
//如果已經(jīng)解析過了,報錯
if (parsed) {
throw new BuilderException("Each XMLConfigBuilder can only be used once.");
}
parsed = true;
//根節(jié)點是configuration
parseConfiguration(parser.evalNode("/configuration"));
return configuration;
}
通過源碼可以看到parse()函數(shù)實際上是調(diào)用的parseConfiguration()函數(shù),內(nèi)部實現(xiàn)如下:
private void parseConfiguration(XNode root) {
try {
//分步驟解析
//issue #117 read properties first
//1.properties
propertiesElement(root.evalNode("properties"));
//2.類型別名
typeAliasesElement(root.evalNode("typeAliases"));
//3.插件
pluginElement(root.evalNode("plugins"));
//4.對象工廠
objectFactoryElement(root.evalNode("objectFactory"));
//5.對象包裝工廠
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
//6.設(shè)置
settingsElement(root.evalNode("settings"));
// read it after objectFactory and objectWrapperFactory issue #631
//7.環(huán)境
environmentsElement(root.evalNode("environments"));
//8.databaseIdProvider
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
//9.類型處理器
typeHandlerElement(root.evalNode("typeHandlers"));
//10.映射器
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
以上函數(shù)為mybatis對于配置文件的關(guān)鍵節(jié)點進行解析處理.
//<properties resource="org/mybatis/example/config.properties">
// <property name="username" value="dev_user"/>
// <property name="password" value="F2Fa3!33TYyg"/>
//</properties>
//<typeAliases>
// <typeAlias alias="Author" type="domain.blog.Author"/>
// <typeAlias alias="Blog" type="domain.blog.Blog"/>
// <typeAlias alias="Comment" type="domain.blog.Comment"/>
// <typeAlias alias="Post" type="domain.blog.Post"/>
// <typeAlias alias="Section" type="domain.blog.Section"/>
// <typeAlias alias="Tag" type="domain.blog.Tag"/>
//</typeAliases>
//MyBatis 允許你在某一點攔截已映射語句執(zhí)行的調(diào)用。默認情況下,MyBatis 允許使用插件來攔截方法調(diào)用
//<plugins>
// <plugin interceptor="org.mybatis.example.ExamplePlugin">
// <property name="someProperty" value="100"/>
// </plugin>
//</plugins>
//<objectFactory type="org.mybatis.example.ExampleObjectFactory">
// <property name="someProperty" value="100"/>
//</objectFactory>
//<settings>
// <setting name="cacheEnabled" value="true"/>
// <setting name="lazyLoadingEnabled" value="true"/>
// <setting name="multipleResultSetsEnabled" value="true"/>
// <setting name="useColumnLabel" value="true"/>
// <setting name="useGeneratedKeys" value="false"/>
// <setting name="enhancementEnabled" value="false"/>
// <setting name="defaultExecutorType" value="SIMPLE"/>
// <setting name="defaultStatementTimeout" value="25000"/>
// <setting name="safeRowBoundsEnabled" value="false"/>
// <setting name="mapUnderscoreToCamelCase" value="false"/>
// <setting name="localCacheScope" value="SESSION"/>
// <setting name="jdbcTypeForNull" value="OTHER"/>
// <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
//</settings>
// <environments default="development">
// <environment id="development">
// <transactionManager type="JDBC">
// <property name="..." value="..."/>
// </transactionManager>
// <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>
// <databaseIdProvider type="VENDOR">
// <property name="SQL Server" value="sqlserver"/>
// <property name="DB2" value="db2"/>
// <property name="Oracle" value="oracle" />
// </databaseIdProvider>
// <typeHandlers>
// <typeHandler handler="org.mybatis.example.ExampleTypeHandler"/>
// </typeHandlers>
// 1.使用類路徑
// <mappers>
// <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
// <mapper resource="org/mybatis/builder/BlogMapper.xml"/>
// <mapper resource="org/mybatis/builder/PostMapper.xml"/>
// </mappers>
//
// 2.使用絕對url路徑
// <mappers>
// <mapper url="file:///var/mappers/AuthorMapper.xml"/>
// <mapper url="file:///var/mappers/BlogMapper.xml"/>
// <mapper url="file:///var/mappers/PostMapper.xml"/>
// </mappers>
//
// 3.使用java類名
// <mappers>
// <mapper class="org.mybatis.builder.AuthorMapper"/>
// <mapper class="org.mybatis.builder.BlogMapper"/>
// <mapper class="org.mybatis.builder.PostMapper"/>
// </mappers>
//
// 4.自動掃描包下所有映射器
// <mappers>
// <package name="org.mybatis.builder"/>
// </mappers>
分享名稱:Mybatis源碼分析[02.XMLConfigBuilder]
瀏覽地址:http://jinyejixie.com/article16/psppdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站排名、Google、微信小程序、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)