博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
深入浅出Mybatis系列(二)---配置简介(mybatis源码篇)
阅读量:6860 次
发布时间:2019-06-26

本文共 3711 字,大约阅读时间需要 12 分钟。

上篇文章《》, 写了一个Demo简单体现了一下Mybatis的流程。本次,将简单介绍一下Mybatis的配置文件:

上次例子中,我们以 SqlSessionFactoryBuilder 去创建 SqlSessionFactory,  那么,我们就先从SqlSessionFactoryBuilder入手, 咱们先看看源码是怎么实现的:

SqlSessionFactoryBuilder源码片段:

1 public class SqlSessionFactoryBuilder { 2 3 //Reader读取mybatis配置文件,传入构造方法 4 //除了Reader外,其实还有对应的inputStream作为参数的构造方法, 5 //这也体现了mybatis配置的灵活性 6 public SqlSessionFactory build(Reader reader) { 7 return build(reader, null, null); 8 } 9 10 public SqlSessionFactory build(Reader reader, String environment) { 11 return build(reader, environment, null); 12 } 13 14 //mybatis配置文件 + properties, 此时mybatis配置文件中可以不配置properties,也能使用${}形式 15 public SqlSessionFactory build(Reader reader, Properties properties) { 16 return build(reader, null, properties); 17 } 18 19 //通过XMLConfigBuilder解析mybatis配置,然后创建SqlSessionFactory对象 20 public SqlSessionFactory build(Reader reader, String environment, Properties properties) { 21 try { 22 XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties); 23 //下面看看这个方法的源码 24 return build(parser.parse()); 25 } catch (Exception e) { 26 throw ExceptionFactory.wrapException("Error building SqlSession.", e); 27 } finally { 28 ErrorContext.instance().reset(); 29 try { 30 reader.close(); 31 } catch (IOException e) { 32 // Intentionally ignore. Prefer previous error. 33 } 34 } 35 } 36 37 public SqlSessionFactory build(Configuration config) { 38 return new DefaultSqlSessionFactory(config); 39 } 40 41 }

通过源码,我们可以看到SqlSessionFactoryBuilder 通过XMLConfigBuilder 去解析我们传入的mybatis的配置文件, 下面就接着看看 XMLConfigBuilder 部分源码:

1 /** 2  * mybatis 配置文件解析 3  */ 4 public class XMLConfigBuilder extends BaseBuilder { 5 public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) { 6 this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props); 7 } 8 9 private XMLConfigBuilder(XPathParser parser, String environment, Properties props) { 10 super(new Configuration()); 11 ErrorContext.instance().resource("SQL Mapper Configuration"); 12 this.configuration.setVariables(props); 13 this.parsed = false; 14 this.environment = environment; 15 this.parser = parser; 16 } 17 18 //外部调用此方法对mybatis配置文件进行解析 19 public Configuration parse() { 20 if (parsed) { 21 throw new BuilderException("Each XMLConfigBuilder can only be used once."); 22 } 23 parsed = true; 24 //从根节点configuration 25 parseConfiguration(parser.evalNode("/configuration")); 26 return configuration; 27 } 28 29 //此方法就是解析configuration节点下的子节点 30 //由此也可看出,我们在configuration下面能配置的节点为以下10个节点 31 private void parseConfiguration(XNode root) { 32 try { 33 propertiesElement(root.evalNode("properties")); //issue #117 read properties first 34 typeAliasesElement(root.evalNode("typeAliases")); 35 pluginElement(root.evalNode("plugins")); 36 objectFactoryElement(root.evalNode("objectFactory")); 37 objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); 38 settingsElement(root.evalNode("settings")); 39 environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631 40 databaseIdProviderElement(root.evalNode("databaseIdProvider")); 41 typeHandlerElement(root.evalNode("typeHandlers")); 42 mapperElement(root.evalNode("mappers")); 43 } catch (Exception e) { 44 throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); 45 } 46 } 47 }

通过以上源码,我们就能看出,在mybatis的配置文件中:

1. configuration节点为根节点。

2. 在configuration节点之下,我们可以配置10个子节点, 分别为:properties、typeAliases、plugins、objectFactory、objectWrapperFactory、 settings、environments、databaseIdProvider、typeHandlers、mappers。

 

本篇文章就先只介绍这些内容,接下来的文章将依次分析解析这个10个节点中比较重要的几个节点的源码,看看在解析这些节点的时候,到底做了些什么。

原文链接:

转载于:https://www.cnblogs.com/jerrylz/p/5486308.html

你可能感兴趣的文章
算法笔记 --- Insertion Sort
查看>>
子父表
查看>>
CUDA npp运动检测模块性能测试
查看>>
前端单点登录(SSO)实现方法(二级域名与主域名)
查看>>
extjs客户端与ABP框架的服务端数据交互杂记
查看>>
kali linux fuzz工具集简述
查看>>
微信小程序云开发不完全指北
查看>>
《构建之法》阅读笔记二
查看>>
20165324 前四周总结反思
查看>>
11.11评价
查看>>
第一章--第一节:环境搭建
查看>>
hdu 2665 划分树
查看>>
hdu 4251 划分树
查看>>
poj 1704 Georgia and Bob(阶梯博弈)
查看>>
JQuery中的$.ajax()
查看>>
js 幻灯片
查看>>
Keras序列模型学习
查看>>
[bzoj2809] 派遣
查看>>
Flask 第四篇 Flask 中的模板语言 Jinja2 及 render_template 的深度用法
查看>>
PHP输出缓冲
查看>>