摘要

本篇文章只是个人阅读mybatis源码总结的经验或者个人理解mybatis的基本轮廓,作为抛砖引玉的功能,希望对你有帮助,如果需要深入了解细节还需亲自去阅读源码。

mybatis基本架构

mybatis的源码应该算是比较容易阅读的,首先mybatis核心功能就是执行Sql语句,但在其基础上又有许多增强的地方(动态Sql,ORM等)。看一个框架的时候,第一步是对整个框架有一个大体的了解。例如mybatis,我们可以从初始化到完成一个sql请求为主线,看一下涉及了哪些类。我个人总结了一下,mybatis的框架主要的核心类有4个

Configuration

Configuration就是用于解析、保存、处理Mybatis的配置内容,包括了

  • mybatis基本配置,例如支持数据库中的字段支持下标转驼峰mapUnderscoreToCamelCase=true等等,参看Mybatis配置说明
  • SqlMapper管理,也就是通过xml或者注解写的一些sql映射。相关的类可以查看源码中MappedStatement类。
  • 创建类,Configuration还有一些创建类的功能,例如Executor、StatementHandler。这个2个类后面还会说到

小节Configuration

总结Configuration的功能,当然,如何读取和解析相关文件是Configuration中大部分代码做的事。这些都是为了准备后面mybatis运行的基本条件。Configuration中创建类是因为创建的这些类都依赖于Configuration(但这样做数据和逻辑没有做到分离)。

SqlSession

SqlSession可能是mybatis中我们最常用的类,其实他是一个门面类,直接对外提供服务

1
2
3
4
5
6
7
8
9
public interface SqlSession extends Closeable {
 <T> T selectOne(String statement);
 <E> List<E> selectList(String statement, Object parameter);
 int delete(String statement);
 void rollback();
 void commit();
 ...
 
}

这些方法都是直接提供给外部调用的。看到这些方法是不是很亲切。(我个人在看源码的时候看到一些自己用过的一些类或方法的时候都有种莫名的亲近感。感觉终于和我的认知世界有交集了)

SqlSession的创建

SqlSessionFactor是用于创建SqlSession建造者,提供给外部快速创建一个SqlSession。是一个工厂类,而SqlSessionFactor的创建则是由SqlSessionFactorBuilder。

Executor

前面说了SqlSession只是一个门面类,Executor才是负责Sql语句执行的。因此Executor才是整个mybatis核心。Executor的实现类有

  • BaseExecutor:看名字知道是最基础Executor,其他的Executor都和这个类有一定的关系
  • CachingExecutor:每次查询的时候会先从缓存中获取,每次有增删改的时候会让缓存失效。CachingExecutor其实是一个代理内,内部代理了BaseExecutor(或其子类)。在BaseExecutor基础上增加了缓存操作。

相关类

我们看一个Executor参数最多的一个方法

1
<E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException;

这些类都对执行Sql有一定关系

MappedStatement

具体点来理解就是我们定义的Sql映射语句,例如我们xml定义的:

1
2
3
4
<select id="selectCountByPath" parameterType="java.lang.String" resultType="java.lang.Long">
 select count(1) FROM config
 WHERE path = #{path}
</select>

paramter

这个就是传递给sql映射的参数,用于生成和填充动态sql语句

RowBound

限定一次查询数据量,类很简单,看代码就明白,不多说

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class RowBounds {
 public static final int NO_ROW_OFFSET = 0;
 public static final int NO_ROW_LIMIT = Integer.MAX_VALUE;
 public static final RowBounds DEFAULT = new RowBounds();
 private int offset;
 private int limit;
 public RowBounds() {
 this.offset = NO_ROW_OFFSET;
 this.limit = NO_ROW_LIMIT;
 }
 public RowBounds(int offset, int limit) {
 this.offset = offset;
 this.limit = limit;
 }
}

ResultHandler

这个和本地缓存有关,用于保存一个查询语句的缓存对象,下次有相同的查询语句的时候就会先尝试从本地缓存中获取。 注意:

,mybatis有2级缓存,第一级是CachingExecutor,第二级缓存就是mybatis的本地缓存,也就是和ResultHandler

缓存失效策略是和一级缓存一样,任何增删改都会清空本地缓存

CacheKey

一个查询语句的在本地缓存中的key,根据sql语句,参数等等组成

BoundSql

这个对象就是本次实际需要执行的sql语句有关的信息,

