sheng的学习笔记-Mybatis原理和源码解析

MyBatis是一个优秀的持久层框架,简化了JDBC操作,支持自定义SQL和映射。它包含SqlSessionFactoryBuilder、SqlSessionFactory和SqlSession等核心组件,以及Executor执行器,如SimpleExecutor、ReuseExecutor和BatchExecutor。文章还详细介绍了MyBatis的缓存机制,包括一级缓存和二级缓存的工作原理。

目录

基础知识

什么是Mybatis?

Mybaits的优点:

架构设计

原理图

Mybatis基本构成

SqlSessionFactoryBuilder

SqlSessionFactory 

SqlSession

DefaultSqlSession源码-初始化

DefaultSqlSession源码-查询

 DefaultSqlSession源码-插入和更新

​编辑

 Executor执行器

SimpleExecutor

ReuseExecutor

BatchExecutor

源码分析

StatementHandler

ParameterHandler:

ResultSetHandler

动态代理

动态代理类的生成

bindMapperForNamespace()

Configuration#addMappper()

Configuration#getMapper()

动态代理的简图

缓存

 1.一级缓存:

2.二级缓存:


基础知识

什么是Mybatis?

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。

1、Mybatis是一个半ORM(对象关系映射)框架,底层封装了JDBC,是程序员在开发时只需要关注SQL语句本身,不需要花费精力去处理加载驱动、创建连接、创建statement等繁杂的过程。使得程序员可以花更多的精力放到业务开发中。另外,程序员直接编写原生态sql,严格控制sql执行性能,灵活度高。

2、MyBatis 可以使用简单的 XML文件 或注解方式来配置和映射原生信息,将 POJO映射成数据库中的记录,避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。

3、通过xml 文件或注解的方式将要执行的各种 statement 配置起来,并通过java对象和 statement中sql的动态参数进行映射生成最终执行的sql语句,最后由mybatis框架执行sql并将结果映射为java对象并返回。(从执行sql到返回result的过程)。

Mybaits的优点:

1、基于SQL语句编程,相当灵活,不会对应用程序或者数据库的现有设计造成任何影响,SQL写在XML里,解除sql与程序代码的耦合,便于统一管理;提供XML标签,支持编写动态SQL语句,并可重用。

2、与JDBC相比,减少了50%以上的代码量,消除了JDBC大量冗余的代码,不需要手动开关连接;

3、很好的与各种数据库兼容(因为MyBatis使用JDBC来连接数据库,所以只要JDBC支持的数据库MyBatis都支持)。

4、能够与Spring很好的集成;

5、提供映射标签,支持对象与数据库的ORM字段关系映射;提供对象关系映射标签,支持对象关系组件维护。

架构设计

我们把Mybatis的功能架构分为三层:

(1)API接口层:提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库。接口层一接收到调用请求就会调用数据处理层来完成具体的数据处理。

(2)数据处理层:负责具体的SQL查找、SQL解析、SQL执行和执行结果映射处理等。它主要的目的是根据调用的请求完成一次数据库操作。

(3)基础支撑层:负责最基础的功能支撑,包括连接管理、事务管理、配置加载和缓存处理,这些都是共用的东西,将他们抽取出来作为最基础的组件。为上层的数据处理层提供最基础的支撑。

 

  • SqlSession:作为MyBatis工作的主要顶层API,表示和数据库交互的会话,完成必要数据库增删改查功能
  • Executor:MyBatis执行器,是MyBatis 调度的核心,负责SQL语句的生成和查询缓存的维护
  • StatementHandler :封装了JDBC Statement操作,负责对JDBC statement 的操作,如设置参数、将Statement结果集转换成List集合。
  • ParameterHandler: 负责对用户传递的参数转换成JDBC Statement 所需要的参数,
  • ResultSetHandler:负责将JDBC返回的ResultSet结果集对象转换成List类型的集合;
  • TypeHandler :负责java数据类型和jdbc数据类型之间的映射和转换
  • MappedStatement: MappedStatement维护了一条<select|update|delete|insert>节点的封装,
  • SqlSource:负责根据用户传递的parameterObject,动态地生成SQL语句,将信息封装到BoundSql对象中,并返回
  • BoundSql :表示动态生成的SQL语句以及相应的参数信息
  • Configuration:MyBatis所有的配置信息都维持在Configuration对象之中。
     

