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.  }

结果执行的查询语句:



posted @ 2018-07-18 20:48  沫小淘  阅读(1606)  评论(0编辑  收藏  举报