1
2
3
4
5
6
7
public class BoundSql {
 private String sql;
 private List<ParameterMapping> parameterMappings;
 private Object parameterObject;
 private Map<String, Object> additionalParameters;
 private MetaObject metaParameters;
 ...

如果说parameter参数是实际传入的参数,那么BoundSql就是根据传入参数进行相关解析后的结果。他的创建在MappedStatement中,根据parameter和当前执行MappedStatement生成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public BoundSql getBoundSql(Object parameterObject) {
 BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
 List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
 if (parameterMappings == null || parameterMappings.isEmpty()) {
  boundSql = new BoundSql(configuration, boundSql.getSql(), parameterMap.getParameterMappings(), parameterObject);
 }
 // check for nested result maps in parameter mappings (issue #30)
 for (ParameterMapping pm : boundSql.getParameterMappings()) {
  String rmId = pm.getResultMapId();
  if (rmId != null) {
  ResultMap rm = configuration.getResultMap(rmId);
  if (rm != null) {
   hasNestedResultMaps |= rm.hasNestedResultMaps();
  }
  }
 }
 return boundSql;
}

Interceptor

Mybatis提供了Interceptor用于在执行Executor之前进行一些操作,mybatis是怎么使用Interceptor。其实就是在创建Executor时候,会

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
 executorType = executorType == null ? defaultExecutorType : executorType;
 executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
 Executor executor;
 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);
 }
 if (cacheEnabled) {
  executor = new CachingExecutor(executor);
 }
 //看这里!!!
 executor = (Executor) interceptorChain.pluginAll(executor);
 return executor;
 }

这里主要是通过jdk动态代理实现的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Plugin implements InvocationHandler {
 ...
 public static Object wrap(Object target, Interceptor interceptor) {
 Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
 Class<?> type = target.getClass();
 Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
 if (interfaces.length > 0) {
  return Proxy.newProxyInstance(
   type.getClassLoader(),
   interfaces,
   new Plugin(target, interceptor, signatureMap));
 }
 return target;
 }
  
 ...
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 try {
  Set<Method> methods = signatureMap.get(method.getDeclaringClass());
  if (methods != null && methods.contains(method)) {
  return interceptor.intercept(new Invocation(target, method, args));
  }
  return method.invoke(target, args);
 } catch (Exception e) {
  throw ExceptionUtil.unwrapThrowable(e);
 }
 }

这样在调用Executor的时候就会先判断是否满足Interceptor的执行条件,满足则会先执行Intercepter#intercept()方法

最底层的Handler

要说直接和Jdbc打交道的就是各种Handler类,例如

  • StatementHandler: 处理java.sql.Statement
  • ParameterHandler: 向PreparedStatement中设置参数
  • ResultSetHandler:处理sql执行结果,并转换成指定的类对象 上面的这些其实都不复杂,所以代码还是比较好理解的

Transaction

每个Executor生成的时候都会把Transaction传入,在BaseExecutor中Transaction是其成员变量,那Transaction的作用是什么呢?

1
2
3
4
5
6
7
public interface Transaction {
 Connection getConnection() throws SQLException;
 void commit() throws SQLException;
 void rollback() throws SQLException;
 void close() throws SQLException;
 Integer getTimeout() throws SQLException;
}

其实之前一直都没提到过Connect谁来管理,这里可以看出来,Transaction负责了Connection的获取,以及对这次Connect的提交和回滚等操作。这个类也是比较好理解的。Executor的commit或者rollback最后都是调用Transaction的

总结

可以看出,mybatis的源码是比较容易阅读的(相对于Spring等)。上面介绍了框架中的一些核心类,但是很多细节的地方值得我们去深挖。这个就需要我们能沉下来好好阅读代码。

