上篇文章《深入浅出 Mybatis 系列 (二)---Mybatis 入门 https://www.cnblogs.com/deityjian/p/11074722.html 》写了一个 Demo 简单体现了一下 Mybatis 的流程. 本次, 将简单介绍一下 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 的配置文件中:
1. configuration 节点为根节点.
2. 在 configuration 节点之下, 我们可以配置 10 个子节点, 分别为: properties,typeAliases,plugins,objectFactory,objectWrapperFactory,settings,environments,databaseIdProvider,typeHandlers,mappers.
本篇文章就先只介绍这些内容, 接下来的文章将依次分析解析这个 10 个节点中比较重要的几个节点的源码, 看看在解析这些节点的时候, 到底做了些什么.
- /** * 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); } }}
来源: http://www.bubuko.com/infodetail-3102058.html