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
|
1
2
3
4
|
<settings>
<setting name="lazyloadingenabled" value="true"/>
<setting name="aggressivelazyloading" value="false"/>
</settings>
|
3.实现:
(1)在mapper.xml映射文件中:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<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接口中定义方法:
|
1
|
public person selectpersonandcardlazybypid(int pid);
|
(3)使用junit测试结果:
1.此处是只发出person信息的查询;
|
1
2
3
4
5
6
7
|
@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的查询
|
1
2
3
4
5
6
7
|
@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映射文件:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<!-- 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信息的查询;
|
1
2
3
4
5
|
@test
public void testselectpersonandcardandadderlazybypid(){//lazy策略一对多
person p=pm.selectpersonandcardandadderlazybypid(1);
system.out.println(p.getpname()+",");////此处是只发出person信息的查询
}
|
结果执行的查询语句:
2.此处是发出person信息和身份信息的查询;
|
1
2
3
4
5
6
|
@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信息和身份信息,地址信息的查询;
|
1
2
3
4
5
6
7
8
9
10
|
@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策略的方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
原文链接:https://blog.csdn.net/m0_37905429/article/details/77074312
相关文章
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-06-04 95
-
2025-05-26 83
-
2025-06-04 88
-
2025-05-25 37
-
2025-05-29 100






