mybatis配置簡介
阿新 • • 發佈:2021-02-04
在Mybatis的配置檔案,我們以 SqlSessionFactoryBuilder 去建立 SqlSessionFactory, 那麼,我們就先從SqlSessionFactoryBuilder入手, 咱們先看看原始碼是怎麼實現的:
SqlSessionFactoryBuilder程式碼片斷:
.
public class SqlSessionFactoryBuilder {
//Reader讀取mybatis配置檔案,傳入構造方法
//除了Reader外,其實還有對應的inputStream作為引數的構造方法,
//這也體現了mybatis配置的靈活性
public SqlSessionFactory build(Reader reader) {
return build(reader, null, null);
}
public SqlSessionFactory build(Reader reader, String environment) {
return build(reader, environment, null);
}
//mybatis配置檔案 + properties, 此時mybatis配置檔案中可以不配置properties,也能使用${}形式
public SqlSessionFactory build (Reader reader, Properties properties) {
return build(reader, null, properties);
}
//通過XMLConfigBuilder解析mybatis配置,然後建立SqlSessionFactory物件
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder (reader, environment, properties);
//下面看看這個方法的原始碼
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().reset();
try {
reader.close();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
}
}
}
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config);
}
}
通過原始碼,我們可以看到SqlSessionFactoryBuilder 通過XMLConfigBuilder 去解析我們傳入的mybatis的配置檔案, 下面就接著看看 XMLConfigBuilder 部分原始碼:
/**
* mybatis 配置檔案解析
*/
public class XMLConfigBuilder extends BaseBuilder {
public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) {
this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props);
}
private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {
super(new Configuration());
ErrorContext.instance().resource("SQL Mapper Configuration");
this.configuration.setVariables(props);
this.parsed = false;
this.environment = environment;
this.parser = parser;
}
//外部呼叫此方法對mybatis配置檔案進行解析
public Configuration parse() {
if (parsed) {
throw new BuilderException("Each XMLConfigBuilder can only be used once.");
}
parsed = true;
//從根節點configuration
parseConfiguration(parser.evalNode("/configuration"));
return configuration;
}
//此方法就是解析configuration節點下的子節點
//由此也可看出,我們在configuration下面能配置的節點為以下10個節點
private void parseConfiguration(XNode root) {
try {
propertiesElement(root.evalNode("properties")); //issue #117 read properties first
typeAliasesElement(root.evalNode("typeAliases"));
pluginElement(root.evalNode("plugins"));
objectFactoryElement(root.evalNode("objectFactory"));
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
settingsElement(root.evalNode("settings"));
environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
}
通過以上原始碼,我們就能看出,在mybatis的配置檔案中:
-
configuration節點為根節點。
-
在configuration節點之下,我們可以配置10個子節點, 分別為:properties、typeAliases、plugins、objectFactory、objectWrapperFactory、settings、environments、databaseIdProvider、typeHandlers、mappers。