Mybatis查詢時(shí)的延遲加載解析
Mybatis延遲加載
什么是延遲加載呢?
通俗的講就是按需加載,我們需要什么的時(shí)候再去進(jìn)行什么操作。
什么時(shí)候會(huì)用到延遲加載呢?
多表單獨(dú)查詢的時(shí)候。
而且先從單表查詢,需要時(shí)再?gòu)年P(guān)聯(lián)表去關(guān)聯(lián)查詢,能大大提高數(shù)據(jù)庫(kù)性能,因?yàn)椴樵儐伪硪汝P(guān)聯(lián)查詢多張表速度要快。 延遲加載分為兩種:深度延時(shí)加載,侵入式延遲加載
如何開(kāi)啟延時(shí)加載?
兩種方法:
1.在collection標(biāo)簽中:
<select id="selectByMinorId" resultMap="selectMinorMap"> select cid,cname from country where cid =#{id} </select> <resultMap id="selectMinorMap" type="Country"> <id column="cid" property="cid"/> <result column="cname" property="cname"/> <collection column="cid" property="mino" ofType="Minor" select="selectByMinorIds" fetchType="lazy"/> //使用fetch屬性的值為true,默認(rèn)開(kāi)啟深度延遲加載 </resultMap> <select id="selectByMinorIds" resultType="com.example.mytest01.pojo.Minor" > select mname from minor where countryid =#{cid} </select>
2.mybatis.xml中進(jìn)行配置
<settings> <!--延遲加載總開(kāi)關(guān),打開(kāi)后,默認(rèn)的是深度延時(shí)加載 --> <setting name="lazyLoadingEnabled" value="true" /> <!--侵入式延遲加載開(kāi)關(guān) --> <!--3.4.1版本之前默認(rèn)是true,之后默認(rèn)是false --> <setting name="aggressiveLazyLoading" value="true" /> </settings>
加載特點(diǎn)
侵入式延遲: 執(zhí)行對(duì)主加載對(duì)象的查詢時(shí),不會(huì)執(zhí)行對(duì)關(guān)聯(lián)對(duì)象的查詢。但當(dāng)要訪問(wèn)主加載對(duì)象的詳情屬性時(shí),就會(huì)馬上執(zhí)行關(guān)聯(lián)對(duì)象的select查詢。
深度延遲: 執(zhí)行對(duì)主加載對(duì)象的查詢時(shí),不會(huì)執(zhí)行對(duì)關(guān)聯(lián)對(duì)象的查詢。訪問(wèn)主加載對(duì)象的詳情時(shí)也不會(huì)執(zhí)行關(guān)聯(lián)對(duì)象的select查詢。只有當(dāng)真正訪問(wèn)關(guān)聯(lián)對(duì)象的詳情時(shí),才會(huì)執(zhí)行對(duì)關(guān)聯(lián)對(duì)象的 select 查詢。
MyBatis 的延遲加載只是對(duì)關(guān)聯(lián)對(duì)象的查詢有遲延設(shè)置,對(duì)于主加載對(duì)象都是直接執(zhí)行查詢語(yǔ)句的。
舉例: mapper.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"> <!-- namespace:填寫(xiě)映射當(dāng)前的Mapper接口,所有的增刪改查的參數(shù)和返回值類(lèi)型, 就可以直接填寫(xiě)縮寫(xiě),不區(qū)分大小寫(xiě),直接通過(guò)方法名去找類(lèi)型--> <mapper namespace="com.example.mytest01.dao.IMinorDao"> <select id="selectByMinorId" resultMap="selectMinorMap"> select cid,cname from country where cid =#{id} </select> <resultMap id="selectMinorMap" type="Country"> <id column="cid" property="cid"/> <result column="cname" property="cname"/> <collection column="cid" property="mino" ofType="Minor" select="selectByMinorIds"/> </resultMap> <select id="selectByMinorIds" resultType="com.example.mytest01.pojo.Minor" > select mname from minor where countryid =#{cid} </select> </mapper>
當(dāng)不開(kāi)啟延遲加載時(shí):
@Autowired private IMinorDao m; @Test void hah(){ Country c =m.selectByMinorId(1); //System.out.println(c.getCname()); }
進(jìn)行兩次查詢;
@Autowired private IMinorDao m; @Test void hah(){ //Country c =m.selectByMinorId(1); System.out.println(c.getCname()); }
進(jìn)行兩次查詢;
重點(diǎn)來(lái)了: 當(dāng)開(kāi)深度啟延遲加載時(shí):
@Autowired private IMinorDao m; @Test void hah(){ Country c =m.selectByMinorId(1); //System.out.println(c.getCname()); }
通過(guò)日志觀察到,只查詢一次,還未進(jìn)行第二次查詢;
@Autowired private IMinorDao m; @Test void hah(){ //Country c =m.selectByMinorId(1); System.out.println(c.getCname()); }
當(dāng)查詢主加載對(duì)象的詳情屬性時(shí),依舊只進(jìn)行了第一次的查詢; .
@Autowired private IMinorDao m; @Test void hah(){ //Country c =m.selectByMinorId(1); //System.out.println(c.getCname()); System.out.println(c.getMino()); }
看到這里我們會(huì)發(fā)現(xiàn):只有當(dāng)查詢主加載對(duì)象的關(guān)聯(lián)屬性時(shí),才進(jìn)行了兩次查詢
當(dāng)開(kāi)啟侵入式延遲加載時(shí):
@Autowired private IMinorDao m; @Test void hah(){ Country c =m.selectByMinorId(1); //System.out.println(c.getCname()); }
通過(guò)日志觀察到,只查詢一次,還未進(jìn)行第二次查詢;
@Autowired private IMinorDao m; @Test void hah(){ //Country c =m.selectByMinorId(1); System.out.println(c.getCname()); }
與深度延遲加載不同的時(shí):當(dāng)查詢主屬性的詳細(xì)信息時(shí),Mybatis就已經(jīng)進(jìn)行了第二次的關(guān)聯(lián)屬性的查詢;
到此這篇關(guān)于Mybatis查詢時(shí)的延遲加載解析的文章就介紹到這了,更多相關(guān)Mybatis延遲加載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis-Plus自定義SQL的詳細(xì)過(guò)程記錄
Java開(kāi)發(fā)使用mybatis-plus來(lái)執(zhí)行sql操作,往往比mybatis能夠省時(shí)省力,下面這篇文章主要給大家介紹了關(guān)于MyBatis-Plus自定義SQL的相關(guān)資料,需要的朋友可以參考下2022-02-02Java實(shí)戰(zhàn)之晚會(huì)抽獎(jiǎng)系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Java語(yǔ)言編寫(xiě)一個(gè)晚會(huì)抽獎(jiǎng)系統(tǒng),文中采用到的技術(shù)有Jdbc、Servlert、JavaScript、JQuery、Ajax等,感興趣的可以學(xué)習(xí)一下2022-03-03Java基礎(chǔ)知識(shí)之CharArrayReader流的使用
這篇文章主要介紹了Java基礎(chǔ)知識(shí)之CharArrayReader流的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12SpringBoot使用AOP,內(nèi)部方法失效的解決方案
這篇文章主要介紹了SpringBoot使用AOP,內(nèi)部方法失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08SpringCloud微服務(wù)架構(gòu)升級(jí)匯總
這篇文章主要介紹了SpringCloud微服務(wù)架構(gòu)升級(jí)匯總,它提倡將單一應(yīng)用程序劃分成一組小的服務(wù),服務(wù)之間互相協(xié)調(diào)、互相配合,為用戶提供最終價(jià)值,需要的朋友可以參考下2019-06-06