一切的执行从MapperProxy开始,MapperProxy是MapperProxyFactory使用SqlSession创建出来的。所以MapperProxy中包含SqlSession。

  可以看到MapperProxy调用invoke方法,进而调用MapperMethod的execute(),这些MapperMethod就是和你要执行的命令相关,比如执行select语句,则会通过SqlSession的select()方法,最终调用到Executor的query方法。Executor会再协调另外三个核心组件。

  • MapperProxyFactory用来创建MapperProxy,这个factory其实主要就是完成了InvokeHandler的bindTarget的功能。而MapperProxy只需要完成invoke方法的功能。
  • MapperProxy包含SqlSession
  • SqlSesion包含四大组件Executor,StatementHandler,ParameterHandler,ResultHandler。还包含Configuration
  • Configuration可以创建四大组件,同时Configuration还包含InterceptorChain,通过调用interceptorChain的pluginAll()方法,完成针对四大组件的插件的动态代理链的创建。

MapperProxy:

  • 因为Mapper接口不能直接被实例化,Mybatis利用JDK动态代理,创建MapperProxy间接实例化Mapper对象。
  • MapperProxy还可以缓存MapperMethod对象

MapperMethod:

  • 负责解析Mapper接口的方法,并封装成MapperMethod对象
  • 将Sql命令的执行路由到恰当的SqlSesison方法上
 1 public class MapperMethod {
2
3 // 保存了Sql命令的类型和键id
4 private final SqlCommand command;
5 // 保存了Mapper接口方法的解析信息
6 private final MethodSignature method;
7
8 public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
9 this.command = new SqlCommand(config, mapperInterface, method);
10 this.method = new MethodSignature(config, method);
11 }
12
13 // 根据解析结果,路由到恰当的SqlSession方法上
14 public Object execute(SqlSession sqlSession, Object[] args) {
15 Object result;
16 if (SqlCommandType.INSERT == command.getType()) {
17 Object param = method.convertArgsToSqlCommandParam(args);
18 result = rowCountResult(sqlSession.insert(command.getName(), param));
19 } else if (SqlCommandType.UPDATE == command.getType()) {
20 Object param = method.convertArgsToSqlCommandParam(args);
21 result = rowCountResult(sqlSession.update(command.getName(), param));
22 } else if (SqlCommandType.DELETE == command.getType()) {
23 Object param = method.convertArgsToSqlCommandParam(args);
24 result = rowCountResult(sqlSession.delete(command.getName(), param));
25 } else if (SqlCommandType.SELECT == command.getType()) {
26 if (method.returnsVoid() && method.hasResultHandler()) {
27 executeWithResultHandler(sqlSession, args);
28 result = null;
29 } else if (method.returnsMany()) {
30 result = executeForMany(sqlSession, args);
31 } else if (method.returnsMap()) {
32 result = executeForMap(sqlSession, args);
33 } else {
34 Object param = method.convertArgsToSqlCommandParam(args);
35 result = sqlSession.selectOne(command.getName(), param);
36 }
37 } else if (SqlCommandType.FLUSH == command.getType()) {
38 result = sqlSession.flushStatements();
39 } else {
40 throw new BindingException("Unknown execution method for: " + command.getName());
41 }
42 if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
43 throw new BindingException("Mapper method '" + command.getName()
44 + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
45 }
46 return result;
47 }
48 // ...

插件的构建:

  谈原理首先要知道StatementHandler,ParameterHandler,Result Handler都是代理,他们是Configuration创建,在创建过程中会调用interceptorChain.pluginAll()方法,为四大组件组装插件(再底层是通过Plugin.wrap(target,XX, new Plugin( interceptor))来来创建的)。

插件链是何时构建的:

  在执行SqlSession的query或者update方法时,SqlSession会通过Configuration创建Executor代理,在创建过程中就调用interceptor的pluginAll方法组装插件。然后executor在调用doQuery()方法的时候,也会调用Configuration的newStatementHandler方法创建StatemenHandler(和上面描述的一样,这个handler就是个代理,也是通过interceptorChain的pluginAll方法构建插件)

插件如何执行:

  以statementhandler的prepare方法的插件为例,正如前面所说,statementhandler是一个proxy,执行他的prepare方法,将调用invokeHandler的invoke方法,而invokeHandler就是Plugin.wrap(target, xxx, new Plugin(interceptor))中的第三个参数,所以很自然invokeHanlder的invoke的方法最终就会调用interceptor对象的intercept方法。

