MyBeatis源码学习笔记(四)

  SqlSessionFactory 作为MyBeatis的关键入口,业务尽量保持单一性,单纯地提供了openSession方法,用于获取Session。

  getConfiguration()耦合了Configuration类,利用Configuration类保存所需要的所有的配置信息。

  XMLConfigBuilder用于解析Xml,提供通过Reader,和InputStream两种方式转换成Configuration。XMLConfigBuilder与Configuration耦合,解耦,降低SqlSessionFactory与其他类的耦合度。

  Configuration中的一个重要的属性,Environment(环境)包含了之前参考文档中提供的DataSource数据源,TransactionFactory用于控制事务方式。

  大量使用了Builder模式,用于构建复杂的配置,环境,Session工厂等属性多,复杂的类。

Configuration中有几个new方法,其中一个是newExecutor,用于生成Executor(执行器),也就是执行查询,更新,插入的实体类。通过之前介绍的ExecutorType枚举类型,生成对应的四种类型的执行器,分别为BatchExecutorReuseExecutor,SimpleExecutor,CachingExecutor。

 public Executor newExecutor(Transaction transaction, ExecutorType executorType, boolean autoCommit) {
    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, autoCommit);
    }
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  }

由此看来执行器的类型比较稳定,应该没有那么轻易增加。

由SqlSessionFactory->Configuration->Environment,TransactionFactory,Executor 。

基本实现了Session工厂到配置,到环境(包括数据源,事务方式,执行器方式)。

我个人认为值得学习的地方:

SqlSessonFacoty保持了业务的单一性,Builder模式构建复杂的,多属性,而且属性之间关系的复杂对象。

  Configuration将Environment环境类,事务类型,执行类型所有构建都集于一身。通过Configuration中的修改各个属性,可以在代码中切换数据源,执行器类型,事务类型等,更灵活。

  通过Executor的各种类型,实现不同的执行方式,功能强大。暂时只从中吸收了这几点,以后再来回顾,或者在通读了源码之后,可能会有更新的认识。

posted on 2013-11-03 22:22  错误王  阅读(717)  评论(0)    收藏  举报

导航