Mybatis Cache Mechanism

Source: Internet
Author: User

The cache mechanism can reduce the pressure on the database. The principle is to cache the query results during the first query and then query the same SQL statement,

Instead of querying the database, the system directly returns the results in the cache.

Caching can reduce the pressure on the database, but it may not be able to obtain the latest results.

1. database cache implementation:

Cache using third-party tools:

Redis memory database-Cache available

Cache is implemented through the cache mechanism provided by mybatis:

Level 1 cache:

The cache is only valid in one transaction, that is, the same query is executed for multiple times in the same transaction, and the results are cached only for the first time, all subsequent queries directly obtain the data in the cache. If the transaction is different, the cache is invalid.

Secondary cache:

The cache is globally valid. When a transaction queries an SQL statement and the result is cached, other transactions query the same SQL statement as long as the cache is unclear, the result will be the result of the previous cache. The second-level cache has a large scope and takes a long time, which may cause greater harm. Therefore, the second-level cache of mybatis is rarely enabled during development.

2. Primary cache of mybatis

The primary cache of mybatis is enabled by default.

// Create sqlsessionfactory private sqlsessionfactory = NULL Based on the configuration file; @ before public void before () throws exception {// 1. read the core configuration file inputstream in = resources. getresourceasstream ("sqlmapconfig. XML "); // 2. create sqlsessionfactory factory = new sqlsessionfactorybuilder () according to the configuration file (). build (in);}/*** level-1 cache */@ test public void test16 () {// 1. create sqlsession session = factory. opensession (); // 2. execute the operation list <user> list1 = session. selectlist ("CN. TEDU. mybatis. beans. usermapper. queryall "); List <user> list2 = session. selectlist ("CN. TEDU. mybatis. beans. usermapper. queryall "); // 3. print the result system. out. println (list1); system. out. println (list2 );}

Test results:

2018-10-27 14:41:53,932 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryAll] - ooo Using Connection [[email protected]]2018-10-27 14:41:53,932 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryAll] - ==>  Preparing: select * from user 2018-10-27 14:41:53,957 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryAll] - ==> Parameters: [User [id=1, name=aaa, age=19], User [id=2, name=bbb, age=29], User [id=4, name=ddd, age=22], User [id=5, name=eee, age=33], User [id=7, name=cjj, age=24]][User [id=1, name=aaa, age=19], User [id=2, name=bbb, age=29], User [id=4, name=ddd, age=22], User [id=5, name=eee, age=33], User [id=7, name=cjj, age=24]]

The database is used only for the first query, and subsequent queries are cached.

Configure to disable the first-level cache
    <select id="queryAll" flushCache="true" resultType="Alias_User">        <include refid="saUser"/>    </select>
2018-10-27 14:48:08,832 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryAll] - ooo Using Connection [[email protected]]2018-10-27 14:48:08,833 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryAll] - ==>  Preparing: select * from user 2018-10-27 14:48:08,857 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryAll] - ==> Parameters: 2018-10-27 14:48:08,874 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryAll] - ooo Using Connection [[email protected]]2018-10-27 14:48:08,874 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryAll] - ==>  Preparing: select * from user 2018-10-27 14:48:08,874 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryAll] - ==> Parameters: [User [id=1, name=aaa, age=19], User [id=2, name=bbb, age=29], User [id=4, name=ddd, age=22], User [id=5, name=eee, age=33], User [id=7, name=cjj, age=24]][User [id=1, name=aaa, age=19], User [id=2, name=bbb, age=29], User [id=4, name=ddd, age=22], User [id=5, name=eee, age=33], User [id=7, name=cjj, age=24]]
3. mybatis second-level cache

The secondary cache of mybatis is disabled by default.

// Create sqlsessionfactory private sqlsessionfactory = NULL Based on the configuration file; @ before public void before () throws exception {// 1. read the core configuration file inputstream in = resources. getresourceasstream ("sqlmapconfig. XML "); // 2. create sqlsessionfactory factory = new sqlsessionfactorybuilder () according to the configuration file (). build (in);}/*** cache mechanism: second-level cache */@ test public void test17 () {// 1. the first transaction sqlsession session1 = factory. opensession (); User user1 = session1.selectone ("CN. TEDU. mybatis. beans. usermapper. queryone ", 1); session1.commit (); // 2. the second transaction sqlsession session2 = factory. opensession (); User user2 = session2.selectone ("CN. TEDU. mybatis. beans. usermapper. queryone ", 1); session2.commit (); // 3. print the result system. out. println (user1); system. out. println (user2 );}

Test results:

2018-10-27 14:49:58,913 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryOne] - ooo Using Connection [[email protected]]2018-10-27 14:49:58,913 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryOne] - ==>  Preparing: select * from user where id = ? 2018-10-27 14:49:58,935 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryOne] - ==> Parameters: 1(Integer)2018-10-27 14:49:58,959 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryOne] - ooo Using Connection [[email protected]]2018-10-27 14:49:58,959 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryOne] - ==>  Preparing: select * from user where id = ? 2018-10-27 14:49:58,959 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryOne] - ==> Parameters: 1(Integer)User [id=1, name=aaa, age=19]User [id=1, name=aaa, age=19]
Enable Level 2 Cache

Configure in sqlmapconfig. xml

<! -- Enable Level 2 Cache --> <Settings> <setting name = "cacheenabled" value = "true"/> </Settings>

Configure in the ing File

The bean to be cached by the second-level cache must implement the serialization interface.

Test class:

Running result:

2018-10-27 14:57:36,745 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryOne] - ooo Using Connection [[email protected]]2018-10-27 14:57:36,746 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryOne] - ==>  Preparing: select * from user where id = ? 2018-10-27 14:57:36,772 [main] DEBUG [cn.tedu.mybatis.beans.UserMapper.queryOne] - ==> Parameters: 1(Integer)User [id=1, name=aaa, age=19]User [id=1, name=aaa, age=19]

 

Mybatis Cache Mechanism

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.