Home > Java > Java Tutorial > body text

Detailed explanation of cache examples in mybatis

Y2J
Release: 2017-05-13 10:40:54
Original
1414 people have browsed it

This article mainly introduces the query cache of the mybatis tutorial (first-level cache, second-level cache and integrated ehcache), which has certain reference value. Interested friends can refer to it

1 The meaning of cache

Put the data frequently queried by users in the cache (memory). Users do not need to query the data from the disk (relational database data file), but from the cache query, thereby improving query efficiency and solving the performance problem of high-concurrency systems.

2 mybatis persistence layer cache

mybatis provides first-level cache and second-level cache

Mybatis first-level cache is a SqlSession level. sqlsession can only access the data of its own first-level cache. The second-level cache is across sqlSession and is a mapper-level cache. For The mapper level cache can be shared by different sqlsession.

3 Level 1 cache

3.1 Principle

The first time a query sql is issued, the sql query results are written into the first-level cache of sqlsession. The data structure used in the cache is a map

key: hashcode+ sql+sql input parameter + output parameter (the unique identifier of sql)

value: user information

If the same sqlsession issues the same sql again, it will be fetched from the cache Don't use the database. If there is a commit operation (modify, add, delete) between two times, all the first-level cache area in this sqlsession will be cleared, and the next time you go to the cache to query it, you will not be able to query it, so you have to query it from the database, query it from the database to the cache again.

Every query is first queried from the cache:


If the query is found in the cache, the cache dataReturn directly.

If the query cannot be found in the cache, query it from the database:

3.2 Level 1 cache configuration

Mybatis supports first-level cache by default and does not require configuration.

Note: Mapper agent development is carried out after the integration of mybatis and spring. First-level cache is not supported. Mybatis and spring are integrated. Spring generates the mapper agent according to the mapper template. Object, template Close the sqlsession uniformly at the end.

3.3 Level 1 cache test


//一级缓存 
  @Test 
  public void testCache1() throws Exception { 
 
    SqlSession sqlSession = sqlSessionFactory.openSession(); 
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 
     
    //第一次查询用户id为1的用户 
    User user = userMapper.findUserById(1); 
    System.out.println(user); 
     
    //中间修改用户要清空缓存,目的防止查询出脏数据 
    /*user.setUsername("测试用户2"); 
    userMapper.updateUser(user); 
    sqlSession.commit();*/ 
     
    //第二次查询用户id为1的用户 
    User user2 = userMapper.findUserById(1); 
    System.out.println(user2); 
     
    sqlSession.close(); 
     
 
  }
Copy after login

4 Level 2 cache

4.1 Principle

The scope of the second-level cache is the mapper level (the mapper is named with the same Space), mapper creates a cache data structure in namespace units, and the structure is map.

Every time you query, first check whether the second-level cache is enabled. If it is enabled to fetch cached data from the data structure of the second-level cache,

If it is not obtained from the second-level cache, search it from the first-level cache. If it is not found in the first-level cache, query it from the database.

4.2 mybatis second-level cache configuration

Add ## to the core configuration file
SqlMapConfig.xml


#

Copy after login

To add a line to your Mapper mapping file: , indicating that this mapper enables the second-level cache.

4.3 Query result mapping pojo serialization

mybatis second-level cache needs to implement the Java.io.serializable

interface for the query result mapping pojo , if not implemented, throws an exception :

org.apache.ibatis.cache.CacheException: Error serializing object. Cause: java.io.NotSerializableException: com .sihai.mybatis.po.User


The second-level cache can write memory data to disk. There is serialization and deserialization of objects, so the java.io.serializable interface must be implemented.


If the pojo of the result mapping also includes pojo, the java.io.serializable interface must be implemented.


4.4 二级缓存禁用

对于变化频率较高的sql,需要禁用二级缓存:

在statement中设置useCache=false可以禁用当前select语句的二级缓存,即每次查询都会发出sql去查询,默认情况是true,即该sql使用二级缓存。