原理图

  1.  读取 MyBatis 配置文件:mybatis-config.xml 为 MyBatis 的全局配置文件,配置了 MyBatis 的运行环境等信息,例如数据库连接信息。
  2. 加载映射文件:映射文件即 SQL 映射文件,该文件中配置了操作数据库的 SQL 语句,需要在 MyBatis 配置文件 mybatis-config.xml 中加载。mybatis-config.xml 文件可以加载多个映射文件,每个文件对应数据库中的一张表。
  3. 构造会话工厂:通过 MyBatis 的环境等配置信息构建会话工厂 SqlSessionFactory。
  4. 创建会话对象:由会话工厂创建 SqlSession 对象,该对象中包含了执行 SQL 语句的所有方法,是一个既可以发送sql执行并返回结果的,也可以获取mapper的接口
  5. Executor 执行器:MyBatis 底层定义了一个 Executor 接口来操作数据库,它将根据 SqlSession 传递的参数动态地生成需要执行的 SQL 语句,同时负责查询缓存的维护。
  6. MappedStatement 对象:在 Executor 接口的执行方法中有一个 MappedStatement 类型的参数,该参数是对映射信息的封装,用于存储要映射的 SQL 语句的 id、参数等信息。
  7. 输入参数映射:输入参数类型可以是 Map、List 等集合类型,也可以是基本数据类型和 POJO 类型。输入参数映射过程类似于 JDBC 对 preparedStatement 对象设置参数的过程。
  8. 输出结果映射:输出结果类型可以是 Map、 List 等集合类型,也可以是基本数据类型和 POJO 类型。输出结果映射过程类似于 JDBC 对结果集的解析过程。

Mybatis基本构成

下面源码是基于mybatis 3.5.2版本

SqlSessionFactoryBuilder

SqlSessionFactory 

作用:创建sqlSession,sqlSession是一个会话,相当于jdbc中的Connection对象

每次访问数据库就要通过SqlSessionFactory创建sqlSession,所以SqlSessionFactory会在mybatis整个生命周期都会使用
如果多次创建同一个数据库的SqlSessionFactory,每次创建SqlSessionFactory会打开更多的数据库连接资源,连接资源会被耗尽,所以这个采用单例模式。一个数据库只对应一个SqlSessionFactory

看下DefaultSqlSessionFactory代码,通过config生成sqlSession,默认是生成DefaultSqlSession

SqlSession

sqlSession提供基本API:增删改查,还有辅助API,也就是提交、关闭会话


【DefaultSqlSession】
是mybatis默认使用的实现类。这个类的线程是不安全的。所以每一个线程都要创建一个他自己有的sqlSession,所以不能把他设置成单例。
【SqlSessionManager】
线程安全
【SqlSessionTemplate】
SqlSessionTemplate是一个线程安全的类,当你运行一个sqlSessionTemplate时,会重新获取一个sqlSession,所有每一个方法都有独立的一个sqlSession,说明是线程安全的。

DefaultSqlSession源码-初始化

关注executor,通过这个来跟Statement交互的,executor是个接口

DefaultSqlSession源码-查询

思路:

通过statement作为ID,在configuration中的map找到对应的MappedStatement(提供统一的接口,在内部分发到不同处理逻辑,外观模式)

将statement作为参数,传到executor中,executor会调用statement的方法(命令模式)

这是configuration代码

 DefaultSqlSession源码-插入和更新

跟select 采用一样的思路,此处insert底层实际上调用的是update

 Executor执行器

是一个执行器。将java提供的sql提交到数据库,mybatis一共有三个执行器

SimpleExecutor

每执行一次 update 或 select,就开启一个 Statement 对象,用完立刻关闭 Statement 对象。
所以这种会特别浪费性能,不合适使用。

