MyBatis延迟加载,分层加载和局部延迟加载

延迟加载,玩过hibernate的都知道那玩意叫懒加载。

在最新官方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 + "]";
	}
	
}


		  	
		  	
		  	
		  	
		  	
		  
		  
		  
		  
		  
		  
		  


情况1:开启延迟加载,默认分层加载,不开启局部加载

执行语句 Student student = sm.selectOneStudent(1);

以下是运行结果:


执行语句:Student student = sm.selectOneStudent(1);
student.getParentOfStudent();

运行结果:MyBatis延迟加载,分层加载和局部延迟加载_第1张图片

这就是默认分层加载的后果,好的那么现在我把分层加载设置为false

即情况2:开启延迟加载,分层加载false,不适用局部加载

执行以上相同语句:


MyBatis延迟加载,分层加载和局部延迟加载_第2张图片

好了 3条sql变成了2条

情况3:不列出来了,就是使用fetchType的情况下,可以指明及时在延迟加载情况下也可以立即加载某个级联关系

你可能感兴趣的:(mybatis)