怎么在mybatis中延遲加載Lazy策略

這篇文章給大家介紹怎么在mybatis中延遲加載Lazy策略,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

成都網(wǎng)站建設(shè)、網(wǎng)站制作介紹好的網(wǎng)站是理念、設(shè)計和技術(shù)的結(jié)合。創(chuàng)新互聯(lián)擁有的網(wǎng)站設(shè)計理念、多方位的設(shè)計風(fēng)格、經(jīng)驗豐富的設(shè)計團(tuán)隊。提供PC端+手機(jī)端網(wǎng)站建設(shè),用營銷思維進(jìn)行網(wǎng)站設(shè)計、采用先進(jìn)技術(shù)開源代碼、注重用戶體驗與SEO基礎(chǔ),將技術(shù)與創(chuàng)意整合到網(wǎng)站之中,以契合客戶的方式做到創(chuàng)意性的視覺化效果。

1.一對一延遲加載:

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

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

原理:在查詢姓名時,實際本沒有查詢出身份證號碼的信息,只有當(dāng)前臺使用身份證號時才發(fā)出對card的查詢,需要查詢出身份證號碼是采取查詢的一種策略;

實現(xiàn)實例:

實現(xiàn)步驟:

       1-導(dǎo)入mybatis 的依賴jar包

       2-添加log4j文件 (可查看內(nèi)存中實際執(zhí)行的程序)

1-原理:只有當(dāng)前臺使用身份證號時才發(fā)出對card的查詢,否則只發(fā)出person信息的查詢
          2-開啟lazy:在conf.xml

 <settings>
     <setting name="lazyLoadingEnabled" value="true"/>
     <setting name="aggressiveLazyLoading" value="false"/>
   </settings>

3.實現(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:指定主語句中的哪個字段的值作為參數(shù)傳遞給從sql語句

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

public Person selectpersonAndCardLazyByPid(int pid);

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

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

@Test 
  public void testselectpersonAndCardLazyByPid(){//lazy策略一對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í)行的查詢語句:

怎么在mybatis中延遲加載Lazy策略

2.當(dāng)前臺使用身份證號時才發(fā)出對card的查詢

@Test 
  public void testselectpersonAndCardLazyByPid(){//lazy策略一對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)前臺使用身份證號時才發(fā)出對card的查詢 
  }

結(jié)果執(zhí)行的查詢語句:

怎么在mybatis中延遲加載Lazy策略

2.一對多延遲加載:

實現(xiàn)實例:

假設(shè)數(shù)據(jù)庫中有person表和card身份信息表,adder地址表:其中person表中有字段pid,pname,page,psex,cid,card表中有字段cid,cnum;adder表有字段aid,ashi,pid

假設(shè)要查詢某個人的姓名和住址,身份證號碼:

(1)mapper.xml映射文件:

<!-- lazy策略一對多 --> 
  <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策略一對多 
  Person p=pm.selectpersonAndCardAndAdderLazyByPid(1); 
  System.out.println(p.getPname()+",");////此處是只發(fā)出person信息的查詢 
}

結(jié)果執(zhí)行的查詢語句:

怎么在mybatis中延遲加載Lazy策略

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

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

 結(jié)果執(zhí)行的查詢語句:

怎么在mybatis中延遲加載Lazy策略

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

@Test 
public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一對多 
  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í)行的查詢語句:

怎么在mybatis中延遲加載Lazy策略

關(guān)于怎么在mybatis中延遲加載Lazy策略就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)頁名稱:怎么在mybatis中延遲加載Lazy策略
標(biāo)題來源:http://muchs.cn/article18/pdhodp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站收錄、靜態(tài)網(wǎng)站全網(wǎng)營銷推廣、微信公眾號小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司