ReuseExecutor

可重用处理器,执行 update 或 select,以 sql 作为 key 查找 Statement 对象,存在就使用,不存在就创建,用完后,不关闭 Statement 对象,而是放置于 Map<String, Statement>内,供下一次使用。简言之,就是重复使用 Statement 对象

BatchExecutor

执行 update(没有 select,JDBC 批处理不支持 select),将所有 sql 都添加到批处理中(addBatch()),等待统一执行(executeBatch()),它缓存了多个 Statement 对象,每个 Statement 对象都是 addBatch()完毕后,等待逐一执行 executeBatch()批处理。与 JDBC 批处理相同。
作用范围:Executor的这些特点,都严格限制在SqlSession生命周期范围内

源码分析

到底用哪个执行器,可以自行配置,在DefaultSqlSessionFactory中生成sqlsession的时候,会根据execType来生成executor

 

 BaseExecutor是个抽象类,使用模板模式,要求子类实现一些关键方法,
接着上面select看,如果设置了simple模式,在query的时候走到BaseExecutor的query,最终是调用了子类的doquery方法(先查一下有没有缓存,没有缓存再查数据库),update也是一样的,用子类的doUpdate方法真正实现

BaseExecutor代码

 SimpleExecutor代码

用传过来的MappedStatement生成StatementHandler,通过StatementHandler查询sql

StatementHandler

封装了JDBC Statement操作,负责对JDBCstatement的操作,如设置参数、将Statement结果集转换成List集合,是真正访问数据库的地方,并调用ResultSetHandler处理查询结果

Mybatis中StatementHandler使用的是RoutingStatementHandler

RoutingStatementHandler构造方法,根据类型创建不同的代理类,这里会使用PreparedStatementHandler

 剩下的操作都是使用的代理类来操作

 在executor中,调用了prepare方法,

 ​​​​​​这个是在BaseStatementHandler中实现,主要是对statement做一些初始化的事情 

 调用了prepare之后,用JDBC执行SQL,然后对结果进行处理

ParameterHandler

负责对用户传递的参数转换成JDBC Statement 所需要的参数

ResultSetHandler

  • ParameterHandler 负责将用户传递的参数转换成JDBC Statement 所需要的参数
  • ResultSetHandler负责将JDBC返回的ResultSet结果集对象转换成List类型的集合;处理查询结果。
  • TypeHandler负责java数据类型和jdbc数据类型之间的映射和转换

TypeHandler 负责java数据类型和jdbc数据类型之间的映射和转换

SqlSource:负责根据用户传递的parameterObject,动态地生成SQL语句,将信息封装到BoundSql对象中,并返回

BoundSql :表示动态生成的SQL语句以及相应的参数信息

Configuration:MyBatis所有的配置信息都维持在Configuration对象之中。
 

动态代理

public static void main(String[] args) {
    try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);// <1>
        User user = userMapper.selectById(1);
        System.out.println("User : " + user);
    }
}

在前面的例子中,我们使用sqlSession.getMapper()方法获取UserMapper对象,实际上这里我们是获取了UserMapper接口的代理类,然后再由代理类执行方法。那么这个代理类是如何生成的呢?在探究动态代理类如何生成之前,我们先来看下SqlSessionFactory工厂的创建过程做了哪些准备工作,比如说mybatis-config配置文件是如何读取的,映射器文件是如何读取的

动态代理类的生成

bindMapperForNamespace()

该方法是核心方法,它会根据mapper文件中的namespace属性值,为接口生成动态代理类,这就来到了我们的主题内容——动态代理类是如何生成的

bindMapperForNamespace方法源码如下所示:

