视频制作软件排行榜,泉州seo按天计费,市场营销策划案例经典大全,江门市住房和城乡建设局门户网站本文是自己的学习笔记#xff0c;主要参考以下资料
- 马士兵教育 1、延迟加载2、开启延迟加载2.1、配置信息2.2、查询语法2.2.1、前置条件2.2.2、xml语法2.2.3、总结 1、延迟加载
延迟加载是用于优化一对多或者多对多的查询。
比如员工表和部门表#xff0c;员工表left jo…本文是自己的学习笔记主要参考以下资料
- 马士兵教育 1、延迟加载2、开启延迟加载2.1、配置信息2.2、查询语法2.2.1、前置条件2.2.2、xml语法2.2.3、总结 1、延迟加载
延迟加载是用于优化一对多或者多对多的查询。
比如员工表和部门表员工表left join部门表一条SQL查出所有数据。
但有时候部门表的使用频率很低这时候我们就可以使用延迟加载先查出员工数据等需要用到部门数据的时候再执行剩下的查询。
2、开启延迟加载
2.1、配置信息
延迟加载与这两个属性有关。
lazyLoadingEnabled: ture|falsetrue开启延迟加载。与aggressiveLoadingEnabled是相反的属性。aggressiveLoadingEnabled: ture|falsetrue开启全加载。与lazyLoadingEnabled是相反的属性。
在mybatis.xml中configuration标签下这样设置可开启全局延迟加载。
configurationsettingssetting namelazyLoadingEnabled valuetrue/setting nameaggressiveLoadingEnabled valuefalse//settings
/configuration2.2、查询语法
2.2.1、前置条件
现有两个POJOEmp--员工Dept--部门。两者的类如下数据库字段同名。
public class Dept {private Integer deptid;private String dname;
}
public class Emp {private Integer id;private String name;private Integer age;private Dept dept;
}2.2.2、xml语法
现在是查员工表left join出部门表。部门数据延迟加载。
我们不能直接使用left join关联数据因为数据库语句一执行就会查出所有数据不会收到lazyLoadingEnabled的影响。
我们需要将原来的left join拆成两个查询然后在配置中关联。这样查询过程就受mybatis的控制。
需要拆成两个查询一个是查询员工表另一个是查询部门表。
select idqueryDeptByIdselect * from t_dept where deptid #{deptid}
/selectselect idqueryEmp resultMapbaseMap1select * from t_emp
/select之后定义返回值也是在这里定义关联关系。
resultMap idbaseMap typeempid columnid propertyid/result columnname propertyname/result columnage propertyage/association propertydept javaTypeDept columndeptId selectqueryDeptByIdid columndeptid propertydeptid/result columndname propertydname//association
/resultMap
2.2.3、总结 resultMap idbaseMap typeempid columnid propertyid/result columnname propertyname/result columnage propertyage/association propertydept javaTypeDept columndeptId selectqueryDeptByIdid columndeptid propertydeptid/result columndname propertydname//association
/resultMapselect idqueryDeptByIdselect * from t_dept where deptid #{deptid}
/selectselect idqueryEmp resultMapbaseMap1select * from t_emp
/select