延迟加载:

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. C#新特性span 和 Tuple

    span 可用于高性能字符串分割等 https://www.cnblogs.com/lonelyxmas/p/10171869.html https://www.codemag.com/article ...

  2. Java课堂疑问解答与思考3

    一. 两对整数明明完全一样,为何一个输出true,一个输出false? 答: 整数在小于127时都可以用常量池,因此第一次比较的的地址是取自同一个地址的数字,而第二次比较的数是创建了两个不同地址的对象 ...

  3. FTP搭建YUM源服务器

    一.FTP搭建YUM源服务器 1.服务器 挂载centos镜像[root@localhost ~]#yum install vsftpd[root@localhost ~]#systemctl sta ...

  4. 无法在发送 HTTP 标头之后进行重定向

    public ActionResult Index2() { Response.Buffer = true; Response.Clear(); return Redirect("/Wech ...

  5. vue 格式化时间的插件库

    格式化时间的插件库 点击进入 moment.js网址 ,在这里可以找到需要引入的script标签 点击进入 moment.js的文档 在文档中可以找到对应的格式和例子 此文来源于: https://w ...

  6. [Web 前端] 034 计算属性,侦听属性

    目录 0. 方便起见,定个轮廓 1. 过滤器 2. 计算属性 2.1 2.2 3. 监听属性 0. 方便起见,定个轮廓 不妨记下方的程序为 code1 <!DOCTYPE html> &l ...

  7. JavaWeb返回Json格式数据JQuery Ajax无法解析的问题

    今天在写实验室的傻逼Java Web小项目的时候,有一个需要发布内容的地方,因为想做的让用户感觉优雅一点 所以就是用了Ajax来做,本来很简单的一个小玩意,竟然花了半个多小时的时间,主要是将时间花在了 ...

  8. net 架构师-数据库-sql server-003-T-SQL 基本语句

    3.1 基本SELECT语句 SELECT [ALL|DISTINCT] [TOP (<expression>)  [PERCENT] [WITH TIES]] <coloumn  ...

  9. springboot工程启动时,报错:No bean named 'shiroFilter' available

    在启动Springboot项目时,报错:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ' ...

  10. 最长上升子序列(LIS) Easy

    A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given ...