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