Mybatis底层框架运用的设计模式解析

我们都知道怎么用mybatis,但是却不了解运用的代码它底层的含义到底是什么,比如当我们在搭建Mybatis的时候,只是知道怎么运用SqlSessionFactoryBuilder、SqlSessionFactory、SqlSession,但是却不知道他们的底层设计原理到底是什么,他们到底里面包含了哪些接口,到底是如何来创建这些接口的呢

运用到的设计模式

1)工厂模式
2)建造者模式(Builder)
3)单例模式
4)适配器模式
5)代理模式
6)模板方法模式
7)装饰器模式

各个设计模式的简介

1.工厂模式

简单工厂模式
组成要素

  • product:工厂方法所创建的对象的接口,这也就是实际需要使用对象的接口
  • ConcreteProduct:具体的产品,具体的product接口实现的对象
  • Factory:这个指的就是工厂生产产品的接口,通常返回一个product类型的实例对象
  • ConcreteFactory:这个就是直接返回一个工厂对象,返回一个具体的product实例

在这里插入图片描述

工厂模式大家一定都很熟悉,那就是说当有一个客户需要调用的时候,只需要调用这个工厂类就可以得到想要的结果,从而无需关注具体某类的实现过程。
工厂模式中的典型代表就是SqlsessionFactory,而SqlSession就是由这个SqlSessionFactory来产生的,其中需要解释的就是SqlSession是和数据库进行交流的,因此它的作用当中需要完成的就是关于mybatis的环境进行的配置,比如下面的opession当中对应的配置就包括了下图当中的一些东西在里面
在这里插入图片描述

工厂模式应用解析
在这当中,SqlSessionFactory是一个接口类,它的子类有一个 Opensession(ExecutorType execType)的方法,其中使用了工厂模式

private SqlSession openSessionFromDataSource(ExecutorType execType,TransactionIsolationLevel level,boolean autoCommit){
Transaction tx=null;
try{
final Environment environment=configuration.getEnvironment();
final TransactionFactory transactionFactory=getTransactionFactoryFromEnvironment(environment);
//这个Transaction指的就是事务管理器的类型,在这当中,需要明白的就是要和environment相区别开来,因为当中对应的东西是数据库当中的环境的配置,配置完环境之后需要你做的就是事务的一个管理,这个事务的管理就是代表的是提交、回滚、开启。
tx=transactionFactory.newTransaction(environment.getDataSource(),level,autoCommit);
//这里会创建不同的执行器
final Executor executor=configuration.newExecutor(tx,execType);
return new DefaultSqlSession(configuration,executor,autoCommit);
}catch(Exception e){
closeTransaction(tx);
throw ExceptionFactory.wrapException("Error opening eseeion. Cause:"+ e,e);
}finally{
ErroContext.instance().reset();
}
}



在当中我们发现有一个Configuration.newExecutor(tx,execType)读取对应的环境配置,而此方法的源代码如下所示

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;
}

由上图代码可以看出这个newExecutor()方法是一个标准的工厂模式,它可以通过传递ExecutorType生成相应的对象然后进行返回。

2.建造者模式(Builder)

组成元素:

  • 产品角色(Product):它是包含多个组成部件的复杂对象,由具体建造者来创建其各个部件
  • 抽象建造者(Builder):它是一个包含创建产品各个部件的抽象方法的接口,通常还包含一个返回复杂产品的方法 getResult()
  • 具体建造者(Concre Builder):实现Builder接口,完成复杂产品的各个部件的具体创建方法
  • 指挥者(Director):它调用建造者对象中的部件构造与装配方法完成复杂对象的创建,在指挥者中不涉及具体产品的信息
    在这里插入图片描述

在这当中,你需要做的就是进行通过XMLConfigBuilder对象创建读取并解析XML的配置文件,然后再将读取到的配置信息存入到Configuration类中,然后再通过build方法生成我们需要的DefaultSqlSessionFactory对象,实现源码如下

// SqlSessionFactoryBuilder 构造 SqlSessionFactory 
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
    try {
        XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
        return build(parser.parse());
    } catch (Exception e) {
        throw ExceptionFactory.wrapException("Error building SqlSession.", e);
    } finally {
        ErrorContext.instance().reset();
        try {
        inputStream.close();
        } catch (IOException e) {
        // Intentionally ignore. Prefer previous error.
        }
    }
}
public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
}

3.单列模式

在Myabatis当中典型的例子就是ErrorContext
ErrorContext是线程级别的单列,每个线程中有一个此对象的单列,用于记录该线程的执行环境的错误信息。
ErrorContext的源码如下:

