youbo1 发表于 2016-11-27 07:25:20

MyBatis源码赏析3-配置

 MyBatis源码赏析3-配置 2012-07-09 16:35:36
分类: Java
 

     作为一名一线应用开发人员,“配置”一词,可能已经听得耳朵都长茧了。但是,一个程序或者说是一个库,具有可配置性,是非常必要的,否则就得以纯编程的方式使用它们。试想一下,如果你在使用数据库产品时,你还需要通过编程来使用,那将是多么地糟糕!

     配置不仅仅是使用的人要用,这个库或者框架的开发者自己也需要用,否则,如何组织内部的各个构件,将会是一件硬编码的事情。总之,配置,就是要组织出一个完整的逻辑或形式系统,以达到使用者的目的。在框架内部来说,通过配置,还在看清楚整个架构。

     通过MyBatis的配置,可以看出整个框架的顶级特性。为什么这么说呢,把这些代码列出来就一目了然了。
点击(此处)折叠或打开


[*]public class Configuration {

[*]  protected Environment environment;
[*]  protected boolean safeRowBoundsEnabled = true;
[*]  protected boolean mapUnderscoreToCamelCase = false;
[*]  protected boolean lazyLoadingEnabled = false;
[*]  protected boolean aggressiveLazyLoading = true;
[*]  protected boolean multipleResultSetsEnabled = true;
[*]  protected boolean useGeneratedKeys = false;
[*]  protected boolean useColumnLabel = true;
[*]  protected boolean cacheEnabled = true;
[*]  protected Integer defaultStatementTimeout;
[*]  protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
[*]  protected AutoMappingBehavior autoMappingBehavior = AutoMappingBehavior.PARTIAL;
[*]  protected Properties variables = new Properties();
[*]  protected ObjectFactory objectFactory = new DefaultObjectFactory();
[*]  protected ObjectWrapperFactory objectWrapperFactory = new DefaultObjectWrapperFactory();
[*]  protected MapperRegistry mapperRegistry = new MapperRegistry(this);
[*]  protected final InterceptorChain interceptorChain = new InterceptorChain();
[*]  protected final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry();
[*]  protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
[*]  protected final Map<String, MappedStatement> mappedStatements = new StrictMap<String, MappedStatement>("Mapped Statements collection");
[*]  protected final Map<String, Cache> caches = new StrictMap<String, Cache>("Caches collection");
[*]  protected final Map<String, ResultMap> resultMaps = new StrictMap<String, ResultMap>("Result Maps collection");
[*]  protected final Map<String, ParameterMap> parameterMaps = new StrictMap<String, ParameterMap>("Parameter Maps collection");
[*]  protected final Map<String, KeyGenerator> keyGenerators = new StrictMap<String, KeyGenerator>("Key Generators collection");
[*]  protected final Set<String> loadedResources = new HashSet<String>();
[*]  protected final Map<String, XNode> sqlFragments = new StrictMap<String, XNode>("XML fragments parsed from previous mappers");
[*]  protected final Collection<XMLStatementBuilder> incompleteStatements = new LinkedList<XMLStatementBuilder>();
[*]  protected final Collection<CacheRefResolver> incompleteCacheRefs = new LinkedList<CacheRefResolver>();
[*]  protected final Collection<ResultMapResolver> incompleteResultMaps = new LinkedList<ResultMapResolver>();
[*]  /**

[*]   * A map holds cache-ref relationship. The key is the namespace that
[*]   * references a cache bound to another namespace and the value is the
[*]   * namespace which the actual cache is bound to.
[*]   */
[*]  protected final Map<String, String> cacheRefMap = new HashMap<String, String>();
[*]  public Configuration(Environment environment) {
[*]    this();
[*]    this.environment = environment;
[*]  }
[*]  public Configuration() {
[*]    typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class.getName());
[*]    typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class.getName());
[*]    typeAliasRegistry.registerAlias("JNDI", JndiDataSourceFactory.class.getName());
[*]    typeAliasRegistry.registerAlias("POOLED", PooledDataSourceFactory.class.getName());
[*]    typeAliasRegistry.registerAlias("UNPOOLED", UnpooledDataSourceFactory.class.getName());
[*]    typeAliasRegistry.registerAlias("PERPETUAL", PerpetualCache.class.getName());
[*]    typeAliasRegistry.registerAlias("FIFO", FifoCache.class.getName());
[*]    typeAliasRegistry.registerAlias("LRU", LruCache.class.getName());
[*]    typeAliasRegistry.registerAlias("SOFT", SoftCache.class.getName());
[*]    typeAliasRegistry.registerAlias("WEAK", WeakCache.class.getName());
[*]  }
[*]......
[*]}


    通过这些字段的名字,我们就可以知道,MyBatis所支持的顶级特性了。之所以叫顶级特性,是因为这些字段出现在顶级配置中,其它一些边缘特性则隐藏在各个字段的实现里。当然,有些不能叫做特性,而应该叫基本术语或基础概念。对应上面的字段,下面列出这些特性或术语:

     数据环境(包括数据源和事务)
     是否启用严格的行绑定
     是否启用下划线与驼峰式命名规则的映射(如first_name => firstName)
     是否启用懒加载模式
     是否启用贪婪地懒加载模式(即尽可能多地使用懒加载)
     是否启用多结果集映射
     是否启用主键生成功能
     是否启用采用列标签功能(如果不启用,则使用下标索引)
     是否启用缓存功能
     默认的JDBC语句超时设置
     默认的执行器类型(共有SIMPLE,REUSE和BATCH三种)
     初始化SqlMapping自动映射行为(共有NONE,PARTIAL和FULL三种)
     
     其它文本属性(作为扩展或使用者自己用,存放在Promerties中)
     初始化生成对象(实例)的工厂
     初始化对象的包装工厂(用于代理,完成一些重要的特性,比如事务)
     初始化SqlMapping的映射器
     
     初始化拦截器链
     初始化类型处理注册器(默认的注册器就已经预注册了常见的类型)
     初始化类型别名注册器
     初始化JDBC语句容器(就是一个Map,下同)
     初始化缓存
     初始化结果集容器
     初始化参数容器
     初始化主键生成器容器
     
     初始化化已经加的载资源容器
     初始化SQL语句片断容器(SQL语句片断,是可重用的,相信大家在Mapping文件中经常用到)
     
     初始化不完整的JDBC语句容器(显然这个语句还没有执行插值操作)
     初始化不完整的缓存引用容器
     初始化不完整的结果集映射容器
     
     初始化缓存引用容器
     ----------------------------------------------------

     Configuration默认有两个构造器,一个是无参的,另一个则需要指定数据环境。但指定数据环境的这个构造器首先调用了无参的构造器。通过上面的源码,我们可以看到,无参构造器主要是注册了一些重要的类型别名,这些别名在XML配置中会用到。整个Configuration的所有字段都已经初始化了,除了environmnet。因此,它还提供一个传递数据环境的构造器。
     
      Configuration是所有组件的有机组合器,同时也是运行时数据收集器,它会在DefaultSqlSession中用到,并再次传递给Executor接口,它们都依赖它。可以说它是拼接整个MyBatis应用的核心人物,就像Spring在应用程序开发中的地位。
页: [1]
查看完整版本: MyBatis源码赏析3-配置