private void bindMapperForNamespace() {
    //获取mapper元素的namespace属性值
    String namespace = builderAssistant.getCurrentNamespace();
    if (namespace != null) {
      Class<?> boundType = null;
      try {
        // 获取namespace属性值对应的Class对象
        boundType = Resources.classForName(namespace);
      } catch (ClassNotFoundException e) {
        //如果没有这个类,则直接忽略,这是因为namespace属性值只需要唯一即可,并不一定对应一个XXXMapper接口
        //没有XXXMapper接口的时候,我们可以直接使用SqlSession来进行增删改查
      }
      if (boundType != null) {
        if (!configuration.hasMapper(boundType)) {
          // Spring may not know the real resource name so we set a flag
          // to prevent loading again this resource from the mapper interface
          // look at MapperAnnotationBuilder#loadXmlResource
          configuration.addLoadedResource("namespace:" + namespace);
          //如果namespace属性值有对应的Java类,调用Configuration的addMapper方法,将其添加到MapperRegistry中
          configuration.addMapper(boundType);
        }
      }
    }
  }

这里提到了Configuration的addMapper方法,实际上Configuration类里面通过MapperRegistry对象维护了所有要生成动态代理类的XxxMapper接口信息,可见Configuration类确实是相当重要一类

public class Configuration {
    ...
    protected MapperRegistry mapperRegistry = new MapperRegistry(this);
    ...
    public <T> void addMapper(Class<T> type) {
      mapperRegistry.addMapper(type);
    }
    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
      return mapperRegistry.getMapper(type, sqlSession);
    }
    ...
}

其中两个重要的方法:getMapper()和addMapper()

  • getMapper(): 用于创建接口的动态类
  • addMapper(): mybatis在解析配置文件时,会将需要生成动态代理类的接口注册到其中

Configuration#addMappper()

Configuration将addMapper方法委托给MapperRegistry的addMapper进行的,源码如下:

public <T> void addMapper(Class<T> type) {
    // 这个class必须是一个接口,因为是使用JDK动态代理,所以需要是接口,否则不会针对其生成动态代理
    if (type.isInterface()) {
      if (hasMapper(type)) {
        throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
      }
      boolean loadCompleted = false;
      try {
        // 生成一个MapperProxyFactory,用于之后生成动态代理类
        knownMappers.put(type, new MapperProxyFactory<>(type));
        //以下代码片段用于解析我们定义的XxxMapper接口里面使用的注解,这主要是处理不使用xml映射文件的情况
        MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
        parser.parse();
        loadCompleted = true;
      } finally {
        if (!loadCompleted) {
          knownMappers.remove(type);
        }
      }
    }
  }

MapperRegistry内部维护一个映射关系,每个接口对应一个MapperProxyFactory(生成动态代理工厂类)

private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>();

这样便于在后面调用MapperRegistry的getMapper()时,直接从Map中获取某个接口对应的动态代理工厂类,然后再利用工厂类针对其接口生成真正的动态代理类

Configuration#getMapper()

Configuration的getMapper()方法内部就是调用MapperRegistry的getMapper()方法,源代码如下:

 public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    //根据Class对象获取创建动态代理的工厂对象MapperProxyFactory
    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
    if (mapperProxyFactory == null) {
      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);
    }
  }

从上面可以看出,创建动态代理类的核心代码就是在MapperProxyFactory.newInstance方法中,源码如下:

protected T newInstance(MapperProxy<T> mapperProxy) {
    //这里使用JDK动态代理,通过Proxy.newProxyInstance生成动态代理类
    // newProxyInstance的参数:类加载器、接口类、InvocationHandler接口实现类
    // 动态代理可以将所有接口的调用重定向到调用处理器InvocationHandler,调用它的invoke方法
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }
 
  public T newInstance(SqlSession sqlSession) {
    final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }

这里的InvocationHandler接口的实现类是MapperProxy,其源码如下:

public class MapperProxy<T> implements InvocationHandler, Serializable {
 
  private static final long serialVersionUID = -6424540398559729838L;
  private final SqlSession sqlSession;
  private final Class<T> mapperInterface;
  private final Map<Method, MapperMethod> methodCache;
 
