2018-06-27 10:26:15 1220瀏覽
lazy策略原理:只有在使用查詢sql返回的數(shù)據(jù)是才真正發(fā)出sql語(yǔ)句到數(shù)據(jù)庫(kù),否則不發(fā)出(主要用在多表的聯(lián)合查詢)
<settings> <settingname="lazyLoadingEnabled"value="true"/> <settingname="aggressiveLazyLoading"value="false"/> </settings>
<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>
publicPersonselectpersonAndCardLazyByPid(intpid);
@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()); }
@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的查詢 }
<!--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>
@Test publicvoidtestselectpersonAndCardAndAdderLazyByPid(){//lazy策略一對(duì)多 Personp=pm.selectpersonAndCardAndAdderLazyByPid(1); System.out.println(p.getPname()+",");////此處是只發(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信息和身份信息的查詢; }
@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)觀看。
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“Java開(kāi)發(fā)資訊”的相關(guān)文章>>