Mybatis的架构设计

Mybatis总共把功能架构分成了四部分

1. 接口层

提供给外部使用的接口API,开发人员通过这些本地API来操纵数据库,接口层一接受到调用请求就会调用数据处理层来完成具体的数据处理,Mybatis提供两种方式

传统的方式

以下为SqlSession接口中提供的接口方法

  /**
   * Retrieve a single row mapped from the statement key and parameter.
   * @param <T> the returned object type
   * @param statement Unique identifier matching the statement to use.
   * @param parameter A parameter object to pass to the statement.
   * @return Mapped object
   */
  <T> T selectOne(String statement, Object parameter);
/**
   * Retrieve a list of mapped objects from the statement key and parameter.
   * @param <E> the returned list element type
   * @param statement Unique identifier matching the statement to use.
   * @param parameter A parameter object to pass to the statement.
   * @return List of mapped object
   */
  <E> List<E> selectList(String statement, Object parameter);
 /**
   * Execute an insert statement with the given parameter object. Any generated
   * autoincrement values or selectKey entries will modify the given parameter
   * object properties. Only the number of rows affected will be returned.
   * @param statement Unique identifier matching the statement to execute.
   * @param parameter A parameter object to pass to the statement.
   * @return int The number of rows affected by the insert.
   */
  int insert(String statement, Object parameter);
 /**
   * Execute an update statement. The number of rows affected will be returned.
   * @param statement Unique identifier matching the statement to execute.
   * @param parameter A parameter object to pass to the statement.
   * @return int The number of rows affected by the update.
   */
  int update(String statement, Object parameter);
 /**
   * Execute a delete statement. The number of rows affected will be returned.
   * @param statement Unique identifier matching the statement to execute.
   * @param parameter A parameter object to pass to the statement.
   * @return int The number of rows affected by the delete.
   */
  int delete(String statement, Object parameter);
 // 此处省略一万字....

Mapper代理的方式

Mapper代理的方式实际上就是通过创建接口(UserMapper接口)的代理对象调用接口中的方法,最终执行方法调用时Executor对象,底层还是调用传统的API方法。

2. 数据处理层

数据处理层主要是JDBC的核心内容,包括参数映射,类型转换,SQL解析,SQL执行以及结果的封装,这部分内容就可以理解成JDBC的核心内容,这里有几个重要类

  • ParameterHandler:SQL的参数处理
  • ResultSetHandler:结果处理集
  • StatementHandler:封装了JDBC Statement操作,设置参数,转换结果集
  • Executor:Mybatis的执行器,用于执行增删改查操作
3. 框架支撑层

这部分主要是框架抽取出来的通用组件包括数据源管理、事务管理、配置加载和缓存处理;为上层的数据处理层提供最基础的支撑

  • 数据源管理

    数据源的管理对于持久层的框架来说,可谓是非常的重要稍有不慎就会浪费大量系统资源,Mybatis也对此做了很好的支持,Mybatis有三种内建的数据源类型 UNPOOLED、POOLED、JNDI

    • UNPOOLED:每次请求时打开和关闭链接
    • POOLED:利用“池”的概念将JDBC链接对象组织起来,避免了创建新的连接实例时所必须的初始化和认证时间
    • JNDI:为了能在如EJB或应用服务器这类容器中使用,容器可以集中或在外部配置数据源、然后放置一个JNDI上下文的数据源引用。

    通常情况下,我们是会使用POOLED的,可以帮我们节省很多宝贵的系统资源

  • 事务管理(transactionManager)
    在Mybatis中有两种类型的事务管理器(JDBC/MANAGED)它通过一个顶层的Transaction接口以及其不同实现JdbcTransaction和ManagedTransaction

    • JDBC 这个配置直接使用了JDBC的提交和回滚设施,它依赖从数据源获得的连接来管理事务作用域,对应的实现类 JdbcTransaction
    • MANAGED 这个配置几乎没做什么。它从不提交或回滚一个连接,而是让容器来管理事务的整个生命周期(比如J2EE应用服务器的上下文,对应的实现类ManagedTransaction)

    对事务的管理,在和Spring整合之后,通常会用Spring的事务管理器

  • 缓存处理
    缓存对于持久层框架来说还是非常重要的,一定程度上可以减少和数据库的交互,Mybatis提供了两种缓存机制,一级缓存和二级缓存

    • 一级缓存 是SqlSession级别的缓存,也就是会话级别的,如果两个相同的查询,第二次的查询会直接先从缓存中去拿,一级缓存也是默认开启的。
    • 二级缓存 是Mapper级别的缓存,也就是xxx.xml内的查询是可以公用的,需要手动开启
  • SQL解析
    MyBatis支持两种SQL解析的方式,一种xml、一种是注解;两种方式对于一些基础的CRUD区别不大,使用注解来映射简单语句会使代码显得更加简洁,但对于稍微复杂一点的语句,注解不仅力不从心,还会让你本就复杂的SQL语句更加混乱不堪。因此,如果你需要做一些很复杂的操作,最好用 XML 来映射语句

4. 引导层

这部分是MyBatis启动时核心配置文件的方式,严格来说也可以不算在架构层面,不过MyBatis也是提供了两种方式一个是xml的方式,也是用的比较多的,一种是使用Java API

  • XML的方式

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    <environments default="development">
    <environment id="development">
    <!--省略-->
    </environment>
    </environments>
    </configuration>
  • Java API的方式

    Environment environment = new Environment("development", transactionFactory, dataSource);
    Configuration configuration = new Configuration(environment);

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注