码迷,mamicode.com
首页 > 移动开发 > 详细

MyBatis框架中Mapper映射配置的使用及原理解析(三) 配置篇 Configuration

时间:2017-09-17 17:31:34      阅读:439      评论:0      收藏:0      [点我收藏+]

标签:tostring   label   pool   provider   city   元素   keygen   base   public   

从上文<MyBatis框架中Mapper映射配置的使用及原理解析(二) 配置篇 SqlSessionFactoryBuilder,XMLConfigBuilder> 我们知道XMLConfigBuilder调用parse()方法解析Mybatis配置文件,生成Configuration对象。

Configuration类主要是用来存储对Mybatis的配置文件及mapper文件解析后的数据,Configuration对象会贯穿整个Mybatis的执行流程,为Mybatis的执行过程提供必要的配置信息。

先看一看Configuration类的成员变量和构造方法:

package org.apache.ibatis.session;


public class Configuration {

  protected Environment environment;

  protected boolean safeRowBoundsEnabled = false;  //是否启用行内嵌套语句
  protected boolean safeResultHandlerEnabled = true;
  protected boolean mapUnderscoreToCamelCase = false; //是否启用数据库表字段A_column自动映射到Java类中的驼峰命名的属性
  //当对象使用延迟加载时 属性的加载取决于能被引用到的那些延迟属性,否则按需加载
  protected boolean aggressiveLazyLoading = true;
  protected boolean multipleResultSetsEnabled = true; //是否允许单条sql 返回多个数据集  (取决于驱动的兼容性)
//允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行
protected boolean useGeneratedKeys = false;
  //使用列标签代替列名。不同的驱动在这方面会有不同的表现, 具体可参考相关驱动文档或通过测试这两种不同的模式来观察所用驱动的结果
  protected boolean useColumnLabel = true;
//配置全局性的cache开关
protected boolean cacheEnabled = true;
//在null时也调用 setter,适应于返回Map,3.2版本以上可用
protected boolean callSettersOnNulls = false;
//全局配置打印所有的sql的前缀
protected String logPrefix;
//指定 MyBatis 所用日志的具体实现,未指定时将自动查找
protected Class <? extends Log> logImpl;
//设置本地缓存范围,session:就会有数据的共享,statement:语句范围,这样不会有数据的共享
protected LocalCacheScope localCacheScope = LocalCacheScope.SESSION;
//设置但JDBC类型为空时,某些驱动程序 要指定值
protected JdbcType jdbcTypeForNull = JdbcType.OTHER;
//设置延迟加载的方法
protected Set<String> lazyLoadTriggerMethods = new HashSet<String>(Arrays.asList(new String[] { "equals", "clone", "hashCode", "toString" })); protected Integer defaultStatementTimeout; //设置等待数据响应超时数
protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;//执行类型,有simple、resue及batch
//指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射 PARTIAL:部分  FULL:全部
protected AutoMappingBehavior autoMappingBehavior = AutoMappingBehavior.PARTIAL;

    //全局属性配置对象
    protected Properties variables = new Properties();

  //对象创建工厂,默认的实现类DefaultObjectFactory,用来创建对象,比如传入List.class,利用反射返回ArrayList的实例
  protected ObjectFactory objectFactory = new DefaultObjectFactory();
//对象包装工厂,默认实现类是DefaultObjectWrapperFactory,包装Object实例
protected ObjectWrapperFactory objectWrapperFactory = new DefaultObjectWrapperFactory();
//注册Mapper
protected MapperRegistry mapperRegistry = new MapperRegistry(this); protected boolean lazyLoadingEnabled = false;//是否使用 懒加载 关联对象
//指定 Mybatis 创建具有延迟加载能力的对象所用到的代理工具
protected ProxyFactory proxyFactory; //数据库类型id protected String databaseId;

