共計 4779 個字符,預(yù)計需要花費 12 分鐘才能閱讀完成。
這篇文章主要介紹“什么是 MyBatis 緩存”,在日常操作中,相信很多人在什么是 MyBatis 緩存問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”什么是 MyBatis 緩存”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學(xué)習(xí)吧!
什么是 Mybatis 緩存?
使用緩存可以減少 Java
Application 與數(shù)據(jù)庫的交互次數(shù),從而提升程序的運行效率。比如,查詢 id= 1 的 user 對象,第一次查詢出來之后,會自動將該對象保存到緩存中。下一次查詢該對象時,就可以直接從緩存中獲取,不需要發(fā)送 SQL 查詢數(shù)據(jù)庫了。
Mybatis 緩存分類
一級緩存:SqlSession 級別,默認開啟,且不能關(guān)閉。
mybatis 的一級緩存是 SqlSession 級別的緩存,在操作數(shù)據(jù)庫時需要構(gòu)造 SqlSession 對象,在對象中有一個 HashMap 用于存儲緩存數(shù)據(jù),不同的 SqlSession 之間緩存數(shù)據(jù)區(qū)域 (HashMap) 是互相不影響的。
一級緩存的作用域是 SqlSession 范圍的,當(dāng)在同一個 SqlSession 中執(zhí)行兩次相同的 sql 語句時,第一次執(zhí)行完畢會將數(shù)據(jù)庫中查詢的數(shù)據(jù)寫到緩存 (內(nèi)存) 中,第二次查詢時會從緩存中獲取數(shù)據(jù),不再去底層進行數(shù)據(jù)庫查詢,從而提高了查詢效率。需要注意的是:如果 SqlSession 執(zhí)行了 DML 操作 (insert、update、delete),并執(zhí)行 commit() 操作,mybatis 則會清空 SqlSession 中的一級緩存,這樣做的目的是為了保證緩存數(shù)據(jù)中存儲的是最新的信息,避免出現(xiàn)臟讀現(xiàn)象。
當(dāng)一個 SqlSession 結(jié)束后該 SqlSession 中的一級緩存也就不存在了,Mybatis 默認開啟一級緩存,不需要進行任何配置。
二級緩存:Mapper 級別,默認關(guān)閉,可以開啟。
二級緩存是 Mapper 級別的緩存,使用二級緩存時,多個 SqlSession 使用同一個 Mapper 的 sql 語句去操作數(shù)據(jù)庫,得到的數(shù)據(jù)會存在二級緩存區(qū)域,它同樣是使用 HashMap 進行數(shù)據(jù)存儲,相比一級緩存 SqlSession,二級緩存的范圍更大,多個 SqlSession 可以共用二級緩存,二級緩存是跨 SqlSession 的。
二級緩存是多個 SqlSession 共享的,其作用域是 mapper 的同一個 namespace,不同的 SqlSession 兩次執(zhí)行相同的 namespace 下的 sql 語句,且向 sql 中傳遞的參數(shù)也相同,即最終執(zhí)行相同的 sql 語句,則第一次執(zhí)行完畢會將數(shù)據(jù)庫中查詢的數(shù)據(jù)寫到緩存(內(nèi)存),第二次查詢時會從緩存中獲取數(shù)據(jù),不再去底層數(shù)據(jù)庫查詢,從而提高查詢效率。
Mybatis 默認關(guān)閉二級緩存,可以在 setting 全局參數(shù)中配置開啟二級緩存。
下面我們通過代碼來學(xué)習(xí)如何使用 MyBatis 緩存。
首先來演示一級緩存,以查詢 Student 對象為例。
/**
* @ClassName Student
* @Description
* @Author lzq
* @Date 2019/7/26 13:53
* @Version 1.0
**/
public class Student {
private int SID;
private String Sname;
private String Ssex;
private int Age;
public int getSID() {
return SID;
}
public void setSID(int SID) {
this.SID = SID;
}
public String getSname() {
return Sname;
}
public void setSname(String sname) {
Sname = sname;
}
public String getSsex() {
return Ssex;
}
public void setSsex(String ssex) {
Ssex = ssex;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
@Override
public String toString() {
return [id +SID+ 名字 +Sname+ 性別 +Ssex+ 年齡 +Age+]
}
}
StudentMapper 接口:
import org.apache.ibatis.annotations.*;
public interface StudentMapper {
public Student getStudentById(int id);
}
mybatis-config.xml:
PUBLIC -//mybatis.org//DTD Config 3.0//EN
http://mybatis.org/dtd/mybatis-3-config.dtd
StudentMapper.xml:
PUBLIC -//mybatis.org//DTD Mapper 3.0//EN
http://mybatis.org/dtd/mybatis-3-mapper.dtd
select * from student where SID = #{id}
測試代碼:
**
* @ClassName Test
* @Description
* @Author lzq
* @Date 2019/7/26 13:53
* @Version 1.0
**/
public class Test {
public static void main(String[] args) throws IOException {
String resource = mybatis-config.xml
// 讀取配置文件
InputStream asStream = Resources.getResourceAsStream(resource);
// 創(chuàng)建 sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(asStream);
// 創(chuàng)建 sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 通過動態(tài)代理產(chǎn)生 StudentMapper 對象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
// 查詢 id 為 1 的元組
Student student = mapper.getStudentById(1);
System.out.println(student);
Student student1 = mapper.getStudentById(1);
System.out.println(student1);
}
}
可以看到結(jié)果,執(zhí)行了一次 SQL 語句,查詢出兩個對象,第一個對象是通過 SQL 查詢的,并保存到緩存中,第二個對象是直接從緩存中獲取的。
我們說過一級緩存是 SqlSession 級別的,所以 SqlSession 一旦關(guān)閉,緩存也就不復(fù)存在了,修改代碼,再次測試。
測試代碼:無錫婦科醫(yī)院 http://www.bhnnk120.com/
**
* @ClassName Test
* @Description
* @Author lzq
* @Date 2019/7/26 13:53
* @Version 1.0
**/
public class Test {
public static void main(String[] args) throws IOException {
String resource = mybatis-config.xml
// 讀取配置文件
InputStream asStream = Resources.getResourceAsStream(resource);
// 創(chuàng)建 sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(asStream);
// 創(chuàng)建 sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 通過動態(tài)代理產(chǎn)生 StudentMapper 對象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
// 查詢 id 為 2 的元組
Student student = mapper.getStudentById(1);
System.out.println(student);
sqlSession.close(); // 關(guān)閉原有的 sqlSession
sqlSession = sqlSessionFactory.openSession(); // 創(chuàng)建一個新的
mapper = sqlSession.getMapper(StudentMapper.class);
Student student1 = mapper.getStudentById(1);
System.out.println(student1);
}
}
可以看到,執(zhí)行了兩次 SQL。
在關(guān)閉 SqlSession,一級緩存失效的情況下,可以啟用二級緩存,實現(xiàn)提升效率的要求。
MyBatis 可以使用自帶的二級緩存,也可以使用第三方的 ehcache 二級緩存。
自帶二級緩存
mybatis-config.xml 配置開啟二級緩存
PUBLIC -//mybatis.org//DTD Config 3.0//EN
http://mybatis.org/dtd/mybatis-3-config.dtd
在 StudentMapper.xml 配置中開啟二級緩存:
PUBLIC -//mybatis.org//DTD Mapper 3.0//EN
http://mybatis.org/dtd/mybatis-3-mapper.dtd
select * from student where SID = #{id}
Student 類實現(xiàn) Serializable 接口:
import java.io.Serializable;
/**
* @ClassName Student
* @Description
* @Author lzq
* @Date 2019/7/26 13:53
* @Version 1.0
**/
public class Student implements Serializable{
private int SID;
private String Sname;
private String Ssex;
private int Age;
public int getSID() {
return SID;
}
public void setSID(int SID) {
this.SID = SID;
}
public String getSname() {
return Sname;
}
public void setSname(String sname) {
Sname = sname;
}
public String getSsex() {
return Ssex;
}
public void setSsex(String ssex) {
Ssex = ssex;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
@Override
public String toString() {
return [id +SID+ 名字 +Sname+ 性別 +Ssex+ 年齡 +Age+]
}
}
測試代碼依舊是上一次用的那個:
可以看到,執(zhí)行了一次 SQL,查詢出兩個對象,cache 命中率為 0.5;
到此,關(guān)于“什么是 MyBatis 緩存”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>