您好,登錄后才能下訂單哦!
// 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
// XMLConfigBuilder屬性
public class XMLConfigBuilder extends BaseBuilder {
//是否已解析
private boolean parsed;
// XPath解析器
private XPathParser parser;
// 當前環境
private String environment;
}
SqlSessionFactoryBuilder調用XMLConfigBuilder的parse()函數
// <?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() {
//如果已經解析過了,報錯
if (parsed) {
throw new BuilderException("Each XMLConfigBuilder can only be used once.");
}
parsed = true;
//根節點是configuration
parseConfiguration(parser.evalNode("/configuration"));
return configuration;
}
通過源碼可以看到parse()函數實際上是調用的parseConfiguration()函數,內部實現如下:
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.設置
settingsElement(root.evalNode("settings"));
// read it after objectFactory and objectWrapperFactory issue #631
//7.環境
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);
}
}
以上函數為mybatis對于配置文件的關鍵節點進行解析處理.
//<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 允許你在某一點攔截已映射語句執行的調用。默認情況下,MyBatis 允許使用插件來攔截方法調用
//<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>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。