一 、MyBatis原理架构图

Mybatis的功能架构分为三层:

  • API接口层:提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库。接口层一接收到调用请求就会调用数据处理层来完成具体的数据处理。
  • 数据处理层:负责具体的SQL查找、SQL解析、SQL执行和执行结果映射处理等。它主要的目的是根据调用的请求完成一次数据库操作。
  • 基础支撑层:负责最基础的功能支撑,包括连接管理、事务管理、配置加载和缓存处理,这些都是共用的东西,将他们抽取出来作为最基础的组件。为上层的数据处理层提供最基础的支撑。

二、MyBatis工作流程

1、 mybatis配置SqlMapConfig.xml,此文件作为mybatis的全局配置文件,配置了mybatis的运行环境等信息。
mapper.xml文件即sql映射文件,文件中配置了操作数据库的sql语句。此文件需要在SqlMapConfig.xml中加载。

2、 通过mybatis环境等配置信息构造SqlSessionFactory即会话工厂

3、 由会话工厂创建sqlSession即会话,操作数据库需要通过sqlSession进行。

4、 mybatis底层自定义了Executor执行器接口操作数据库(CURD),Executor接口有两个实现,一个是基本执行器、一个是缓存执行器。Executor才是真正将sql与Connection打交道的地方。在真正执行查询时,外面是嵌套了一层CachingExecutor。

首先,最底层的接口是Executor,有两个实现类:BaseExecutor和CachingExecutor,CachingExecutor用于二级缓存,而BaseExecutor则用于一级缓存及基础的操作。并且由于BaseExecutor是一个抽象类,提供了三个实现:SimpleExecutor,BatchExecutor,ReuseExecutor,而具体使用哪一个Executor则是可以在mybatis-config.xml中进行配置的。配置如下:
<settings>
<!--SIMPLE、REUSE、BATCH-->
<setting name="defaultExecutorType" value="REUSE"/>
</settings>

不开启二级缓存

<settings>
<setting name="cacheEnabled" value="false"/>
</settings>

通过configuration.newExecutor来获取Executor

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor;
// 根据不同的executorType来创建
if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
}
// cacheEnabled 也就是我们配置的二级缓存,如果该值配置为true,则获取的是CachingExecutor
if (cacheEnabled) {
executor = new CachingExecutor(executor);
}
// 这里是通过责任链模式来生成代理对象
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}

  

5、 Mapped Statement也是mybatis一个底层封装对象,它包装了mybatis配置信息及sql映射信息等。mapper.xml文件中一个sql对应一个Mapped Statement对象,sql的id即是Mapped statement的id(mapper.xml文件中的namespacce+id)。

6、 Mapped Statement对sql执行输入参数进行定义,包括HashMap、基本类型、pojo,Executor通过 Mapped Statement在执行sql前将输入的java对象映射至sql中,输入参数映射就是jdbc编程中对preparedStatement设置参数。

7、 Mapped Statement对sql执行输出结果进行定义,包括HashMap、基本类型、pojo,Executor通过 Mapped Statement在执行sql后将输出结果映射至java对象中,输出结果映射过程相当于jdbc编程中对结果的解析处理过程。

最新文章

  1. 架构师养成记--4.volatile关键字
  2. iOS 学习 - 10下载(3) NSURLSession 音乐 篇
  3. rpm包制作(待实验)
  4. 与你相遇好幸运,mbview的mbtiles文件分析
  5. Win7开始菜单之【附件】不全解决方案
  6. HDOJ(1003) Max Sum
  7. MKDOCS在线文档编辑器
  8. 【Oracle】控制文件管理
  9. PHP数组运算符
  10. python 爬取糗事百科 gui小程序
  11. 如何让 jQuery Mobile 不显示讨厌的 loading 界面
  12. Ubuntu下crontab启动、重启、关闭命令
  13. postgreSQL学习(二):pgsql的一些基础操作
  14. react与react-router
  15. Linux下clock计时函数学习
  16. AnswerOpenCV(0826-0901)一周佳作欣赏
  17. WebService远程调用技术
  18. terraform plugin 版本以及changlog 规范
  19. spring boot 整合redis --sea 方式1
  20. PHPSQL注入

热门文章

  1. C++:只用初始化列表初始化变量的几种情况
  2. 2018-8-10-WPF-修改按钮按下的颜色
  3. C# 局部函数与事件
  4. chrome谷歌浏览器怎么清除指定网站cookie
  5. shell去掉最后一个字符
  6. phpstudy2016安装redis扩展
  7. poj 3181 Dollar Dayz(完全背包)
  8. mysql基础(库、表相关)
  9. 深入java面向对象三:抽象类和接口(转载)
  10. SAX解析xml (遍历DOM树各节点)