As with most persistence layer frameworks, MyBatis also provides support for first-level caches and level two caching
1. The MyBatis cache is based on the Perpetualcache HashMap local cache, which is stored in Session, and by default, the first level cache is enabled . When the Session is flush (); or close (); After that, all the caches in the session will be emptied.
The 2.MyBatis level two cache is the same as the first-level cache, and by default it uses Perpetualcache,hashmap storage, except that it has a storage scope of Mapper (Namespace) and can customize the storage source, such as Ehcache.
3. For the cache data update mechanism, when a scope (cache session/level two cache namespaces) has been c/u/d, the cache in all select in the default scope will be clear.
Program Test level cache
Usersmapper.xml configuration:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace= "Com.entity.test009.usersMapper" >
<select id= "myusers" parametertype= "int" resulttype= "Users" >
SELECT * from users where id=#{id}
</select>
<select id= "Myupdate" parametertype= "Users" >
Update users set Username=#{username},password=#{password} where Id=#{id}
</select>
</mapper>
Write a tool class that gets the first-level cache session:
public class Mybatisutils {public static sqlsession mysession () throws exception{String resource = "Conf.xml"; Reader reader = Resources.getresourceasreader (Resource); Sqlsessionfactory factory = new Sqlsessionfactorybuilder (). build (reader); sqlsession session = Factory.opensession (true); Session.clearcache ();//delete cache return session;}}
test program Code
※ The 1th method
@Test Public voidCacheone ()throwsException {sqlsession session=mybatisutils.mysession (); String Statement= "Com.entity.test009.usersMapper.myUsers"; Users= Session.selectone (statement, 3); System.out.println (users); Users= Session.selectone (statement, 3); System.out.println (users); Clearing the session Cache Session.clearcache (); System.out.println ("-----------------------------------"); Users= Session.selectone (statement, 3); System.out.println (users); }
※ Results after the operation:
2015-05-04 22:49:43,323 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-==> Preparing:SELECT * from users where id=?2015-05-04 22:49:43,361 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-==> parameters:3(Integer)2015-05-04 22:49:43,387 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-<== total:1Users [ID=3, Username= Zhang San, password=123456, age=99, sex=0]users [ID=3, Username= Zhang San, password=123456, age=99, sex=0]---------------------//session.clearcache (); The database was queried again, not the data from the cache2015-05-04 22:49:43,388 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-==> Preparing:SELECT * from users where id=?2015-05-04 22:49:43,389 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-==> parameters:3(Integer)2015-05-04 22:49:43,390 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-<== total:1Users [ID=3, Username= Zhang San, password=123456, age=99, sex=0]
※ The 2nd method
@Test Public voidCacheone ()throwsException {sqlsession session=mybatisutils.mysession (); String Statement= "Com.entity.test009.usersMapper.myUsers"; Users= Session.selectone (statement, 3); System.out.println (users); Users= Session.selectone (statement, 3); System.out.println (users); /*** 2nd method, perform an updated operation*/Session.update ("Com.entity.test009.usersMapper.myUpdate",NewUsers (3, "Zhang San", "123456",NULL,NULL)); Session.commit ();
System.out.println ("---------------------"); Users= Session.selectone (statement, 3); System.out.println (users); }
※ Results after the operation:
2015-05-04 22:55:38,685 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-==> Preparing:SELECT * from users where id=?2015-05-04 22:55:38,724 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-==> parameters:3(Integer)2015-05-04 22:55:38,747 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-<== total:1Users [ID=3, Username= Zhang San, password=123456, age=99, sex=0]users [ID=3, Username= Zhang San, password=123456, age=99, sex=0]2015-05-04 22:55:38,748 [main] DEBUG [com.entity.test009.usersMapper.myUpdate]-==> Preparing:Update users set username=?,password=? where id=?2015-05-04 22:55:38,749 [main] DEBUG [com.entity.test009.usersMapper.myUpdate]-==> Parameters: Zhang San (String), 123456 ( String), 3(Integer)2015-05-04 22:55:38,749 [main] DEBUG [com.entity.test009.usersMapper.myUpdate]-<== updates:1-------------------- -//As you can see, after the update is executed, the first level cache is automatically cleaned up, and then the database is queried again below2015-05-04 22:55:38,750 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-==> Preparing:SELECT * from users where id=?2015-05-04 22:55:38,750 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-==> parameters:3(Integer)2015-05-04 22:55:38,751 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-<== total:1Users [ID=3, Username= Zhang San, password=123456, age=99, sex=0]
※ The 3rd method
@Test Public voidCacheone ()throwsException {sqlsession session=mybatisutils.mysession (); String Statement= "Com.entity.test009.usersMapper.myUsers"; Users= Session.selectone (statement, 3); System.out.println (users); Users= Session.selectone (statement, 3); System.out.println (users); /*** 3rd method, not the same session object. After closing the session, open a session2again. This time is not the same as the earlier one, so we will not use the session just cached.*/Session.close (); Sqlsession session2 =mybatisutils.mysession (); System.out.println ("---------------------"); Users= Session2.selectone (statement, 3); System.out.println (users); }
※ Results after the operation:
2015-05-04 23:06:25,518 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-==> Preparing:SELECT * from users where id=?2015-05-04 23:06:25,557 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-==> parameters:3(Integer)2015-05-04 23:06:25,580 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-<== total:1Users [ID=3, Username= Zhang San, password=123456, age=99, sex=0]users [ID=3, Username= Zhang San, password=123456, age=99, sex=0]---------------------//Regain session, this session2 to retrieve a data from the database.2015-05-04 23:06:25,794 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-==> Preparing:SELECT * from users where id=?2015-05-04 23:06:25,794 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-==> parameters:3(Integer)2015-05-04 23:06:25,795 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-<== total:1Users [ID=3, Username= Zhang San, password=123456, age=99, sex=0]
level two cache test (default off state)
First Open in Usersmapper.xml
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><Mappernamespace= "Com.entity.test009.usersMapper"><cache/><SelectID= "Myusers"ParameterType= "int"Resulttype= "Users">SELECT * from users where id=#{id}</Select><SelectID= "Myupdate"ParameterType= "Users">update users set Username=#{username},password=#{password} where Id=#{id}</Select> </Mapper>
Then create a new tool class that creates a factory, returning a factory, so that the session created is different.
public class Copyofmybatisutils {public static sqlsessionfactory factory () throws exception{String resource = "Conf.xml"; Reader reader = Resources.getresourceasreader (Resource); Sqlsessionfactory factory = new Sqlsessionfactorybuilder (). build (reader); return factory;}}
Test code:
@Test Public voidCachetwo ()throwsException {String statement= "Com.entity.test009.usersMapper.myUsers"; Sqlsessionfactory Factory=copyofmybatisutils.factory (); Sqlsession Session1=factory.opensession (); Sqlsession Session2=factory.opensession (); Users= Session1.selectone (statement, 6); session1.commit ();//need to commit a thing, otherwise the level two cache is invalid System.out.println (users); System.out.println ("****************************"); Users= Session1.selectone (statement, 6); session2.commit ();System.out.println (users); }
Operation Result:
2015-05-04 23:45:21,750 [main] DEBUG [Com.entity.test009.usersMapper]-Cache hit Ratio [com.entity.test009.usersMapper ]: 0.02015-05-04 23:45:22,035 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-==> preparing:select * from use RS where id=? 2015-05-04 23:45:22,079 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-==> parameters:6(Integer)2015-05-04 23:45:22,100 [main] DEBUG [com.entity.test009.usersMapper.myUsers]-<== total:1Users [ID=6, Username= Zhang 3, password=654321, age=99, sex=0]2015-05-04 23:45:22,109 [main] DEBUG [Com.entity.test009.usersMapper]-Cache Hit Ratio[Com.entity.test009.usersMapper]:0.5%Users [ID=6, Username= Zhang 3, password=654321, age=99, sex=0]
Additional notes:
All SELECT statements in the map statement file will be cached.
All insert,update and DELETE statements in the map statement file flush the cache.
The cache is retracted using the least recently used (LRU, least recently used) algorithm.
Depending on the schedule (such as no flush Interval, there is no refresh interval), the cache is not refreshed in any chronological order.
The cache stores 1024 references to a list collection or object, regardless of what the Query method returns.
The cache is considered to be a read/write (readable/writable) cache, meaning that object retrieval is not shared and can be safely modified by the caller without interfering with potential modifications made by other callers or threads
<eviction= "FIFO" //Recycle policy for FIFO flushinterval= "60000" Auto Refresh time 60ssize= "Max" //cache up to 512 reference objects readonly= "true" //Read -only />
MyBatis Learning (9): first-level cache and level two cache