Ibatis learning summary 4--SQL Map XML ing file extension, ibatis4 -- SQL

Source: Internet
Author: User

Ibatis learning summary 4--SQL Map XML ing file extension, ibatis4 -- SQL

In addition to the attributes mentioned above, the SQL Map XML ing file also has some important attributes. These attributes are described in detail below.

  • Cache Mapped Statement result set

    By specifying the cacheModel attribute in the query statement, you can cache the query results obtained in the Mapped Statement. The Cache model is a configurable Cache mode defined in the SQL Map XML file. You can use the cacheModel element to configure it.

    1 <cacheModel id="product-cache" type ="LRU" readOnly=”true” serialize=”false”>2 <flushInterval hours="24"/>3 <flushOnExecute statement="insertProduct"/>4 <flushOnExecute statement="updateProduct"/>5 <flushOnExecute statement="deleteProduct"/>6 <property name=”cache-size” value=”1000” />7 </cacheModel>

    The above cache model creates a cache named "product-cache", which is implemented using "least recently used" (LRU. The Implemention attribute name is either a fully qualified class name or a cache implementation alias (see the following content ). According to the content of the flush element in cacheModel, the above example is refreshed every 24 hours. A cacheModel can have only one flushInteval element, which can be set using hours, minutes, seconds, or milliseconds. In addition, when the Mapped Statement of insertProduct, updateProduct, or deleteProduct is executed, the cache is refreshed. CacheModel can define any number of flushOnExecute elements. The implementation of some cache models may require additional attributes, such as the "cache-size" attribute above. In the LRU cache model, cache-size specifies the number of items stored in the cache. Once the cache model is configured, you can specify the cache model used by the mapped statement. For example:

    1 <statement id=”getProductList” cacheModel=”product-cache”>2 select * from PRODUCT where PRD_CAT_ID = #value#3 </statement>

    Read-Only VS read/write

    The Framework supports both read-only and read-write caches. The read-only cache can be shared by all users, so the performance is better. However, the read-only cached data should not be modified. Instead, data must be read from the database (or read/write cache) to update data. Therefore, to read and modify the data, you must be able to read and write the cache. To use the read-only cache, set readOnly = "true" in cacheModel ". To use the read/write cache, set readOnly to "false ". The default value is read-only cache (true ).

    Serializable read/write Cache

    As you know, only effective cache for the current Session can improve the overall application performance. Serializable read/write cache can improve the performance of the entire application (not just each Session. This cache returns different instances (replicas) of the cached object for each Session ). Therefore, each Session can safely modify the returned object. The difference is that you usually want to get the same object from the cache, but in this case, you get different objects. In addition, each cached object in the Serializable cache must be Serializable. This means that Serializable caching and delayed loading cannot be used at the same time, because the delayed loading agent is not Serializable. If you want to know how to use Serializable cache, delayed loading, and joint query, the best way is to try. To use the Serializable cache, set readOnly = "false" and serialize = "true ". By default, the cache is read-only and does not use Serializable cache. Serializable is not required for read-only caching.

    Cache type

    The Cache Model uses plug-ins to support different Cache algorithms. Its implementation is specified by the type attribute in cacheModel (as shown above ). The specified implementation class must implement the CacheController interface or one of the following four aliases. Other configuration parameters implemented by the Cache Model are set through the attribute element of the cacheModel. Currently, this includes the following four implementations:

    * "MEMORY" (com. ibatis. db. sqlmap. cache. memory. MemoryCacheController)

    MEMORY cache implements the use of the reference type to manage cache behavior. The garbage collector can determine whether to recycle data in the cache based on the reference type. MEMORY implementation is applicable to applications without a unified image reuse mode or applications with insufficient MEMORY.

    MEMORY can be configured as follows:

    1 <cacheModel id="product-cache" type="MEMORY">2 <flushInterval hours="24"/>3 <flushOnExecute statement="insertProduct"/>4 <flushOnExecute statement="updateProduct"/>5 <flushOnExecute statement="deleteProduct"/>6 <property name=”reference-type” value=”WEAK” />7 </cacheModel>

    MEMORY cache only recognizes one <property> element. The value of the property "reference-type" must be STRONG, SOFT, or WEAK. These three values correspond to different types of memory reference in JVM.

    The following table describes different reference types in MEMORY implementation. For a better understanding of the reference type, see java. lang. ref In the JDK documentation to obtain

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.