Mybatis框架及原理实例分析的更多相关文章

  1. MyBatis框架及原理分析

    MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架,其主要就完成2件事情: 封装JDBC操作 利用反射打通Java类与SQL语句之间的相互转换 MyBatis的主要设计目的就 ...

  2. Spring3.0 与 MyBatis框架 整合小实例

    本文将在Eclipse开发环境下,采用Spring MVC + Spring + MyBatis + Maven + Log4J 框架搭建一个Java web 项目. 1. 环境准备: 1.1 创建数 ...

  3. mybatis(一、原理,一对多,多对一查询)

    MyBatis框架及原理分析 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架,其主要就完成2件事情: 封装JDBC操作 利用反射打通Java类与SQL语句之间的相互转换 ...

  4. 深入详解Mybatis的架构原理与6大核心流程

    MyBatis 是 Java 生态中非常著名的一款 ORM 框架,目前在一线互联网大厂中应用广泛,Mybatis已经成为了一个必会框架. 如果你想要进入一线大厂,能够熟练使用 MyBatis 开发已经 ...

  5. 《深入理解mybatis原理》 MyBatis的架构设计以及实例分析

    作者博客:http://blog.csdn.net/u010349169/article/category/2309433 MyBatis是目前非常流行的ORM框架,它的功能很强大,然而其实现却比较简 ...

  6. MyBatis的深入原理分析之1-架构设计以及实例分析

    MyBatis是目前非常流行的ORM框架,它的功能很强大,然而其实现却比较简单.优雅.本文主要讲述MyBatis的架构设计思路,并且讨论MyBatis的几个核心部件,然后结合一个select查询实例, ...

  7. 《深入理解mybatis原理1》 MyBatis的架构设计以及实例分析

    <深入理解mybatis原理> MyBatis的架构设计以及实例分析 MyBatis是目前非常流行的ORM框架,它的功能很强大,然而其实现却比较简单.优雅.本文主要讲述MyBatis的架构 ...

  8. Mybatis实现简单的CRUD(增删改查)原理及实例分析

    Mybatis实现简单的CRUD(增删改查) 用到的数据库: CREATE DATABASE `mybatis`; USE `mybatis`; DROP TABLE IF EXISTS `user` ...

  9. MyBatis的架构设计以及实例分析--转

    原文地址:http://blog.csdn.net/luanlouis/article/details/40422941 MyBatis是目前非常流行的ORM框架,它的功能很强大,然而其实现却比较简单 ...

随机推荐

  1. CentOS 8 已经不再支持,Rocky Linux 才是未来

    2020年12月8日,红帽公司宣布,他们将停止开发CentOS,而在此之前CentOS一直作为红帽企业Linux的生产型分支及下游版本,此后他们将转而开发该操作系统的一个更新的上游开发变种,即 &qu ...

  2. 「AGC021E」Ball Eat Chameleons

    「AGC021E」Ball Eat Chameleons 考虑如何判定一个合法的颜色序列. 不妨设颜色序列中有 \(R\) 个红球,\(B\) 个蓝球,所以有 \(R+B=k\). 考虑分情况讨论: ...

  3. 检测当前手机正在运行的APP

    import re #一定要引入,否则不提示错误,但找不到目标def jiance(sjh): aakk="adb -s {0} shell dumpsys activity activit ...

  4. 【LeetCode】12. 整数转罗马数字

    12. 整数转罗马数字 知识点:字符串 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 100 ...

  5. python+API接口测试框架设计(unittest)

    1.测试框架简介 整个接口测试框架的设计图如下: basepage:存放的是公共的方法 common:存放的是自定义工具 data:存放的是公共动态数据,如BB.xls/ Id.md log:存放的是 ...

  6. python -- 程序异常与调试(程序调试)

    一.程序调试 A.使用assert语句检测程序代码中的错误. assert 表达式[, 参数] 如果表达式为True,则继续往下运行:如果为False,则抛出一个AssertionError异常,并且 ...

  7. C++ Primer Plus 第四章 复合类型 学习笔记

    第四章 复合类型 1. 数组概述 1.1 数组的定义 数组(array)是一种数据格式,能够存储多个同类型的值.每个值都存储在一个独立的数组元素中,计算机在内存中依次存储数组的各个元素. 数组声明的三 ...

  8. 在docker for windows建立mssql容器后,ssms连接mssql出现错误号码18456的问题

    在docker for windows建立mssql容器后,ssms连接mssql出现错误号码18456的问题 笔者提供一个可能会没考虑到的点. 请检查本机是否安装了mssql!!! 请检查本机的ms ...

  9. innodb引擎相关参数

    1.innodb_flush_log_at_trx_commit (双一标准之一) 作用:主要控制了innodb将log buffer中的数据写入日志文件并flush磁盘的时间点,取值分别为0.1.2 ...

  10. ifix历史数据(H04/H08/H24)转换为CSV文件导出

    在最近的一次环保数据维护中,由于自己疏忽导致数据库中TP值并未有效记录,还好历史趋势有相关记录,问题是我该如何将.H24文件记录导出?在逛论坛后,无意发现一款工具解决了我的燃眉之急-HTD2CSV.e ...