mybatis中延遲加載Lazy策略


延遲加載:

lazy策略原理:只有在使用查詢sql返回的數據是才真正發出sql語句到數據庫,否則不發出(主要用在多表的聯合查詢)

1.一對一延遲加載:

假設數據庫中有person表和card表:其中person表中有字段pid,pname,page,psex,cid,card表中有字段cid,cnum;

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

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

實現實例:

實現步驟:

       1-導入mybatis 的依賴jar包
       2-添加log4j文件 (可查看內存中實際執行的程序)

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

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

3.實現:

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

 

  1. < select id="findCid" parameterType="int" resultType="card">
  2. select * from card where cid=#{value}
  3. </ select>
  4. < resultMap type="person" id="p_c1">
  5. < id column="pid" property="pid" />
  6. < result column="pname" property="pname" />
  7. < result column="page" property="page" />
  8. < result column="psex" property="psex" />
  9. < association property="card" javaType="card" select="findCid"
  10.  column= "cid">
  11.  </ association>
  12.   </resultMap>
  13.   <select id="selectpersonAndCardLazyByPid" parameterType="int"
  14.  resultMap= "p_c1">
  15.  SELECT * FROM person where pid=#{value}
  16.   </select>

     1-select:指定關聯的查詢語句
     2-column:指定主語句中的哪個字段的值作為參數傳遞給從sql語句

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

public Person selectpersonAndCardLazyByPid(int pid);

(3)使用junit測試結果:

1.此處是只發出person信息的查詢;

 

  1.   @Test
  2.   public void testselectpersonAndCardLazyByPid(){//lazy策略一對1
  3.   
  4.  Person p=pm.selectpersonAndCardLazyByPid( 1);
  5.  //System.out.println(p);
  6.  System.out.println(p.getPname()+ ",");
  7.  //System.out.println(p.getPname()+","+p.getCard().getCnum());
  8.   
  9.   
  10.  }

結果執行的查詢語句:

2.當前台使用身份證號時才發出對card的查詢

 

  1.   @Test
  2.   public void testselectpersonAndCardLazyByPid(){//lazy策略一對1
  3.   
  4.  Person p=pm.selectpersonAndCardLazyByPid( 1);
  5.  //System.out.println(p);
  6.  System.out.println(p.getPname()+ ",");
  7.  System.out.println(p.getPname()+ ","+p.getCard().getCnum());//當前台使用身份證號時才發出對card的查詢 
  8.   
  9.  
  10.  }

結果執行的查詢語句:


2.一對多延遲加載:

 

實現實例:

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

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

(1)mapper.xml映射文件:

 

  1.   <!-- lazy策略一對多 -->
  2.   <select id="fingCard_Adder" parameterType="int" resultType="adder">
  3.  select * from adder where pid=#{value}
  4.   </select>
  5.   <select id="findCid1" parameterType="int" resultType="card">
  6.  select * from card where cid=#{value}
  7.   </select>
  8.   <resultMap type="person" id="p_c1_a1">
  9.  < id column="pid" property="pid" />
  10.  < result column="pname" property="pname" />
  11.  < result column="page" property="page" />
  12.  < result column="psex" property="psex" />
  13.  < association property="card" javaType="card" select="findCid1"
  14.  column= "cid">
  15.  </ association>
  16.  < collection property="adder" ofType="Adder" select="fingCard_Adder"
  17.  column= "pid">
  18.  </ collection>
  19.  
  20.   </resultMap>
  21.   <select id="selectpersonAndCardAndAdderLazyByPid" parameterType="int"
  22.  resultMap= "p_c1_a1">
  23.  SELECT * FROM person where pid=#{value}
  24.   </select>

(2)mapper接口定義方法:

1.此處是只發出person信息的查詢;

 

  1.   @Test
  2.   public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一對多
  3.   
  4.  Person p=pm.selectpersonAndCardAndAdderLazyByPid( 1);
  5.  System.out.println(p.getPname()+ ",");////此處是只發出person信息的查詢
  6.   
  7.   
  8.  }

結果執行的查詢語句:

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

 

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

結果執行的查詢語句:

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

 

  1.   @Test
  2.   public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一對多
  3.   
  4.  Person p=pm.selectpersonAndCardAndAdderLazyByPid( 1);
  5.  System.out.println(p.getPname()+ ",");//此處是只發出person信息的查詢;
  6.  System.out.println(p.getPname()+ ","+p.getCard().getCnum());//此處是發出person信息和身份信息的查詢;
  7.   //System.out.println(p.getPname()+","+p.getCard().getCnum());
  8.   for (Adder adder : p.getAdder()) {////此處發出person信息和身份信息,地址信息的查詢;
  9.  System.out.println(adder.getAshi());
  10.  }
  11.   
  12.  }

結果執行的查詢語句:




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM