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映射文件中:
- <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:指定关联的查询语句
2-column:指定主语句中的哪个字段的值作为参数传递给从sql语句
(2)在mapper接口中定义方法:
public Person selectpersonAndCardLazyByPid(int pid);
(3)使用junit测试结果:
1.此处是只发出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());
- }
结果执行的查询语句:
2.当前台使用身份证号时才发出对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());//当前台使用身份证号时才发出对card的查询
- }
结果执行的查询语句:
2.一对多延迟加载:
实现实例:
假设数据库中有person表和card身份信息表,adder地址表:其中person表中有字段pid,pname,page,psex,cid,card表中有字段cid,cnum;adder表有字段aid,ashi,pid
假设要查询某个人的姓名和住址,身份证号码:
(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.此处是只发出person信息的查询;
- @Test
- public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一对多
- Person p=pm.selectpersonAndCardAndAdderLazyByPid(1);
- System.out.println(p.getPname()+",");////此处是只发出person信息的查询
- }
结果执行的查询语句:
2.此处是发出person信息和身份信息的查询;
- @Test
- public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一对多
- Person p=pm.selectpersonAndCardAndAdderLazyByPid(1);
- System.out.println(p.getPname()+",");//此处是只发出person信息的查询;
- System.out.println(p.getPname()+","+p.getCard().getCnum());//此处是发出person信息和身份信息的查询;
- }
结果执行的查询语句:
3.此处发出person信息和身份信息,地址信息的查询;
- @Test
- public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一对多
- Person p=pm.selectpersonAndCardAndAdderLazyByPid(1);
- System.out.println(p.getPname()+",");//此处是只发出person信息的查询;
- System.out.println(p.getPname()+","+p.getCard().getCnum());//此处是发出person信息和身份信息的查询;
- //System.out.println(p.getPname()+","+p.getCard().getCnum());
- for (Adder adder : p.getAdder()) {////此处发出person信息和身份信息,地址信息的查询;
- System.out.println(adder.getAshi());
- }
- }
结果执行的查询语句:
mybatis中延迟加载Lazy策略的更多相关文章
- SSM-MyBatis-16:Mybatis中延迟加载
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 延迟加载: 所谓延迟加载是什么? 从字面意思理解到的是等一会再加载 从行为分析,他主要是缓解数据库压力,提高性 ...
- Mybatis中的延迟加载的使用方法
Mybatis中的延迟加载的使用方法 在Mybatis中查询订单,然后带出商品信息和快递信息的配置方法 orderMapper.xml配置如下 <?xml version="1.0&q ...
- Mybatis中的N+1问题与延迟加载
0.什么是N+1问题? 在查询中一下子取出所有属性,就会使数据库多执行几条毫无意义的SQL .实际中不需要把所有信息都加载进来,因为有些信息并不常用,加载它们会多执行几条毫无用处的 SQL,导致数据库 ...
- 【MyBatis学习11】MyBatis中的延迟加载
1. 什么是延迟加载 举个例子:如果查询订单并且关联查询用户信息.如果先查询订单信息即可满足要求,当我们需要查询用户信息时再查询用户信息.把对用户信息的按需去查询就是延迟加载. 所以延迟加载即先从单表 ...
- 8.Mybatis的延迟加载
在Mybatis中的延迟加载只有resultMap可以实现,ResultMap 可以实现高级映射(association,collection可以实现一对1和一对多的映射),他们具有延迟加载的功能,r ...
- Hibernate中延迟加载和缓存
什么是延迟加载? 延迟加载是指当应用程序想要从数据库获取对象时(在没有设置lazy属性值为false),Hibernate只是从数据库获取符合条件的对象的OId从而生成代理对象,并没有加载出对象 访问 ...
- mybatis中resultMap配置细则
resultMap算是mybatis映射器中最复杂的一个节点了,能够配置的属性较多,我们在mybatis映射器配置细则这篇博客中已经简单介绍过resultMap的配置了,当时我们介绍了resultMa ...
- mybatis的延迟加载、一级缓存、二级缓存
mybatis的延迟加载.一级缓存.二级缓存 mybatis是什么? mybatis是一个持久层框架,是apache下的开源项目,前身是itbatis,是一个不完全的ORM框架,mybatis提供输入 ...
- Mybatis的延迟加载和缓存
1. MyBatis中的延迟加载,也称为懒加载,是指在进行关联查询时,按照设置延迟加载规则推迟对关联对象的select查询.延迟加载可以有效的减少数据库压力. 注意:MyBatis的延迟加 ...
随机推荐
- 【MM系列】SAP MRKO如何操作
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MRKO如何操作 前言部 ...
- Our growth depends not on how many experiences we devour, but on how manywe digest.
rot. v/n. 腐烂 vibration.n. 震动 charcoal. n 木炭 wrinkle. v. 长皱纹 geometry. n. 几何学 walnut.n. 核桃 tailor. n. ...
- 【VS开发】C++调用外部程序
关于三个SDK函数:WinExec, ShellExecute,CreateProcess的其他注意事项:[1]定义头文件必须定义以下两个头文件: [cpp] view plain copy #inc ...
- 深入理解java:1.3. 垃圾收集
Java垃圾收集(Garbage Collection,GC) 某一个时点,一个对象如果有一个以上的引用(Rreference)指向它,那么该对象就为活着的(Live), 否则死亡(Dead),视为垃 ...
- [Python3] 001 "Hello World" 与注释
目录 1. 致敬 1.1 致敬 "Hello World" 1.2 致敬 Python 之父 Guido van Rossum 2. 注释 2.1 单行注释 2.2 多行注释 3. ...
- 终于有人把 Docker 讲清楚了,万字详解!
一.简介 1.了解Docker的前生LXC LXC为Linux Container的简写.可以提供轻量级的虚拟化,以便隔离进程和资源,而且不需要提供指令解释机制以及全虚拟化的其他复杂性.相当于C++中 ...
- 逆向工程 生成mapper 接口的 重要方法
@Test public void testSelectByExample() { ItemsExample itemsExample = new ItemsExample(); ItemsExamp ...
- PHP之简单工厂模式(二)
定义 简单工厂模式,通过定义一个工厂类,负责完成类实例的创建,根据参数的不同返回不同的类实例.对外部来讲,只需传入一个正常的参数就可以获得想要的对象,而不必需要具体创建细节.创建类实例的方法通常为静态 ...
- 06: django+celery+redis
目录: 1.1 Celery介绍 1.2 celery 组件 1.3 安装相关包 与 管理命令 1.4 celery与Django执行异步任务 1.5 在django中使用计划任务功能 1.1 Cel ...
- [LeetCode] 65. 有效数字
题目链接 : https://leetcode-cn.com/problems/valid-number/ 题目描述: 验证给定的字符串是否可以解释为十进制数字. 例如: "0"` ...