mybatis的运行分为两个部分,第一个部分是读取配置文件并缓存到Configuration对象,用以创建SqlSessionFactory对象。第二部分是sqlSession运行过程。这篇文章主要讨论如何解析配置文件得到Configuration对象,以及Configuration对象中一些重要属性的含义。
1.配置文件解析
先来看读取配置文件得到Configuration对象的过程。mybatis的运行入口是SqlSessionFactoryBuilder.build()方法,通过给这个方法传入一个配置文件的输入流,则可以拿到SqlSessionFactory对象。SqlSessionFactory对象代表的是一个数据库对象,通过它可以获得与数据库会话的sqlSession对象。所以将配置文件解析为Configuration对象是在SqlSessionFactoryBuilder.build()方法里实现的。这里解析的配置文件包括mybatis-config.xml和各个mapper.xml文件,下面是build()方法的源码:
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().reset();
try {
inputStream.close();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
}
}
}
可以看到具体的解析过程是通过XMLConfigBuilder来实现的,这个类中是包含一个Configuration对象的,并且限制了一个XMLConfigBuilder对象只能解析一次得到一个Configuration对象,下面具体看下其parse()方法:
public Configuration parse() {
if (parsed) {
throw new BuilderException("Each XMLConfigBuilder can only be used once.");
}
parsed = true;
parseConfiguration(parser.evalNode("/configuration"));
return configuration;
}
这里负责拿出根节点,然后里面具体元素的解析是交给了parseConfiguration()方法,下面是其源码:
private void parseConfiguration(XNode root) {
try {
//issue #117 read properties first
propertiesElement(root.evalNode("properties"));
Properties settings = settingsAsProperties(root.evalNode("settings"));
loadCustomVfs(settings);
typeAliasesElement(root.evalNode("typeAliases"));
pluginElement(root.evalNode("plugins"));
objectFactoryElement(root.evalNode("objectFactory"));
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
reflectorFactoryElement(root.evalNode("reflectorFactory"));
settingsElement(settings);
// read it after objectFactory and objectWrapperFactory issue #631
environmentsElement(root.evalNode("environments"));
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQