Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101496
  • 博文数量: 9
  • 博客积分: 335
  • 博客等级: 一等列兵
  • 技术积分: 175
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-06 17:13
文章分类
文章存档

2012年(6)

2011年(3)

分类: Java

2012-07-09 16:35:36

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

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

     通过MyBatis的配置,可以看出整个框架的顶级特性。为什么这么说呢,把这些代码列出来就一目了然了。

点击(此处)折叠或打开

  1. public class Configuration {

  2.   protected Environment environment;

  3.   protected boolean safeRowBoundsEnabled = true;
  4.   protected boolean mapUnderscoreToCamelCase = false;
  5.   protected boolean lazyLoadingEnabled = false;
  6.   protected boolean aggressiveLazyLoading = true;
  7.   protected boolean multipleResultSetsEnabled = true;
  8.   protected boolean useGeneratedKeys = false;
  9.   protected boolean useColumnLabel = true;
  10.   protected boolean cacheEnabled = true;
  11.   protected Integer defaultStatementTimeout;
  12.   protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
  13.   protected AutoMappingBehavior autoMappingBehavior = AutoMappingBehavior.PARTIAL;

  14.   protected Properties variables = new Properties();
  15.   protected ObjectFactory objectFactory = new DefaultObjectFactory();
  16.   protected ObjectWrapperFactory objectWrapperFactory = new DefaultObjectWrapperFactory();
  17.   protected MapperRegistry mapperRegistry = new MapperRegistry(this);

  18.   protected final InterceptorChain interceptorChain = new InterceptorChain();
  19.   protected final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry();
  20.   protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
  21.   protected final Map<String, MappedStatement> mappedStatements = new StrictMap<String, MappedStatement>("Mapped Statements collection");
  22.   protected final Map<String, Cache> caches = new StrictMap<String, Cache>("Caches collection");
  23.   protected final Map<String, ResultMap> resultMaps = new StrictMap<String, ResultMap>("Result Maps collection");
  24.   protected final Map<String, ParameterMap> parameterMaps = new StrictMap<String, ParameterMap>("Parameter Maps collection");
  25.   protected final Map<String, KeyGenerator> keyGenerators = new StrictMap<String, KeyGenerator>("Key Generators collection");

  26.   protected final Set<String> loadedResources = new HashSet<String>();
  27.   protected final Map<String, XNode> sqlFragments = new StrictMap<String, XNode>("XML fragments parsed from previous mappers");

  28.   protected final Collection<XMLStatementBuilder> incompleteStatements = new LinkedList<XMLStatementBuilder>();
  29.   protected final Collection<CacheRefResolver> incompleteCacheRefs = new LinkedList<CacheRefResolver>();
  30.   protected final Collection<ResultMapResolver> incompleteResultMaps = new LinkedList<ResultMapResolver>();
  31.   /**
  32.    * A map holds cache-ref relationship. The key is the namespace that
  33.    * references a cache bound to another namespace and the value is the
  34.    * namespace which the actual cache is bound to.
  35.    */
  36.   protected final Map<String, String> cacheRefMap = new HashMap<String, String>();

  37.   public Configuration(Environment environment) {
  38.     this();
  39.     this.environment = environment;
  40.   }

  41.   public Configuration() {
  42.     typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class.getName());
  43.     typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class.getName());
  44.     typeAliasRegistry.registerAlias("JNDI", JndiDataSourceFactory.class.getName());
  45.     typeAliasRegistry.registerAlias("POOLED", PooledDataSourceFactory.class.getName());
  46.     typeAliasRegistry.registerAlias("UNPOOLED", UnpooledDataSourceFactory.class.getName());

  47.     typeAliasRegistry.registerAlias("PERPETUAL", PerpetualCache.class.getName());
  48.     typeAliasRegistry.registerAlias("FIFO", FifoCache.class.getName());
  49.     typeAliasRegistry.registerAlias("LRU", LruCache.class.getName());
  50.     typeAliasRegistry.registerAlias("SOFT", SoftCache.class.getName());
  51.     typeAliasRegistry.registerAlias("WEAK", WeakCache.class.getName());
  52.   }

  53. ......
  54. }
    通过这些字段的名字,我们就可以知道,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在应用程序开发中的地位。

阅读(8621) | 评论(0) | 转发(0) |
0

上一篇:MySql编码配置

下一篇:Navicat永久试用

给主人留下些什么吧!~~