MyBatis深入解讀懶加載的實(shí)現(xiàn)
懶加載 ,也稱為嵌套查詢
需要查詢關(guān)聯(lián)信息時(shí),使用 Mybatis 懶加載特性可有效的減少數(shù)據(jù)庫(kù)壓力, 首次查詢只查詢主表信息,關(guān)聯(lián)表的信息在用戶獲取時(shí)再加載。
Mybatis 一對(duì)一關(guān)聯(lián)的 association 和一對(duì)多的 collection 可以實(shí)現(xiàn)懶加載。懶加載時(shí)要 使用resultMap,不能使用 resultType 。
這里我們以員工表和部門表為例
通過deptId 與 部門表 id 關(guān)聯(lián)
我們這里首先需要開啟一個(gè)設(shè)置
<settings> <!--指定哪些方法去觸發(fā)延遲加載,hashCode,equals,clone,toString--> <setting name="lazyLoadTriggerMethods" value=""/> </settings>
懶加載功能是默認(rèn)開啟的, 但這里我們也需要設(shè)置這個(gè)屬性, 不設(shè)置則不會(huì)觸發(fā)延遲加載功能
Employee selectOneEmployee(int id);
我們以查詢單個(gè)員工為例 , resultMap 與sql 如下
<!--定義resultMap--> <resultMap id="employeeMap1" type="Employee"> <id column="id" property="id"/> <result property="name" column="name"/> <result property="age" column="age"/> <!--fetchType為查詢的類型,這里選擇lazy select為嵌套查詢--> <association property="dept" javaType="Dept" fetchType="lazy" select="selectDept" column="deptId"> <result column="name" property="name"/> </association> </resultMap> <select id="selectOneEmployee" resultMap="employeeMap1"> select id,name,age,deptId from employee where id=#{id} </select> <!--通過上一級(jí)sql提供的deptId查詢--> <select id="selectDept" resultType="Dept"> select name from dept where id=#{deptId} </select>
此處一對(duì)一 ,我們使用<association>
java測(cè)試 :
public static void main(String[] args) { SqlSession sqlSession= MybatisUtil.getSqlSession(); EmployeeDao mapper=sqlSession.getMapper(EmployeeDao.class); Employee employee = mapper.selectOneEmployee(3); System.out.println(employee); System.out.println(employee.getDept()); sqlSession.commit(); //提交事務(wù) sqlSession.close(); //關(guān)閉
查詢結(jié)果 :
通過結(jié)果可以看到 , 當(dāng)我們第一次輸出這個(gè) employee 對(duì)象時(shí), 部門是沒有被查詢的 , 而當(dāng)我們需要使用到部門的信息時(shí), 才會(huì)去觸發(fā)這個(gè)查詢
查詢部門 resultMap 與 sql如下:
<resultMap id="deptMap1" type="Dept"> <id column="id" property="id"/> <result column="name" property="name"/> <!--collection為一對(duì)多 , 這里一個(gè)部門包含多個(gè)員工--> <collection property="list" javaType="List" ofType="Employee" select="selectEmployee" fetchType="lazy" column="id"> <result property="name" column="name"/> </collection> </resultMap> <select id="selectOneDept" resultMap="deptMap1"> SELECT id,name FROM dept where id=#{id} </select> <select id="selectEmployee" resultType="Employee"> select name from employee where deptId=#{id} </select>
一對(duì)多,我們使用<collection>
懶加載就介紹到這里,感謝閱讀
到此這篇關(guān)于MyBatis深入解讀懶加載的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis懶加載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java時(shí)間段查詢將00:00:00更換成23:59:59
本文主要介紹了java時(shí)間段查詢將00:00:00更換成23:59:59,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01淺談Storm在zookeeper上的目錄結(jié)構(gòu)
這篇文章主要介紹了淺談Storm在zookeeper上的目錄結(jié)構(gòu)的相關(guān)內(nèi)容,涉及storm使用zookeeper的操作以及詳細(xì)結(jié)構(gòu)圖,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10分布式系統(tǒng)中的降級(jí)熔斷設(shè)計(jì)問題面試
這篇文章主要為大家介紹了分布式系統(tǒng)中的降級(jí)熔斷設(shè)計(jì)問題面試解答,有需要的朋友可以借鑒參考下,希望能有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03SpringBoot配置log4j2的實(shí)現(xiàn)示例
SpringBoot中默認(rèn)使用Logback作為日志框架,本文主要介紹了SpringBoot配置log4j2的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12基于@RequestParam與@RequestBody使用對(duì)比
這篇文章主要介紹了@RequestParam與@RequestBody的使用對(duì)比,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10