Mybatis一級二級緩存

一 mybatis 一級緩存
**mybatis默認開啓的是一級緩存。**第一次執行完畢會將數據庫中查詢的數據寫到緩存(內存),第二次會從緩存中獲取數據將不再從數據庫查詢,從而提高查詢效率。當一個sqlSession結束後該sqlSession中的一級緩存也就不存在了。
Mybatis 對緩存提供支持,在沒有默認配置的情況下,它只開啓一級緩存,一級緩存只是相對於同一個sqlsession而言。所有參數和sql完全一樣的情況下,我們使用同一個sqlsession對象調用一個mapper方法,往往只執行一次sql,因爲使用SelSession第一次查詢後,MyBatis會將其放在緩存中,以後再查詢的時候,如果沒有聲明需要刷新,並且緩存沒有超時的情況下,SqlSession都會取出當前緩存的數據,而不會再次發送SQL到數據庫。
在這裏插入圖片描述
二 二級緩存
MyBatis的二級緩存是Application級別的緩存,它可以提高對數據庫查詢的效率,以提高應用的性能。
MyBatis的緩存機制整體設計以及二級緩存的工作模式
在這裏插入圖片描述
 SqlSessionFactory層面上的二級緩存默認是不開啓的,二級緩存的開席需要進行配置,實現二級緩存的時候,MyBatis要求返回的POJO必須是可序列化的。 也就是要求實現Serializable接口,配置方法很簡單,只需要在映射XML文件配置就可以開啓緩存了,如果我們配置了二級緩存就意味着:

映射語句文件中的所有select語句將會被緩存。
映射語句文件中的所欲insert、update和delete語句會刷新緩存。
緩存會使用默認的Least Recently Used(LRU,最近最少使用的)算法來收回。
根據時間表,比如No Flush Interval,(CNFI沒有刷新間隔),緩存不會以任何時間順序來刷新。
緩存會存儲列表集合或對象(無論查詢方法返回什麼)的1024個引用
緩存會被視爲是read/write(可讀/可寫)的緩存,意味着對象檢索不是共享的,而且可以安全的被調用者修改,不干擾其他調用者或線程所做的潛在修改。

<?xml version="1.0" encoding="UTF-8" ?> 複製代碼

三、在 mybatis-config.xml中開啓二級緩存
複製代碼

<?xml version="1.0" encoding="UTF-8" ?> ..... .... 複製代碼

四、測試
複製代碼
package com.yihaomen.service.student;

import com.yihaomen.mybatis.dao.StudentMapper;
import com.yihaomen.mybatis.model.Student;
import com.yihaomen.mybatis.model.Teacher;
import com.yihaomen.service.BaseTest;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;

/**
*

  • @ProjectName: springmvc-mybatis
    */
    public class TestStudent extends BaseTest {

    public static void selectAllStudent() {
    SqlSessionFactory sqlSessionFactory = getSession();
    SqlSession session = sqlSessionFactory.openSession();
    StudentMapper mapper = session.getMapper(StudentMapper.class);
    List list = mapper.selectAllStudents();
    System.out.println(list);
    System.out.println(“第二次執行”);
    List list2 = mapper.selectAllStudents();
    System.out.println(list2);
    session.commit();
    System.out.println(“二級緩存觀測點”);
    SqlSession session2 = sqlSessionFactory.openSession();
    StudentMapper mapper2 = session2.getMapper(StudentMapper.class);
    List list3 = mapper2.selectAllStudents();
    System.out.println(list3);
    System.out.println(“第二次執行”);
    List list4 = mapper2.selectAllStudents();
    System.out.println(list4);
    session2.commit();

    }

    public static void main(String[] args) {
    selectAllStudent();
    }
    }
    複製代碼

結果:

[QC] DEBUG [main] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(98) | Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@51e0173d]
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(139) | ==> Preparing: SELECT id, name, age FROM student
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(139) | > Parameters:
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(139) | <
Total: 6
[Student{id=‘1’, name=‘劉德華’, age=55, gender=null, teachers=null}, Student{id=‘2’, name=‘張惠妹’, age=49, gender=null, teachers=null}, Student{id=‘3’, name=‘謝霆鋒’, age=35, gender=null, teachers=null}, Student{id=‘4’, name=‘王菲’, age=47, gender=null, teachers=null}, Student{id=‘5’, name=‘汪峯’, age=48, gender=null, teachers=null}, Student{id=‘6’, name=‘章子怡’, age=36, gender=null, teachers=null}]
第二次執行
[QC] DEBUG [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(62) | Cache Hit Ratio [com.yihaomen.mybatis.dao.StudentMapper]: 0.0
[Student{id=‘1’, name=‘劉德華’, age=55, gender=null, teachers=null}, Student{id=‘2’, name=‘張惠妹’, age=49, gender=null, teachers=null}, Student{id=‘3’, name=‘謝霆鋒’, age=35, gender=null, teachers=null}, Student{id=‘4’, name=‘王菲’, age=47, gender=null, teachers=null}, Student{id=‘5’, name=‘汪峯’, age=48, gender=null, teachers=null}, Student{id=‘6’, name=‘章子怡’, age=36, gender=null, teachers=null}]
二級緩存觀測點
[QC] DEBUG [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(62) | Cache Hit Ratio [com.yihaomen.mybatis.dao.StudentMapper]: 0.3333333333333333
[Student{id=‘1’, name=‘劉德華’, age=55, gender=null, teachers=null}, Student{id=‘2’, name=‘張惠妹’, age=49, gender=null, teachers=null}, Student{id=‘3’, name=‘謝霆鋒’, age=35, gender=null, teachers=null}, Student{id=‘4’, name=‘王菲’, age=47, gender=null, teachers=null}, Student{id=‘5’, name=‘汪峯’, age=48, gender=null, teachers=null}, Student{id=‘6’, name=‘章子怡’, age=36, gender=null, teachers=null}]
第二次執行
[QC] DEBUG [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(62) | Cache Hit Ratio [com.yihaomen.mybatis.dao.StudentMapper]: 0.5
[Student{id=‘1’, name=‘劉德華’, age=55, gender=null, teachers=null}, Student{id=‘2’, name=‘張惠妹’, age=49, gender=null, teachers=null}, Student{id=‘3’, name=‘謝霆鋒’, age=35, gender=null, teachers=null}, Student{id=‘4’, name=‘王菲’, age=47, gender=null, teachers=null}, Student{id=‘5’, name=‘汪峯’, age=48, gender=null, teachers=null}, Student{id=‘6’, name=‘章子怡’, age=36, gender=null, teachers=null}]

Process finished with exit code 0

我們可以從結果看到,sql只執行了一次,證明我們的二級緩存生效了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章