Mybatis执行SQL的完整过程及四大组件介绍的更多相关文章

  1. 一个PHP的SQL注入完整过程

    本篇文章介绍的内容是一个PHP的SQL注入完整过程,现在分享给大家,有需要的朋友可以参考一下 希望帮助到大家,很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里 ...

  2. Mybatis执行sql(insert、update、delete)返回值问题

    数据库:Mysql 在使用mybatis的过程中对执行sql的返回值产生疑问,顺手记录一下. 结论: insert:   插入n条记录,返回影响行数n.(n>=1,n为0时实际为插入失败) up ...

  3. 在mybatis执行SQL语句之前进行拦击处理

    转载自:http://blog.csdn.net/hfmbook/article/details/41985853 比较适用于在分页时候进行拦截.对分页的SQL语句通过封装处理,处理成不同的分页sql ...

  4. log4j打印mybatis执行sql,将占位符换成真实的参数输出

    背景: 在我日常码代码的时候,由于对mybatis的动态sql,比较依赖,并且有时候需求复杂,导致sql较长,而且参数众多,当出现问题是,需要将sql,放到navicat里面去执行查看结果,但是对于复 ...

  5. oracle-SQL语句执行原理和完整过程详解

    SQL语句执行过程详解 一条sql,plsql的执行到底是怎样执行的呢? 一.SQL语句执行原理 第一步:客户端吧语句发个服务端执行 当我们在客户端执行select语句时,客户端会把这条SQL语句发送 ...

  6. Mybatis执行SQL的流程

    前篇:Mybatis初始化过程 SqlSession : SqlSession是一个接口,它有两个实现类:DefaultSqlSession (默认)和 SqlSessionManager (弃用,不 ...

  7. mybatis整合spring的完整过程

    1.1 整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得sqlsession对象. 3.Mappe ...

  8. Oracle执行SQL语句的过程

    转载至:http://blog.csdn.net/aqszhuaihuai/article/details/7024551 当我们提交一条sql语句时,Oracle会做哪些操作呢? Oracle会为每 ...

  9. 使用Mybatis执行sql脚本

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

随机推荐

  1. 08 . Jenkins之SpringCloud微服务+Vue+Docker持续集成

    简介 大致流程 /* 1.开发人员每天把代码提交到Gitlab代码仓库 2.jenkins从gitlab中拉取项目源码,编译并打包成war包,然后构建Docker镜像,将镜像上传到Harbor私有仓库 ...

  2. P4715 【深基16.例1】淘汰赛

    P4715 [深基16.例1]淘汰赛 题目描述 有 2^n(n≤7) 个国家参加世界杯决赛圈且进入淘汰赛环节.我经知道各个国家的能力值,且都不相等.能力值高的国家和能力值低的国家踢比赛时高者获胜.1 ...

  3. matlab中datest() 将日期和时间转换为字符串格式

    来源:https://ww2.mathworks.cn/help/matlab/ref/datestr.html?searchHighlight=datestr&s_tid=doc_srcht ...

  4. VUE 安装项目

    注意:在cmd中执行的命令 1,前提是安装了node.js 查看 npm 版本号 2,创建项目路径 mkdir vue cd vue 3,安装vue-cli (脚手架) npm install -个v ...

  5. shell-字符串及整数操作符讲解与多实践

    1. 字符串测试操作符 字符串测试操作符的作用:比较两个字符串是否相同.字符串长度是否为零,字符串是否为null(注:bash区分零长度字符串和空字符串)等. "="比较两个字符串 ...

  6. ansible-任务控制tags

    1. ansible-任务控制tags介绍        如果你有一个大型的剧本,那么只能运行它的特定部分而不是在剧本中运行所有内容可能会很有用.因此,Ansible支持"tags:&quo ...

  7. JSX 详解

    一 jsx 的本质是什么? jsx是语法糖,需要被编译成js才能运行. jsx 看似是html 结构,实质是js结构的语法糖,在代码编译阶段被编译成js结构.所以jsx的本质可描述为看似html结构的 ...

  8. PHP代码审计03之实例化任意对象漏洞

    前言 根据红日安全写的文章,学习PHP代码审计的第三节内容,题目均来自PHP SECURITY CALENDAR 2017,讲完相关知识点,会用一道CTF题目来加深巩固.之前分别学习讲解了in_arr ...

  9. day32 Pyhton hashlib模块 总结异常处理

    一.当用明文密码进行信息存储的时候,会导致密码的泄露,如何解决问题 通过导入hashlib模块,利用里面存在的算法对字符串进行加密计算得到一串密文的结果 1.这个过程不可逆 2.对于同一个字符串,同一 ...

  10. hugo主题文档-manpassant

    +++ date="2020-10-17T10:32:00+08:00" title="hugo主题文档manpassant" tags=["hugo ...