public class ErrorContext {
  private static final String LINE_SEPARATOR = System.lineSeparator();
  // 每个线程存储的容器
  private static final ThreadLocal<ErrorContext> LOCAL = ThreadLocal.withInitial(ErrorContext::new);
  public static ErrorContext instance() {
    return LOCAL.get();
  }
  // 忽略其他
}

4.适配器模式

组成要素:

  • 目标角色:这是客户锁期待的接口。目标可以是具体的或抽象的类,也可以是接口
  • 适配者角色:已有接口,但是和客户器期待的接口不兼容
  • 适配器角色:将已有接口转换成目标接口

在这里插入图片描述

首先我们定义了一个关于log的接口,然后在这当中对这些方法进行了编写,那就是

public interface Log {
  boolean isDebugEnabled();
  boolean isTraceEnabled();
  void error(String s, Throwable e);
  void error(String s);
  void debug(String s);
  void trace(String s);
  void warn(String s);
}

以log4j2实现源码解析来进行的一个分析可以看到

public class Log4j2Impl implements Log {
  private final Log log;
  public Log4j2Impl(String clazz) {
    Logger logger = LogManager.getLogger(clazz);
    if (logger instanceof AbstractLogger) {
      log = new Log4j2AbstractLoggerImpl((AbstractLogger) logger);
    } else {
      log = new Log4j2LoggerImpl(logger);
    }
  }
  @Override
  public boolean isDebugEnabled() {
    return log.isDebugEnabled();
  }
  @Override
  public boolean isTraceEnabled() {
    return log.isTraceEnabled();
  }
  @Override
  public void error(String s, Throwable e) {
    log.error(s, e);
  }
  @Override
  public void error(String s) {
    log.error(s);
  }
  @Override
  public void debug(String s) {
    log.debug(s);
  }
  @Override
  public void trace(String s) {
    log.trace(s);
  }
  @Override
  public void warn(String s) {
    log.warn(s);
  }
}

5、代理模式
代理模式指的是给某一个对象提供一个代理对象,并由代理对象控制原对象的调用。代理模式在 MyBatis 中的典型代表是 MapperProxyFactory。

MapperProxyFactory 的 newInstance() 方法就是生成一个具体的代理来实现功能的,源码如下:

public class MapperProxyFactory<T> {
  private final Class<T> mapperInterface;
  private final Map<Method, MapperMethodInvoker> methodCache = new ConcurrentHashMap<>();
  public MapperProxyFactory(Class<T> mapperInterface) {
    this.mapperInterface = mapperInterface;
  }
  public Class<T> getMapperInterface() {
    return mapperInterface;
  }
  public Map<Method, MapperMethodInvoker> getMethodCache() {
    return methodCache;
  }
  // 创建代理类
  @SuppressWarnings("unchecked")
  protected T newInstance(MapperProxy<T> mapperProxy) {
    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);
  }
}

6、模板方法模式。
模板方法模式是最常用的设计模式之一,它是指定义一个操作算法的骨架,而将一些步骤的实现延迟到子类中去实现,使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。此模式是基于继承的思想实现代码复用的。
模板方法在 MyBatis 中的典型代表是 BaseExecutor。
在 MyBatis 中 BaseExecutor 实现了大部分 SQL 执行的逻辑,然后再把几个方法交给子类来实现,它的继承关系如下图所示:
在这里插入图片描述

7、装饰器模式。
组成部分:
在这里插入图片描述

Component 定义一个对象的接口,定义了该对象的职责,也是装饰类和被装饰类的基本类型
ConcreteComponent 是Component 借口的具体实现类,为被装饰类
Decorator 是装饰类,继承Component接口,包含 Component接口实例
ConcreteDecoratorA,ConcreteDecoratorB
是ConcreteComponent的派生类,扩展了Decorator的职能
装饰器模式允许向一个现有的对象添加新的功能,同时又不改变其结构,这种类型的设计模式属于结构型模式,它是作为现有类的一个包装。
装饰器模式在 MyBatis 中的典型代表是 Cache。

Cache 除了有数据存储和缓存的基本功能外(由 PerpetualCache 永久缓存实现),还有其他附加的 Cache 类,比如先进先出的 FifoCache、最近最少使用的 LruCache、防止多线程并发访问的 SynchronizedCache 等众多附加功能的缓存类,Cache 所有实现子类如下图所示:
组成结构:
在这里插入图片描述

接口为Cache,被装饰类PerpetualCache,被装饰类的派生类为:FifoCache、最近最少使用的 LruCache等等。

总结

在本章当中,主要介绍了在Mybatis当中底层配置当中关于设计模式的一些观点,比较重要,我觉得可以深入了解,这样的话以后在处理代码的时候至少能够理解为什么出现报错。


版权声明:本文为m0_46741750原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。