1.  我们通过接口操作数据库时,发现相关的操作都是在org.apache.ibatis.binding下

  • 从sqSessin 获取getMapper()
SqlSession session = sqlSessionFactory.openSession();
CxCaseMapper caseMapper = session.getMapper(CxCaseMapper.class);
CxCaseTable tablelm = (CxCaseTable) caseMapper.selectById(id);
  • binging 包结构

  可以发现binding 包输入mybatis 下面,是mybatis 的核心文件。这个包中包含有四个类:

  1. BindingException 该包中的异常类
  2. MapperMethod 代理类中真正执行数据库操作的类
  3. MapperProxy 实现了InvocationHandler接口的动态代理类
  4. MapperProxyFactory 为代理工厂
  5. MapperRegistry mybatis中mapper的注册类及对外提供生成代理类接口的类

  在sqlSession初始化的时候,将所有的mapper 注册到了MapperRegistry 中,以Map<Class<?>, MapperProxyFactory<?>>的形式存储。

2. 通过sqlSession接口的代理对象

  • SqSession中持有configuration,configuration中持有mapperRegister、mapperRegister中持有的mapper代理对象工程Map<Class<?>, MapperProxyFactory<?>>。
  • 当mapperRegister通过getMapper方法获取,代理对象的时候,流程已经进入到binding 包中。
  public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
//以mapper的class对象为key值,获取当面mapper的代理对象工厂
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
if (mapperProxyFactory == null) {
//如果没有获取到代理对象工厂 抛出BindingException 异常信息
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
try {
//用获取到的代理对象工厂,生产目标对象以及目标代理对象
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}
  • 代理对象工厂生产目标对象已经目标代理对象
  @SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
// 3.mapperProxy实现了InvocationHandler接口,主要为具体接口的代理
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
} public T newInstance(SqlSession sqlSession) {
// 1. 产生目标对象MapperProxy
final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
// 2. 根据目标对象,产生代理对象
return newInstance(mapperProxy);
}

 3.mapperProxy

  • 这个类继承了InvocationHandler接口,我们主要看这个类中的两个方法。一是生成具体代理类的函数newMapperProxy,另一个就是实现InvocationHandler接口的invoke。我们先看invoke方法。
public class MapperProxy<T> implements InvocationHandler, Serializable {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//并不是任何一个方法都需要执行调用代理对象进行执行,如果这个方法是Object中通用的方法(toString、hashCode等)无需执行
if (Object.class.equals(method.getDeclaringClass())) {
try {
return method.invoke(this, args);
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
//生成MapperMethod对象,从cache中获取
final MapperMethod mapperMethod = cachedMapperMethod(method);
//执行MapperMethod对象的execute方法,目标方法的执行
return mapperMethod.execute(sqlSession, args);
} private MapperMethod cachedMapperMethod(Method method) {
MapperMethod mapperMethod = methodCache.get(method);
if (mapperMethod == null) {
mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
methodCache.put(method, mapperMethod);
}
return mapperMethod;
} }

4. mapperMethod

  • 在初始化后就是向外提供的函数了,这个类向外提供服务主要是通过如下的函数进行:
public class MapperMethod {
public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
//根据初始化时确定的命令类型,选择对应的操作
if (SqlCommandType.INSERT == command.getType()) {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.insert(command.getName(), param));
} else if (SqlCommandType.UPDATE == command.getType()) {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.update(command.getName(), param));
} else if (SqlCommandType.DELETE == command.getType()) {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.delete(command.getName(), param));
} else if (SqlCommandType.SELECT == command.getType()) {
//相比较而言,查询稍微有些复杂,不同的返回结果类型有不同的处理方法
if (method.returnsVoid() && method.hasResultHandler()) {
executeWithResultHandler(sqlSession, args);
result = null;
} else if (method.returnsMany()) {
result = executeForMany(sqlSession, args);
} else if (method.returnsMap()) {
result = executeForMap(sqlSession, args);
} else {
Object param = method.convertArgsToSqlCommandParam(args);
//这个函数整体上还是调用sqlSession的各个函数进行相应的操作,在执行的过程中用到了初始化时的各个参数。
result = sqlSession.selectOne(command.getName(), param);
}
} else if (SqlCommandType.FLUSH == command.getType()) {
result = sqlSession.flushStatements();
} else {
throw new BindingException("Unknown execution method for: " + command.getName());
}
if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
throw new BindingException("Mapper method '" + command.getName()
+ " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
}
return result;
}
}

5 总结:

