The MyBatis 3.x already supports the cache feature, and it is easy to add the following nodes to the Mappper XML file:
1 <Mappernamespace= "Com.cnblogs.yjmyzz.mybatis.mapper.OrderMapper">2 3 <Cache4 Eviction= "LRU"5 Flushinterval= "60000"6 size= "1024x768"7 readOnly= "true"8 /> 9 ...Ten </Mapper>
Here are a few points to note:
Eviction is a cache culling algorithm with an optional value of "LRU", "FIFO", "SOFT", "WEAK", and the default value is LRU
Flashinterval refers to the cache expiration time, in milliseconds, 60000 is 60 seconds, the default value is empty, that is, as long as the capacity is sufficient, never expires
size refers to how many objects are cached, with a default value of 1024
If readOnly is read-only, if true, all the same SQL statements return the same object (which helps improve performance, but may not be secure when the same data is concurrently manipulated), and if set to False, the same SQL, The clone copy of the cache is accessed later.
This is the global setting, which can also have local settings on each individual SQL statement, such as:
<id= "GetOrder" parametertype= "int" resulttype= "Torder" usecache= "false"> ... </ Select >
usecache= "false" means that the SELECT statement does not use the cache (even if the XML starts with the global cache enabled)
By default, if the cache is turned on globally, the associated cache entries are automatically refreshed when Insert/update/delete succeeds, but one thing to note : When MyBatis and hibernate are mixed, Because MyBatis and hibernate cache is irrelevant, if use mybatis do select query, Hibernate do insert/update/delete,hibernate to the data modification, and will not refresh MyBatis cache.
MyBatis 3.x Caching Cache usage