  public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
    this.sqlSession = sqlSession;
    this.mapperInterface = mapperInterface;
    this.methodCache = methodCache;
  }
 
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    
    try {
      //如果调用的是Object类中定义的方法,直接通过反射调用即可
      if (Object.class.equals(method.getDeclaringClass())) {
        return method.invoke(this, args);
      } else if (isDefaultMethod(method)) {
        return invokeDefaultMethod(proxy, method, args);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
    //调用XxxMapper接口自定义的方法,进行代理
    //首先将当前被调用的方法Method构造成一个MapperMethod对象,然后掉用其execute方法真正的开始执行。
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    return mapperMethod.execute(sqlSession, args);
  }
  private MapperMethod cachedMapperMethod(Method method) {
    return methodCache.computeIfAbsent(method, k -> new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));
  }
  ...
}

最终的执行逻辑在于MapperMethod类的execute方法,源码如下:

public class MapperMethod {
 
  private final SqlCommand command;
  private final MethodSignature method;
 
  public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
    this.command = new SqlCommand(config, mapperInterface, method);
    this.method = new MethodSignature(config, mapperInterface, method);
  }
  public Object execute(SqlSession sqlSession, Object[] args) {
    Object result;
    switch (command.getType()) {
      //insert语句的处理逻辑
      case INSERT: {
        Object param = method.convertArgsToSqlCommandParam(args);
        result = rowCountResult(sqlSession.insert(command.getName(), param));
        break;
      }
      //update语句的处理逻辑
      case UPDATE: {
        Object param = method.convertArgsToSqlCommandParam(args);
        result = rowCountResult(sqlSession.update(command.getName(), param));
        break;
      }
      //delete语句的处理逻辑
      case DELETE: {
        Object param = method.convertArgsToSqlCommandParam(args);
        result = rowCountResult(sqlSession.delete(command.getName(), param));
        break;
      }
      //select语句的处理逻辑
      case SELECT:
        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 if (method.returnsCursor()) {
          result = executeForCursor(sqlSession, args);
        } else {
          Object param = method.convertArgsToSqlCommandParam(args);
          //调用sqlSession的selectOne方法
          result = sqlSession.selectOne(command.getName(), param);
          if (method.returnsOptional()
              && (result == null || !method.getReturnType().equals(result.getClass()))) {
            result = Optional.ofNullable(result);
          }
        }
        break;
      case FLUSH:
        result = sqlSession.flushStatements();
        break;
      default:
        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;
  }
  ...
}

在MapperMethod中还有两个内部类,SqlCommand和MethodSignature类,在execute方法中首先用switch case语句根据SqlCommand的getType()方法,判断要执行的sql类型,比如INSET、UPDATE、DELETE、SELECT和FLUSH,然后分别调用SqlSession的增删改查等方法。

我们调用SqlSession的getMapper()方法

UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
 
public class DefaultSqlSession implements SqlSession {
 
  private final Configuration configuration;
  private final Executor executor;
  @Override
  public <T> T getMapper(Class<T> type) {
    return configuration.getMapper(type, this);
  }
  ...
}

所以getMapper方法的大致调用逻辑链是:
SqlSession#getMapper() ——> Configuration#getMapper() ——> MapperRegistry#getMapper() ——> MapperProxyFactory#newInstance() ——> Proxy#newProxyInstance()

动态代理的简图

缓存

 1.一级缓存:

是基于数据库会话的,并且默认开启。一级缓存的作用域为SqlSession。在同一个SqlSession中,执行相同的sql语句,那么第一次就会去数据库中进行查询,并写到缓存中,如果我们后面还想去访问数据库查询,就直接去一级缓存中获取就可以了。

2.二级缓存:

是基于全局的,不能默认开启,开启时需要手动配置。二级缓存的作用域为SqlSessionFactory,是一个映射器级别的缓存,针对不同namespace的映射器。一个会话中,查询一条数据,这个数据会被放到一级缓存中,但是一旦这个会话关闭,一级缓存汇总的数据就会被保存到二级缓存。新的会话查询信息就会参照二级缓存中存储的信息。
 

参考文章:

百度安全验证

Mybatis的工作原理_make_888的博客-优快云博客_mybatis原理

MyBatis动态代理原理_fedorafrog的博客-优快云博客

百度安全验证

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值