mybatis源码分析(4)----org.apache.ibatis.binding包的更多相关文章

  1. MyBatis源码分析(5)——内置DataSource实现

    @(MyBatis)[DataSource] MyBatis源码分析(5)--内置DataSource实现 MyBatis内置了两个DataSource的实现:UnpooledDataSource,该 ...

  2. MyBatis源码分析(4)—— Cache构建以及应用

    @(MyBatis)[Cache] MyBatis源码分析--Cache构建以及应用 SqlSession使用缓存流程 如果开启了二级缓存,而Executor会使用CachingExecutor来装饰 ...

  3. Mybatis源码分析-BaseExecutor

    根据前文Mybatis源码分析-SqlSessionTemplate的简单分析,对于SqlSession的CURD操作都需要经过Executor接口的update/query方法,本文将分析下Base ...

  4. MyBatis 源码分析系列文章导读

    1.本文速览 本篇文章是我为接下来的 MyBatis 源码分析系列文章写的一个导读文章.本篇文章从 MyBatis 是什么(what),为什么要使用(why),以及如何使用(how)等三个角度进行了说 ...

  5. Mybatis源码分析之Cache二级缓存原理 (五)

    一:Cache类的介绍 讲解缓存之前我们需要先了解一下Cache接口以及实现MyBatis定义了一个org.apache.ibatis.cache.Cache接口作为其Cache提供者的SPI(Ser ...

  6. MyBatis 源码分析

    MyBatis 运行过程 传统的 JDBC 编程查询数据库的代码和过程总结. 加载驱动. 创建连接,Connection 对象. 根据 Connection 创建 Statement 或者 Prepa ...

  7. (一) Mybatis源码分析-解析器模块

    Mybatis源码分析-解析器模块 原创-转载请说明出处 1. 解析器模块的作用 对XPath进行封装,为mybatis-config.xml配置文件以及映射文件提供支持 为处理动态 SQL 语句中的 ...

  8. 精尽 MyBatis 源码分析 - 基础支持层

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

  9. 精尽 MyBatis 源码分析 - MyBatis 初始化(一)之加载 mybatis-config.xml

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

  10. 精尽MyBatis源码分析 - MyBatis初始化(二)之加载Mapper接口与XML映射文件

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

随机推荐

  1. flask插件系列之SQLAlchemy基础使用

    sqlalchemy是一个操作关系型数据库的ORM工具.下面研究一下单独使用和其在flask框架中的使用方法. 直接使用sqlalchemy操作数据库 安装sqlalchemy pip install ...

  2. SQLite3 C/C++ 开发接口简介(API函数)

    from : http://www.sqlite.com.cn/MySqlite/5/251.Html 1.0 总览 SQLite3是SQLite一个全新的版本,它虽然是在SQLite 2.8.13的 ...

  3. 在Perl中使用Getopt::Long模块来接收用户命令行参数

    我们在linux常常用到一个程序需要加入参数,现在了解一下perl中的有关控制参数的函数.getopt.在linux有的参数有二种形式.一种是–help,另一种是-h.也就是-和–的分别.–表示完整参 ...

  4. elk系列3之通过json格式采集Nginx日志【转】

    转自 elk系列3之通过json格式采集Nginx日志 - 温柔易淡 - 博客园http://www.cnblogs.com/liaojiafa/p/6158245.html preface 公司采用 ...

  5. geoip 扩展包根据ip定位详情

    教程:https://laravel-china.org/courses/laravel-package/2024/get-the-corresponding-geo-location-informa ...

  6. npm install 装本地一直安装全局问题

    想用npm安装一些模块,不管怎么装,一直装作全局. 以为是node有问题,重装了N次,却还发现这个问题. 困惑几天无果, 偶然间通过此文章发现,npm存在配置文件:https://www.sitepo ...

  7. centos7 lvs+keepalived nat模式

    1.架构图   3.地址规划 主机名 内网ip 外网ip lvs-master 192.168.137.111(仅主机)eth1 172.16.76.111(桥接)eth0 lvs-slave 192 ...

  8. 删除/添加/调用WordPress用户个人资料的联系信息

    如果你要折腾主题或者将WordPress站点开放注册,你可能需要自定义WordPress用户个人资料信息.下面倡萌将简单说一下如何删除.添加和调用自定义用户信息字段. 添加或删除字段,可以在主题的 f ...

  9. 快速搭建sonar代码质量管理平台

    安装 下载,直接解压http://www.sonarqube.org/downloads/ 添加mysql驱动至\extensions\jdbc-driver\mysql\ 创建mysql数据库和用户 ...

  10. logstash收集java日志,多行合并成一行

    使用codec的multiline插件实现多行匹配,这是一个可以将多行进行合并的插件,而且可以使用what指定将匹配到的行与前面的行合并还是和后面的行合并. 1.java日志收集测试 input { ...