欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂java培訓(xùn)mybatis中延遲加載Lazy策略的方法

2018-06-27 10:26:15 1220瀏覽

lazy策略原理:只有在使用查詢sql返回的數(shù)據(jù)是才真正發(fā)出sql語(yǔ)句到數(shù)據(jù)庫(kù),否則不發(fā)出(主要用在多表的聯(lián)合查詢)



1.一對(duì)一延遲加載:

假設(shè)數(shù)據(jù)庫(kù)中有person表和card表:其中person表中有字段pid,pname,page,psex,cid,card表中有字段cid,cnum;

假設(shè)要查詢某個(gè)人的姓名和身份證號(hào)碼:

原理:在查詢姓名時(shí),實(shí)際本沒(méi)有查詢出身份證號(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-開(kāi)啟lazy:在conf.xml

<settings>
<settingname="lazyLoadingEnabled"value="true"/>
<settingname="aggressiveLazyLoading"value="false"/>
</settings>

3.實(shí)現(xiàn):

(1)在mapper.xml映射文件中:

  <selectid="findCid"parameterType="int"resultType="card">  select*fromcardwherecid=#{value}
  </select>
  <resultMaptype="person"id="p_c1">
  <idcolumn="pid"property="pid"/>
  <resultcolumn="pname"property="pname"/>
  <resultcolumn="page"property="page"/>
  <resultcolumn="psex"property="psex"/>
  <associationproperty="card"javaType="card"select="findCid"
  column="cid">
  </association>
  </resultMap>
 <selectid="selectpersonAndCardLazyByPid"parameterType="int"
  resultMap="p_c1">
  SELECT*FROMpersonwherepid=#{value}
  </select>


1-select:指定關(guān)聯(lián)的查詢語(yǔ)句

2-column:指定主語(yǔ)句中的哪個(gè)字段的值作為參數(shù)傳遞給從sql語(yǔ)句

(2)在mapper接口中定義方法:

publicPersonselectpersonAndCardLazyByPid(intpid);


(3)使用junit測(cè)試結(jié)果:

1.此處是只發(fā)出person信息的查詢;

@Test
publicvoidtestselectpersonAndCardLazyByPid(){//lazy策略一對(duì)1
Personp=pm.selectpersonAndCardLazyByPid(1);
//System.out.println(p);
System.out.println(p.getPname()+",");
//System.out.println(p.getPname()+","+p.getCard().getCnum());
}


2.當(dāng)前臺(tái)使用身份證號(hào)時(shí)才發(fā)出對(duì)card的查詢

@Test
publicvoidtestselectpersonAndCardLazyByPid(){//lazy策略一對(duì)1
Personp=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的查詢
}


2.一對(duì)多延遲加載:

實(shí)現(xiàn)實(shí)例:

假設(shè)數(shù)據(jù)庫(kù)中有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ì)多-->

<selectid="fingCard_Adder"parameterType="int"resultType="adder">
select*fromadderwherepid=#{value}
</select>
<selectid="findCid1"parameterType="int"resultType="card">
select*fromcardwherecid=#{value}
</select>
<resultMaptype="person"id="p_c1_a1">
<idcolumn="pid"property="pid"/>
<resultcolumn="pname"property="pname"/>
<resultcolumn="page"property="page"/>
<resultcolumn="psex"property="psex"/>
<associationproperty="card"javaType="card"select="findCid1"
column="cid">
</association>
<collectionproperty="adder"ofType="Adder"select="fingCard_Adder"
column="pid">
</collection>
</resultMap>
<selectid="selectpersonAndCardAndAdderLazyByPid"parameterType="int"
resultMap="p_c1_a1">
SELECT*FROMpersonwherepid=#{value}
</select>

(2)mapper接口定義方法:

1.此處是只發(fā)出person信息的查詢;

@Test
publicvoidtestselectpersonAndCardAndAdderLazyByPid(){//lazy策略一對(duì)多
Personp=pm.selectpersonAndCardAndAdderLazyByPid(1);
System.out.println(p.getPname()+",");////此處是只發(fā)出person信息的查詢
}




2.此處是發(fā)出person信息和身份信息的查詢;

@Test
publicvoidtestselectpersonAndCardAndAdderLazyByPid(){//lazy策略一對(duì)多
Personp=pm.selectpersonAndCardAndAdderLazyByPid(1);
System.out.println(p.getPname()+",");//此處是只發(fā)出person信息的查詢;
System.out.println(p.getPname()+","+p.getCard().getCnum());//此處是發(fā)出person信息和身份信息的查詢;
}


3.此處發(fā)出person信息和身份信息,地址信息的查詢;

@Test
publicvoidtestselectpersonAndCardAndAdderLazyByPid(){//lazy策略一對(duì)多
Personp=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(Adderadder:p.getAdder()){////此處發(fā)出person信息和身份信息,地址信息的查詢;
System.out.println(adder.getAshi());
}
}



以上所述是大家介紹的mybatis中延遲加載Lazy策略的方法,希望對(duì)大家有所幫助,如果對(duì)java感興趣的話,或者想了解更多的java資訊的話,請(qǐng)登錄扣丁學(xué)堂官網(wǎng)或關(guān)注微信公眾號(hào),更有大量java在線視頻教程來(lái)觀看。



扣丁學(xué)堂微信公眾號(hào)

【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】




查看更多關(guān)于“Java開(kāi)發(fā)資訊的相關(guān)文章>>



標(biāo)簽: java java培訓(xùn) java在線視頻教程 java編程

熱門專區(qū)

暫無(wú)熱門資訊

課程推薦

微信
微博
15311698296

全國(guó)免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號(hào)中關(guān)村智誠(chéng)科創(chuàng)大廈4層
京ICP備2021002079號(hào)-2   Copyright ? 2017 - 2022
返回頂部 返回頂部