  /**
   * Configuration factory class.
   * Used to create Configuration for loading deserialized unread properties.
   *
   * @see <a href=‘https://code.google.com/p/mybatis/issues/detail?id=300‘>Issue 300</a> (google code)
  */
    protected Class<?> configurationFactory;

  //拦截器链
  protected final InterceptorChain interceptorChain = new InterceptorChain();
//TypeHandler注册
protected final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry();
//别名和具体类注册
protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
//这个是指定解析的驱动,比如你可以使用velocity模板引擎来替代xml文件,默认是XMLLanguageDriver,也就是使用xml文件来写sql语句
protected final LanguageDriverRegistry languageRegistry = new LanguageDriverRegistry();
//对应Mapper.xml里配置的Statement
protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection");
//对应Mapper.xml里配置的cache
protected final Map<String, Cache> caches = new StrictMap<Cache>("Caches collection");
//对应Mapper.xml里的ResultMap
protected final Map<String, ResultMap> resultMaps = new StrictMap<ResultMap>("Result Maps collection");
//对应Mapper.xml里的ParameterMap
protected final Map<String, ParameterMap> parameterMaps = new StrictMap<ParameterMap>("Parameter Maps collection");
//主键生成器
protected final Map<String, KeyGenerator> keyGenerators = new StrictMap<KeyGenerator>("Key Generators collection"); protected final Set<String> loadedResources = new HashSet<String>(); protected final Map<String, XNode> sqlFragments = new StrictMap<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>(); protected final Collection<MethodResolver> incompleteMethods = new LinkedList<MethodResolver>();

    /*
     * 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来注册一些类的别名
    typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);
    typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class);

    typeAliasRegistry.registerAlias("JNDI", JndiDataSourceFactory.class);
    typeAliasRegistry.registerAlias("POOLED", PooledDataSourceFactory.class);
    typeAliasRegistry.registerAlias("UNPOOLED", UnpooledDataSourceFactory.class);

    typeAliasRegistry.registerAlias("PERPETUAL", PerpetualCache.class);
    typeAliasRegistry.registerAlias("FIFO", FifoCache.class);
    typeAliasRegistry.registerAlias("LRU", LruCache.class);
    typeAliasRegistry.registerAlias("SOFT", SoftCache.class);
    typeAliasRegistry.registerAlias("WEAK", WeakCache.class);

    typeAliasRegistry.registerAlias("DB_VENDOR", VendorDatabaseIdProvider.class);

    typeAliasRegistry.registerAlias("XML", XMLLanguageDriver.class);
    typeAliasRegistry.registerAlias("RAW", RawLanguageDriver.class);

    typeAliasRegistry.registerAlias("SLF4J", Slf4jImpl.class);
    typeAliasRegistry.registerAlias("COMMONS_LOGGING", JakartaCommonsLoggingImpl.class);
    typeAliasRegistry.registerAlias("LOG4J", Log4jImpl.class);
    typeAliasRegistry.registerAlias("LOG4J2", Log4j2Impl.class);
    typeAliasRegistry.registerAlias("JDK_LOGGING", Jdk14LoggingImpl.class);
    typeAliasRegistry.registerAlias("STDOUT_LOGGING", StdOutImpl.class);
    typeAliasRegistry.registerAlias("NO_LOGGING", NoLoggingImpl.class);

    typeAliasRegistry.registerAlias("CGLIB", CglibProxyFactory.class);
    typeAliasRegistry.registerAlias("JAVASSIST", JavassistProxyFactory.class);

    languageRegistry.setDefaultDriverClass(XMLLanguageDriver.class);
    languageRegistry.register(RawLanguageDriver.class);
  }
  
 }

 

后续我们将结合XMLConfigBuilder,Configuration 讲解Mapper.xml的各元素的解析。

MyBatis框架中Mapper映射配置的使用及原理解析(三) 配置篇 Configuration

标签:tostring   label   pool   provider   city   元素   keygen   base   public   

原文地址:http://www.cnblogs.com/zsg88/p/7534951.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!