标签:
Caching data that is loaded from the database is a common requirement for many applications to improve their performance. MyBatis provides in-built support for caching the query results loaded by mapped SELECT statements. By default, the first-level cache is enabled; this means that if you‘ll invoke the same SELECT statement within the same SqlSession interface, results will be fetched from the cache instead of the database.
We can add global second-level caches by adding the <cache/> element in SQL Mapper XML files.
When you‘ll add the <cache/> element the following will occur:
You can also customize this behavior by overriding the default attribute values as follows:
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
A description for each of the attributes is as follows:
A cache configuration and cache instance are bound to the namespace of the SQL Mapper file, so all the statements in the same namespace table as the cache are bound by it.
The default cache configuration for a mapped statement is:
<select ... flushCache="false" useCache="true"/> <insert ... flushCache="true"/> <update ... flushCache="true"/> <delete ... flushCache="true"/>
You can override this default behavior for any specific mapped statements; for example, by not using a cache for a select statement by setting the useCache="false" attribute.
In addition to in-built Cache support, MyBatis provides support for integration with popular third-party Cache libraries, such as Ehcache, OSCache, and Hazelcast.
标签:
原文地址:http://www.cnblogs.com/huey/p/5233919.html