2、Configuration详解

Configuration类

Configuration类的位置org.apache.ibatis.session.Configuration
Configuration类保存了所有Mybatis的配置信息。
。一般情况下Mybatis在运行过程中只会创建一个Configration对象,并且配置信息不能再被修改。

Configuration的属性

configuration的属性主要分为两大部分:
1. 从mybatis-config.xml中读取的配置
2. 从mapper配置文件或Mapper注解读取的配置

从mybatis-config.xml文件中对应的属性

protected boolean safeRowBoundsEnabled = false;  
protected boolean safeResultHandlerEnabled = true;  
protected boolean mapUnderscoreToCamelCase = false;  
protected boolean aggressiveLazyLoading = true;  
protected boolean multipleResultSetsEnabled = true;  
protected boolean useGeneratedKeys = false;  
protected boolean useColumnLabel = true;  
protected boolean cacheEnabled = true;  
protected boolean callSettersOnNulls = false;  
protected String logPrefix;  
protected Class <? extends Log> logImpl;  
protected LocalCacheScope localCacheScope = LocalCacheScope.SESSION;  
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;  
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 boolean lazyLoadingEnabled = false;  
protected ProxyFactory proxyFactory;  

protected final InterceptorChain interceptorChain = new InterceptorChain();  
protected final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry();  
protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();  
protected final LanguageDriverRegistry languageRegistry = new LanguageDriverRegistry(); 

从Mapper配置文件中读取的属性

protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection");  
protected final Map<String, Cache> caches = new StrictMap<Cache>("Caches collection");  
protected final Map<String, ResultMap> resultMaps = new StrictMap<ResultMap>("Result Maps collection");  
protected final Map<String, ParameterMap> parameterMaps = new StrictMap<ParameterMap>("Parameter Maps collection");  
protected final Map<String, KeyGenerator> keyGenerators = new StrictMap<KeyGenerator>("Key Generators collection");  
  1. mappedStatements属性,保存了所有Mapper配置文件中的select/update/insert/delete节点信息。属性类型为一个Map,key为sql对应的ID,MappedSatement为一个java对象,保存了一个select/update/insert/delete的节点信息。
  2. resultMaps属性,保存了所有Mapper配置文件中的resultMap节点。
    Mapper配置文件也主要是配置select/update/insert/delete/resultMap这几个节点。

Configuration加载过程

  1. XMLConfigBuilder解析mybatis-config.xml的配置到Configuration中
  2. XMLMapperBuilder解析Mapper配置文件的配置到Configuration中

Configuration的创建

Configuration创建的过程会对属性进行初始化
下面的属性都是不可变的,意味着只能使用,mybatis默认提供的实现,或者类


    protected final 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<MappedStatement>("Mapped Statements collection");
    protected final Map<String, Cache> caches = new StrictMap<Cache>("Caches collection");
    protected final Map<String, ResultMap> resultMaps = new StrictMap<ResultMap>("Result Maps collection");
    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>();

Configuration的无参的构造方法,默认注册了很多类型别名

public Configuration() {
        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);
    }

另外还有其他的构造器,具体参考`Configuration

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值