mybatis中延遲加載Lazy策略的方法
lazy策略原理:只有在使用查詢sql返回的數(shù)據(jù)是才真正發(fā)出sql語句到數(shù)據(jù)庫,否則不發(fā)出(主要用在多表的聯(lián)合查詢)
1.一對(duì)一延遲加載:
假設(shè)數(shù)據(jù)庫中有person表和card表:其中person表中有字段pid,pname,page,psex,cid,card表中有字段cid,cnum;
假設(shè)要查詢某個(gè)人的姓名和身份證號(hào)碼:
原理:在查詢姓名時(shí),實(shí)際本沒有查詢出身份證號(hào)碼的信息,只有當(dāng)前臺(tái)使用身份證號(hào)時(shí)才發(fā)出對(duì)card的查詢,需要查詢出身份證號(hào)碼是采取查詢的一種策略;
實(shí)現(xiàn)實(shí)例:
實(shí)現(xiàn)步驟:
1-導(dǎo)入mybatis 的依賴jar包
2-添加log4j文件 (可查看內(nèi)存中實(shí)際執(zhí)行的程序)
1-原理:只有當(dāng)前臺(tái)使用身份證號(hào)時(shí)才發(fā)出對(duì)card的查詢,否則只發(fā)出person信息的查詢
2-開啟lazy:在conf.xml
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
3.實(shí)現(xiàn):
(1)在mapper.xml映射文件中:
<select id="findCid" parameterType="int" resultType="card">
select * from card where cid=#{value}
</select>
<resultMap type="person" id="p_c1">
<id column="pid" property="pid" />
<result column="pname" property="pname" />
<result column="page" property="page" />
<result column="psex" property="psex" />
<association property="card" javaType="card" select="findCid"
column="cid">
</association>
</resultMap>
<select id="selectpersonAndCardLazyByPid" parameterType="int"
resultMap="p_c1">
SELECT * FROM person where pid=#{value}
</select>
1-select:指定關(guān)聯(lián)的查詢語句
2-column:指定主語句中的哪個(gè)字段的值作為參數(shù)傳遞給從sql語句
(2)在mapper接口中定義方法:
public Person selectpersonAndCardLazyByPid(int pid);
(3)使用junit測(cè)試結(jié)果:
1.此處是只發(fā)出person信息的查詢;
@Test
public void testselectpersonAndCardLazyByPid(){//lazy策略一對(duì)1
Person p=pm.selectpersonAndCardLazyByPid(1);
//System.out.println(p);
System.out.println(p.getPname()+",");
//System.out.println(p.getPname()+","+p.getCard().getCnum());
}
結(jié)果執(zhí)行的查詢語句:

2.當(dāng)前臺(tái)使用身份證號(hào)時(shí)才發(fā)出對(duì)card的查詢
@Test
public void testselectpersonAndCardLazyByPid(){//lazy策略一對(duì)1
Person p=pm.selectpersonAndCardLazyByPid(1);
//System.out.println(p);
System.out.println(p.getPname()+",");
System.out.println(p.getPname()+","+p.getCard().getCnum());//當(dāng)前臺(tái)使用身份證號(hào)時(shí)才發(fā)出對(duì)card的查詢
}
結(jié)果執(zhí)行的查詢語句:

2.一對(duì)多延遲加載:
實(shí)現(xiàn)實(shí)例:
假設(shè)數(shù)據(jù)庫中有person表和card身份信息表,adder地址表:其中person表中有字段pid,pname,page,psex,cid,card表中有字段cid,cnum;adder表有字段aid,ashi,pid
假設(shè)要查詢某個(gè)人的姓名和住址,身份證號(hào)碼:
(1)mapper.xml映射文件:
<!-- lazy策略一對(duì)多 -->
<select id="fingCard_Adder" parameterType="int" resultType="adder">
select * from adder where pid=#{value}
</select>
<select id="findCid1" parameterType="int" resultType="card">
select * from card where cid=#{value}
</select>
<resultMap type="person" id="p_c1_a1">
<id column="pid" property="pid" />
<result column="pname" property="pname" />
<result column="page" property="page" />
<result column="psex" property="psex" />
<association property="card" javaType="card" select="findCid1"
column="cid">
</association>
<collection property="adder" ofType="Adder" select="fingCard_Adder"
column="pid">
</collection>
</resultMap>
<select id="selectpersonAndCardAndAdderLazyByPid" parameterType="int"
resultMap="p_c1_a1">
SELECT * FROM person where pid=#{value}
</select>
(2)mapper接口定義方法:
1.此處是只發(fā)出person信息的查詢;
@Test
public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一對(duì)多
Person p=pm.selectpersonAndCardAndAdderLazyByPid(1);
System.out.println(p.getPname()+",");////此處是只發(fā)出person信息的查詢
}
結(jié)果執(zhí)行的查詢語句:

2.此處是發(fā)出person信息和身份信息的查詢;
@Test
public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一對(duì)多
Person p=pm.selectpersonAndCardAndAdderLazyByPid(1);
System.out.println(p.getPname()+",");//此處是只發(fā)出person信息的查詢;
System.out.println(p.getPname()+","+p.getCard().getCnum());//此處是發(fā)出person信息和身份信息的查詢;
}
結(jié)果執(zhí)行的查詢語句:

3.此處發(fā)出person信息和身份信息,地址信息的查詢;
@Test
public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一對(duì)多
Person p=pm.selectpersonAndCardAndAdderLazyByPid(1);
System.out.println(p.getPname()+",");//此處是只發(fā)出person信息的查詢;
System.out.println(p.getPname()+","+p.getCard().getCnum());//此處是發(fā)出person信息和身份信息的查詢;
//System.out.println(p.getPname()+","+p.getCard().getCnum());
for (Adder adder : p.getAdder()) {////此處發(fā)出person信息和身份信息,地址信息的查詢;
System.out.println(adder.getAshi());
}
}
結(jié)果執(zhí)行的查詢語句:

總結(jié)
以上所述是小編給大家介紹的mybatis中延遲加載Lazy策略的方法,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
SpringBoot @Async如何自定義線程池及使用教程
這篇文章主要介紹了SpringBoot @Async如何自定義線程池及使用教程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
關(guān)于idea剛打開時(shí)瘋狂報(bào)錯(cuò)的問題
這篇文章主要介紹了關(guān)于idea剛打開時(shí)瘋狂報(bào)錯(cuò)的問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
java數(shù)據(jù)庫開發(fā)之JDBC基礎(chǔ)使用方法及實(shí)例詳解
這篇文章主要介紹了java數(shù)據(jù)庫開發(fā)之JDBC基礎(chǔ)知識(shí)詳解,需要的朋友可以參考下2020-02-02
Java 數(shù)據(jù)結(jié)構(gòu)線性表之順序存儲(chǔ)詳解原理
線性表的順序存儲(chǔ)是指用一組地址連續(xù)的存儲(chǔ)單元依次存儲(chǔ)線性表中的各個(gè)元素、使得線性表中在邏輯結(jié)構(gòu)上相鄰的數(shù)據(jù)元素存儲(chǔ)在相鄰的物理存儲(chǔ)單元中,即通過數(shù)據(jù)元素物理存儲(chǔ)的相鄰關(guān)系來反映數(shù)據(jù)元素之間邏輯上的相鄰關(guān)系2021-10-10
springboot定時(shí)任務(wù)@Scheduled執(zhí)行多次的問題
這篇文章主要介紹了springboot定時(shí)任務(wù)@Scheduled執(zhí)行多次問題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
如何在Mac上安裝并配置JDK環(huán)境變量詳細(xì)步驟
這篇文章主要介紹了如何在Mac上安裝并配置JDK環(huán)境變量詳細(xì)步驟,包括下載JDK、安裝JDK、配置環(huán)境變量、驗(yàn)證JDK配置以及可選地設(shè)置PowerShell為默認(rèn)終端,需要的朋友可以參考下2025-04-04
Spring Shell應(yīng)用程序開發(fā)流程解析
這篇文章主要介紹了Spring Shell應(yīng)用程序開發(fā)流程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10

