延迟加载:

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

结果执行的查询语句:

mybatis中延迟加载Lazy策略的更多相关文章

  1. SSM-MyBatis-16:Mybatis中延迟加载

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 延迟加载: 所谓延迟加载是什么? 从字面意思理解到的是等一会再加载 从行为分析,他主要是缓解数据库压力,提高性 ...

  2. Mybatis中的延迟加载的使用方法

    Mybatis中的延迟加载的使用方法 在Mybatis中查询订单,然后带出商品信息和快递信息的配置方法 orderMapper.xml配置如下 <?xml version="1.0&q ...

  3. Mybatis中的N+1问题与延迟加载

    0.什么是N+1问题? 在查询中一下子取出所有属性,就会使数据库多执行几条毫无意义的SQL .实际中不需要把所有信息都加载进来,因为有些信息并不常用,加载它们会多执行几条毫无用处的 SQL,导致数据库 ...

  4. 【MyBatis学习11】MyBatis中的延迟加载

    1. 什么是延迟加载 举个例子:如果查询订单并且关联查询用户信息.如果先查询订单信息即可满足要求,当我们需要查询用户信息时再查询用户信息.把对用户信息的按需去查询就是延迟加载. 所以延迟加载即先从单表 ...

  5. 8.Mybatis的延迟加载

    在Mybatis中的延迟加载只有resultMap可以实现,ResultMap 可以实现高级映射(association,collection可以实现一对1和一对多的映射),他们具有延迟加载的功能,r ...

  6. Hibernate中延迟加载和缓存

    什么是延迟加载? 延迟加载是指当应用程序想要从数据库获取对象时(在没有设置lazy属性值为false),Hibernate只是从数据库获取符合条件的对象的OId从而生成代理对象,并没有加载出对象 访问 ...

  7. mybatis中resultMap配置细则

    resultMap算是mybatis映射器中最复杂的一个节点了,能够配置的属性较多,我们在mybatis映射器配置细则这篇博客中已经简单介绍过resultMap的配置了,当时我们介绍了resultMa ...

  8. mybatis的延迟加载、一级缓存、二级缓存

    mybatis的延迟加载.一级缓存.二级缓存 mybatis是什么? mybatis是一个持久层框架,是apache下的开源项目,前身是itbatis,是一个不完全的ORM框架,mybatis提供输入 ...

  9. Mybatis的延迟加载和缓存

    1. MyBatis中的延迟加载,也称为懒加载,是指在进行关联查询时,按照设置延迟加载规则推迟对关联对象的select查询.延迟加载可以有效的减少数据库压力.       注意:MyBatis的延迟加 ...

随机推荐

  1. IntelliJ IDEA调试时,格式化显示日期变量

    格式化前: 格式化后: new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this); 入门常用知识: Ct ...

  2. Numpy——进阶篇

    impoort numpy as np arr=np.arange(10) #输出奇数 arr[arr%2==1] #将arr中的所有奇数替换为-1,而不改变arr out=np.where(arr% ...

  3. ansible-playbook -l 选项

    -l <SUBSET>, --limit <SUBSET> further limit selected hosts to an additional pattern 限制脚本 ...

  4. [转帖]Oracle 查询各表空间使用情况--完善篇

    Oracle 查询各表空间使用情况--完善篇 链接:http://blog.itpub.net/28602568/viewspace-1770577/ 标题: Oracle 查询各表空间使用情况--完 ...

  5. qt 部分控件 setStyleSheet 使用总结

    刚用Qt不久,但是已经感受到Qt ui设计的便捷. 总结一下最近使用的控件,把它们setStyleSheet的使用方法记录下来. 主要使用到的工具有:QToolBar,QToolBox,QPushBu ...

  6. [集合]Map的 entrySet() 详解以及用法(四种遍历map的方式)

    Entry 由于Map中存放的元素均为键值对,故每一个键值对必然存在一个映射关系. Map中采用Entry内部类来表示一个映射项,映射项包含Key和Value (我们总说键值对键值对, 每一个键值对也 ...

  7. CSS 毛玻璃效果

    效果图: <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <met ...

  8. SpringAOP用到了什么代理,以及动态代理与静态代理的区别

    spring aop (面向切面)常用于数据库事务中,使用了2种代理. jdk动态代理:对实现了接口的类生成代理对象.要使用jdk动态代理,要求类必须要实现接口. cglib代理:对类生成代理对象. ...

  9. gorpeln的个人博客 - gorpeln

    2019-10-18    App Store 审核指南 2019-10-03    锚点跳转距离顶部指定距离 2019-09-23    Jekyll 简单加密 (pwd=123456) 2019- ...

  10. 30. Substring with Concatenation of All Words (JAVA)

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...