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 ...
随机推荐
- 从缓冲上看阻塞与非阻塞socket在发送接收上的区别(转载)
转自:http://blog.chinaunix.net/uid-24517549-id-4044877.html 首先socket在默认情况下是阻塞状态的,这就使得发送以及接收操作处于阻塞的状态 ...
- MyBatis基本应用
框架的概念: 框架(Framework)是一个提供了可重用的公共结构的半成品. 数据持久化: 数据持久化是将内存中的数据模型转换为存储模型,以及将存储模型转换为内存中的数据模型的统称. ORM(Obj ...
- ssm lodop打印图片不显示
在打印预览的时候图片就是不显示 最终解决方案就是修改过滤器
- [C++ STL] set使用详解
一.set介绍: set容器内的元素会被自动排序,set与map不同,set中的元素即是键值又是实值,set不允许两个元素有相同的键值.不能通过set的迭代器去修改set元素,原因是修改元素会破坏se ...
- [C++ STL] deque使用详解
一.deque介绍: deque(双端队列)是一个动态数组,可以向两端发展,因此不论在尾部或头部安插元素都十分迅速. 在中间部分安插元素则比较费时,因为必须移动其它元素. 二.用法 1.头文件 #in ...
- 04—AOP 实现项目中的切面编程
- 每天学点linux命令之nc
nc is NetCat.素以短小精悍著称的网络工具包.主要用来开放的扫描端口(黑客或者OSAdmin的最爱),不同主机之间传输文字 | 文件. http://blog.csdn.net/zhangx ...
- 1393 0和1相等串 鸽笼原理 || 化简dp公式
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1393 正解一眼看出来的应该是鸽笼原理.记录每个位置的前缀和,就是dp[i ...
- 6.12---esultMap查询所有
- java web 学习笔记 - Java Bean
1. Java Bean 是一个简单的 java 类,一般放在WEB-INF下的 classes目录下(如果没有则需要手工新建) 一个简单的Bean包括属性,getter ,setter方法,如果没有 ...