zoukankan      html  css  js  c++  java
  • MyBatis原理-架构流程

    一 、MyBatis原理架构图 

     

     

    Mybatis的功能架构分为三层:

    • API接口层:提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库。接口层一接收到调用请求就会调用数据处理层来完成具体的数据处理。
    • 数据处理层:负责具体的SQL查找、SQL解析、SQL执行和执行结果映射处理等。它主要的目的是根据调用的请求完成一次数据库操作。
    • 基础支撑层:负责最基础的功能支撑,包括连接管理、事务管理、配置加载和缓存处理,这些都是共用的东西,将他们抽取出来作为最基础的组件。为上层的数据处理层提供最基础的支撑。

    二、MyBatis工作流程

     

     

    1、 mybatis配置SqlMapConfig.xml,此文件作为mybatis的全局配置文件,配置了mybatis的运行环境等信息。
    mapper.xml文件即sql映射文件,文件中配置了操作数据库的sql语句。此文件需要在SqlMapConfig.xml中加载。

    2、 通过mybatis环境等配置信息构造SqlSessionFactory即会话工厂

    3、 由会话工厂创建sqlSession即会话,操作数据库需要通过sqlSession进行。

    4、 mybatis底层自定义了Executor执行器接口操作数据库(CURD),Executor接口有两个实现,一个是基本执行器、一个是缓存执行器。Executor才是真正将sql与Connection打交道的地方。在真正执行查询时,外面是嵌套了一层CachingExecutor

    首先,最底层的接口是Executor,有两个实现类:BaseExecutor和CachingExecutor,CachingExecutor用于二级缓存,而BaseExecutor则用于一级缓存及基础的操作。并且由于BaseExecutor是一个抽象类,提供了三个实现:SimpleExecutor,BatchExecutor,ReuseExecutor,而具体使用哪一个Executor则是可以在mybatis-config.xml中进行配置的。配置如下:
    <settings>
        <!--SIMPLE、REUSE、BATCH-->
        <setting name="defaultExecutorType" value="REUSE"/>
    </settings>

    不开启二级缓存

    <settings>
            <setting name="cacheEnabled" value="false"/>
    </settings>

    通过configuration.newExecutor来获取Executor

    public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
        executorType = executorType == null ? defaultExecutorType : executorType;
        executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
        Executor executor;
        // 根据不同的executorType来创建
        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);
        }
        // cacheEnabled 也就是我们配置的二级缓存,如果该值配置为true,则获取的是CachingExecutor
        if (cacheEnabled) {
          executor = new CachingExecutor(executor);
        }
        // 这里是通过责任链模式来生成代理对象
        executor = (Executor) interceptorChain.pluginAll(executor);
        return executor;
    }
    

      

    5、 Mapped Statement也是mybatis一个底层封装对象,它包装了mybatis配置信息及sql映射信息等。mapper.xml文件中一个sql对应一个Mapped Statement对象,sql的id即是Mapped statement的id(mapper.xml文件中的namespacce+id)。

    6、 Mapped Statement对sql执行输入参数进行定义,包括HashMap、基本类型、pojo,Executor通过 Mapped Statement在执行sql前将输入的java对象映射至sql中,输入参数映射就是jdbc编程中对preparedStatement设置参数。

    7、 Mapped Statement对sql执行输出结果进行定义,包括HashMap、基本类型、pojo,Executor通过 Mapped Statement在执行sql后将输出结果映射至java对象中,输出结果映射过程相当于jdbc编程中对结果的解析处理过程。

  • 相关阅读:
    Building Seam 2.0 Application with NetBeans 6.1
    Better Builds with Maven学习笔记
    NetBeans Globel Translation Team Tshirt!
    Participate in MySQLGlassFish Student Contest and Win $500
    NetBeans Globel Translation Team Tshirt!
    Better Builds with Maven学习笔记
    Building Seam 2.0 Application with NetBeans 6.1
    Maven2 的新特性
    Participate in MySQLGlassFish Student Contest and Win $500
    数据库设计及数据缓存
  • 原文地址:https://www.cnblogs.com/harvey2017/p/9577163.html
Copyright © 2011-2022 走看看