MyBatis延迟加载,分层加载和局部延迟加载
文章图片
延迟加载,玩过hibernate的都知道那玩意叫懒加载。
【MyBatis延迟加载,分层加载和局部延迟加载】在最新官方MyBatis文档里,有上面这2个属性,一个是延迟加载,一个是分层加载。
lazyLoadingEnabled 默认值为false,那么在有级联关系的resultMap里,查询后会加载出所有的级联关系,当然有时候我们并不需要这些所有的时候,我们就可以应用到延迟加载给我们带来的好处了。
aggressiveLazyLoading默认值是true,这里我称之为分层加载,大概意思是如果它为true,那么当我使用了延迟加载,要么所有级联都不记载,要么如果我加载一个,其他都得加载
fetchType是可以注明在association 和 collection里的,选值为eager和lazy,就是为了方便我们结合aggressiveLazyLoading(false)来配合使用的,让延迟加载发挥到极致,即只加载我需要的!
以下是我做得一个demo
文章图片
这是实验要用到的3个表模型
以下是resultMap对应的POJO
package Model;
import java.io.Serializable;
import java.util.List;
public class Student implements Serializable{
private Integer id;
private String studentName;
private String studentAge;
private List studentHealthCards;
private ParentOfStudent parentOfStudent;
public List getStudentHealthCards() {
return studentHealthCards;
}
public void setStudentHealthCards(List studentHealthCards) {
this.studentHealthCards = studentHealthCards;
}
public ParentOfStudent getParentOfStudent() {
return parentOfStudent;
}
public void setParentOfStudent(ParentOfStudent parentOfStudent) {
this.parentOfStudent = parentOfStudent;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentAge() {
return studentAge;
}
public void setStudentAge(String studentAge) {
this.studentAge = studentAge;
}
@Override
public String toString() {
return "Student [id=" + id + ", studentName=" + studentName + ", studentAge=" + studentAge
+ ", studentHealthCards=" + studentHealthCards + ", parentOfStudent=" + parentOfStudent + "]";
}
}package Model;
import java.io.Serializable;
public class StudentHealthCard implements Serializable{
private Integer id;
private Integer stu_id;
private String name;
private String message;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getStu_id() {
return stu_id;
}
public void setStu_id(Integer stu_id) {
this.stu_id = stu_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "StudentHealthCard [id=" + id + ", stu_id=" + stu_id + ", name=" + name + ", message=" + message + "]";
}
}package Model;
import java.io.Serializable;
public class ParentOfStudent implements Serializable{
private Integer id;
private Integer stu_id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getStu_id() {
return stu_id;
}
public void setStu_id(Integer stu_id) {
this.stu_id = stu_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "ParentOfStudent [id=" + id + ", stu_id=" + stu_id + ", name=" + name + "]";
}
}
select * from student where id = #{id}select * from parentofstudent where stu_id = #{id}select * from studenthealthcard where stu_id = #{id}
情况1:开启延迟加载,默认分层加载,不开启局部加载
执行语句 Student student = sm.selectOneStudent(1);
以下是运行结果:
![MyBatis延迟加载,分层加载和局部延迟加载](https://img.it610.com/image/info8/462c124c264140289249cb3a2abb00bd.png)
文章图片
执行语句:Student student = sm.selectOneStudent(1);
student.getParentOfStudent();
运行结果:
![MyBatis延迟加载,分层加载和局部延迟加载](https://img.it610.com/image/info8/842450b295f043e5bc1e66834235d78c.png)
文章图片
这就是默认分层加载的后果,好的那么现在我把分层加载设置为false
即情况2:开启延迟加载,分层加载false,不适用局部加载
执行以上相同语句:
![MyBatis延迟加载,分层加载和局部延迟加载](https://img.it610.com/image/info8/dcb3f85d55c1494e9b3609427fb11490.png)
文章图片
![MyBatis延迟加载,分层加载和局部延迟加载](https://img.it610.com/image/info8/43c9dc8e23ac405a9fbfcfaa38a3921d.png)
文章图片
好了 3条sql变成了2条
情况3:不列出来了,就是使用fetchType的情况下,可以指明及时在延迟加载情况下也可以立即加载某个级联关系
推荐阅读
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- mybatisplus如何在xml的连表查询中使用queryWrapper
- mybatisplus|mybatisplus where QueryWrapper加括号嵌套查询方式
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- 使用composer自动加载类文件
- 众泰T500智能互联双加载,让汽车生活更有趣
- ImageLoaders 加载图像
- MyBatis|MyBatis Generator配置
- 类的加载过程