Lazy loading and caching

Lazy loading

  What is lazy loading:  

    MyBatis delay in loading, also called lazy loading means performing a table associated with the query , the delay in accordance with the rules set for the associated object postponed select query. For example, when performing many of the query, the query only one, when the multi-party program requires data, and then send MyBatis sql statement queries, this way can reduce the delay loading pressure database. MyBatis delay loading of only queries on the associated object has a set delay , the main objects are loaded execute a query directly.

  Load opportunity:  

    Direct loading: completion of the loading of the main objects of the select statement, immediately execute a query to select the associated object.

    Intrusive delay loading: when the query is executed for the main load an object will not execute the query for the associated object. But when you want to access the main load object details properties associated objects will immediately execute select query.

    Depth is loaded: when the query is executed on the primary loading objects, will not perform queries on the associated object. It will not execute the associated object when the object is loaded to access the main details of select query. Only when the real details to access the associated object, select queries will be executed on the associated object.

  Intrusive extended cache:

       

    Mybatis-config.xml large profile, first turn-on delay loading, and then loading the configuration intrusive

      

 

 

       

    When the main object is loaded not only a call to SQL

      

 

 

           

    It will have two main object is loaded when the information is called SQL

      

 

 

       

  Delay buffer depth:   

    Mybatis-config.xml large configuration file, first open lazy loading, and then configure the load depth

      

 

 

       

    The second load will not be executed when you call the main object is loaded SQL

      

          

    The second query executes associated objects For more information call

      

 

      

 

mybatis Cache

  Cache

    A cache is based PerpetualCache (MyBatis carrying ) the HashMap local cache scope of session in the art, when the session the flush or close after the session all the cache will be empty.

    The underlying implementation is a cache the Map , the Map 's key is based on the query, use ORM different framework, based on different queries, Mybatis query basis for SQL 's ID + SQL statements, Hibernate is the result of a query object ID

    

 

    Case proved a cache :

      

      When the first query, go database queries and data into the cache, a second query, go query data cache, so the value of the implementation of a SQL;

 

 

       

    Additions and deletions affect the cache:

      

      In a cache, regardless of whether the transaction commits, will re-query the database;

      When a transaction is committed to the data cache emptied;

      

    in conclusion:     

      Level cache is real, no configuration

 

      Level cache is based on Id + SQL statements;

 

      Additions and deletions will clear the cache

 

  Secondary cache  

    Secondary cache is Global Caching , it goes session range, may be all SqlSession shared, it is only necessary to open the MyBatis core document (mybatis-config.xml) settings provided can be in.

    The cache is a cache SQL statements, as a result of secondary cache of cache objects.  

    pom.xml file configuration:

 

      <!--引入需要的ehcache插件-->
      <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>1.2.3</version>
      </dependency>
      <!--mybatis整合ehcachejar-->
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-ehcache</artifactId>
        <version>1.0.0</version>
      </dependency>  

    In the opening of the secondary cache mybatis-config.xml

      <Settings>
              <-! Open secondary cache ->
              <Setting name = "cacheEnabled" value = "to true" />
      </ Settings>   

    In the small configuration xml file to increase cache node

         

      <cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>

    Entity to achieve Serializable (marker interfaces: there is no way)

      

 

 

       

 

    test:

      

    result:

        

    Additions and deletions affect the cache:

      Transaction submitted:

        

 

         

      Do not submit transaction:

        

 

         

 

  Close to refresh the cache    

    In small additions and deletions to the corresponding node configuration, added flushCache value to false , the default is true;

    In the select statement default value is false, in the first query, database query, follow the same query, the query will be cached. ,

    

 

 

     

 

  Query caching does not apply

    Application Cache representative will not again send SQL query data

    

 

 

     

 

     

 

Guess you like

Origin www.cnblogs.com/wnwn/p/11671327.html