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

來源:互聯網
上載者:User

標籤: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.