// 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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。