mybatis源码阅读-执行一个sql的流程(九)
图解
图片来源:https://my.oschina.net/zudajun/blog/670373
Mapper接口调用原理
我们整合成Spring 直接使用Mapper就能执行对应的sql
表现形式
xml
<select id="selectAll" resultType="com.liqiang.entity.Classes">
select * from classes
</select>
mapper
ClassesMapper classesMapper=sqlSessionFactory.openSession().getMapper(ClassesMapper.class);
List<Classes> classesList= classesMapper.selectAll();
原理
内部通过调用Configuration的MapperRegistry通过Proxy生成mapper接口的MapperProxy
代理对象
public class MapperRegistry {
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory) this.knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
} else {
try {
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception var5) {
throw new BindingException("Error getting mapper instance. Cause: " + var5, var5);
}
}
}
}
public class MapperProxy<T> implements InvocationHandler, Serializable {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//如果不是接口类型直接调用 不增强
if (Object.class.equals(method.getDeclaringClass())) {
try {
return method.invoke(this, args);
} catch (Throwable var5) {
throw ExceptionUtil.unwrapThrowable(var5);
}
} else {
//将Mehod传入封装成MapperMethod并调用execute方法
MapperMethod mapperMethod = this.cachedMapperMethod(method);
return mapperMethod.execute(this.sqlSession, args);
}
} private MapperMethod cachedMapperMethod(Method method) {
MapperMethod mapperMethod = (MapperMethod)this.methodCache.get(method);
if (mapperMethod == null) {
mapperMethod = new MapperMethod(this.mapperInterface, method, this.sqlSession.getConfiguration());
this.methodCache.put(method, mapperMethod);
}
return mapperMethod;
}
}
MapperMethod
public class MapperMethod {
public Object execute(SqlSession sqlSession, Object[] args) {
Object param;
Object result;
//通过方法的操作类型进行路由 commandType是根据当前mapper的namespace+方法名字作为id去找对对应的MaperStatement的 commandType获得
if (SqlCommandType.INSERT == this.command.getType()) {
param = this.method.convertArgsToSqlCommandParam(args);
result = this.rowCountResult(sqlSession.insert(this.command.getName(), param));
} else if (SqlCommandType.UPDATE == this.command.getType()) {
param = this.method.convertArgsToSqlCommandParam(args);
result = this.rowCountResult(sqlSession.update(this.command.getName(), param));
} else if (SqlCommandType.DELETE == this.command.getType()) {
param = this.method.convertArgsToSqlCommandParam(args);
result = this.rowCountResult(sqlSession.delete(this.command.getName(), param));
} else if (SqlCommandType.SELECT == this.command.getType()) {
if (this.method.returnsVoid() && this.method.hasResultHandler()) {
this.executeWithResultHandler(sqlSession, args);
result = null;
} else if (this.method.returnsMany()) {
result = this.executeForMany(sqlSession, args);
} else if (this.method.returnsMap()) {
result = this.executeForMap(sqlSession, args);
} else if (this.method.returnsCursor()) {
result = this.executeForCursor(sqlSession, args);
} else {
param = this.method.convertArgsToSqlCommandParam(args);
result = sqlSession.selectOne(this.command.getName(), param);
}
} else {
if (SqlCommandType.FLUSH != this.command.getType()) {
throw new BindingException("Unknown execution method for: " + this.command.getName());
} result = sqlSession.flushStatements();
} if (result == null && this.method.getReturnType().isPrimitive() && !this.method.returnsVoid()) {
throw new BindingException("Mapper method '" + this.command.getName() + " attempted to return null from a method with a primitive return type (" + this.method.getReturnType() + ").");
} else {
return result;
}
}
mybatis源码阅读-执行一个sql的流程(九)的更多相关文章
- Caddy源码阅读(二)启动流程与 Event 事件通知
Caddy源码阅读(二)启动流程与 Event 事件通知 Preface Caddy 是 Go 语言构建的轻量配置化服务器.https://github.com/caddyserver/caddy C ...
- Spring mybatis源码篇章-XMLLanguageDriver解析sql包装为SqlSource
前言:通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-MybatisDAO文件解析(二) 首先了解下sql mapper的动态sql语法 具体的动态sql的 ...
- Mybatis源码阅读-配置文件及映射文件解析
Mybatis源码分析: 1.配置文件解析: 1.1源码阅读入口: org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(); 功能:解析全局配置文 ...
- mybatis源码-Mapper解析之SQL 语句节点解析(一条语句对应一个MappedStatement)
目录 一起学 mybatis 0 <sql> 节点解析 1 解析流程 2 节点解析 2.1 解析流程 2.2 <include> 节点的解析 2.3 Node.ELEMENT_ ...
- mybatis源码阅读(动态代理)
这一篇文章主要是记录Mybatis的动态代理学习成果,如果对源码感兴趣,可以看一下上篇文章 https://www.cnblogs.com/ChoviWu/p/10118051.html 阅读本篇的 ...
- mybatis源码阅读心得
第一天阅读源码及创建时序图.(第一次用prosson画时序图,挺丑..) 1. 调用 SqlSessionFactoryBuilder 对象的 build(inputStream) 方法: 2. ...
- mybatis源码阅读-初始化过程(七)
说明 mybatis初始化过程 就是解析xml到封装成Configuration对象 供后续使用 SqlSessionFactoryBuilder 代码例子 SqlSessionFactoryBuil ...
- Mybatis源码阅读 之 玩转Executor
承接上篇博客, 本文探究MyBatis中的Executor, 如下图: 是Executor体系图 本片博客的目的就是探究如上图中从顶级接口Executor中拓展出来的各个子执行器的功能,以及进一步了解 ...
- Mybatis源码系列 执行流程(一)
1.Mybatis的使用 public static void main(String[] args) throws IOException { //1.获取配置文件流 InputStream is ...
随机推荐
- 清北考前刷题day2下午好
#include<iostream> #include<cstdio> #include<cstring> #include<stack> #defin ...
- 读懂mysql慢查询日志
我们来看一下如何去读懂这些慢查询日志.在跟踪慢查询日志之前,首先你得保证最少发生过一次慢查询.如果你没有可以自己制造一个:root@server# mysql -e 'SELECT SLEEP(8); ...
- ROS-URDF仿真
前言:URDF (标准化机器人描述格式),是一种用于描述机器人及其部分结构.关节.自由度等的XML格式文件. 一.首先做一个带有四个轮子的机器人底座. 1.1 新建urdf文件 在chapter4_t ...
- RaspberryPi cProfile使用
使用sudo python -m cProfile -o 1.cprof your.py生成cprof文件 windows下安装snakeviz:pip install -i https://pypi ...
- springboot与dubbo整合入门(三种方式)
Springboot与Dubbo整合三种方式详解 整合环境: jdk:8.0 dubbo:2.6.2 springboot:2.1.5 项目结构: 1.搭建项目环境: (1)创建父项目与三个子项目,创 ...
- Activity随笔
Activity的生命周期 1.正常情况下的生命周期 onCreate: Activity正在被创建,生命周期中的第一个方法,常在此方法中做一些初始化工作,比如调用setContentView方法, ...
- python对象以及pickle腌制
#python对象 1.什么是python的对象 2.详解pickle腌制 1.什么是python的对象 Python的内置的对象类型主要有数字.字符串.列表.元组.字典.集合等等.其实,在pytho ...
- python学习笔记(6)——字典(Dictionary)
dict= {key1 : value1, key2 : value2 ...} 关键词:字典中元素成对出现- key:value 格式- 两端{ } ,键:值,每对键值间用 ,隔开. 键key-唯一 ...
- 【alert(1) to win】不完全攻略
alert(1) to win 一个练习XSS的平台,地址:https://alf.nu/alert1 Warmup 给出了一段JavaScript代码 function escape(s) { re ...
- 并发编程学习笔记(12)----Fork/Join框架
1. Fork/Join 的概念 Fork指的是将系统进程分成多个执行分支(线程),Join即是等待,当fork()方法创建了多个线程之后,需要等待这些分支执行完毕之后,才能得到最终的结果,因此joi ...