MyBatis Lazy Loading
MyBatis的Lazy Loading可以实现延迟查询Bean里的嵌套成员类,控制lazy loading的<settings>属性有
lazyLoadingEnabled: lazy loading开关,默认为true
aggressiveLazyLoading: 侵略性 lazy loading 开关, 默认为true, 这个属性比较搞笑,如果为true则当你访问任何一个属性都会加载所有的其他lazy load属性,即使你根本没有调用哪个lazy load属性,说白了就是aggressiveLazyLoading=true,则lazy load等于没用,所以要使用lazy load还是将其设为false
一个使用lazyload的例子
ResultMap
<!-- 使用子查询的方式查询成员变量 -->
<resultMap id="articleResultMap2" type="Article">
<id property="id" column="article_id" />
<result property="title" column="article_title" />
<result property="content" column="article_content" />
<association property="user" column="user_id" select="dao.userdao.selectUserByID"/>
</resultMap>
查询Mapper
<!-- 测试lazy load user -->
<select id="selectArticleById2" parameterType="int" resultMap="dao.base.articleResultMap2">
SELECT * FROM article WHERE id = #{id}
</select> <select id="selectUserByID" parameterType="int" resultType="User"
flushCache="false" useCache="true" timeout="10000" statementType="PREPARED">
SELECT
*
FROM user
WHERE id = #{id}
</select>
测试代码
public String getArticle2(Model model) {
Article article = articleDao.selectArticleById2(1);
// 如果你在这里打一个断点,你会发现还没有执行到getUser()这一句article.user已经被查询加载
// 而如果你将 getUser() 那行注释,则article.user在执行到这里也不会被加载
// 我的理解是java是编译型语言,mybatis可以根据编译好的中间码查看哪些属性被调用
// 然后在第一次执行sql的时候把后面将会调用到的延迟加载属性都提前加载了
// 另外,MyBatis有个更搞笑和骗人的地方是,如果你不在这里打断点,它lazy load的子查询就一定会出现在getUser()之后
// 而如果这里打了断点,则lazy load的子查询语句会在selectArticleById2()这个方法就出现了
System.out.println();
// 如果aggressiveLazyLoading为true,则getContent()的时候就会执行查询user的sql
// 即使你根本没有调用getUser(),也会将user属性查询出来,例如将getUser()那行注释了
System.out.println(article.getContent());
System.out.println("Lazy loading ......");
System.out.println(article.getUser());
return "index";
}
输出
DEBUG - ooo Using Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, UserName=root@localhost, MySQL-AB JDBC Driver]
DEBUG - ==> Preparing: SELECT * FROM article WHERE id = ?
DEBUG - ==> Parameters: 1(Integer)
DEBUG - Cache Hit Ratio [dao.userdao]: 0.0
DEBUG - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6bbe7]
DEBUG - Returning JDBC Connection to DataSourcetest_content
Lazy loading ......
DEBUG - Fetching JDBC Connection from DataSource
DEBUG - JDBC Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, UserName=root@localhost, MySQL-AB JDBC Driver] will not be managed by Spring
DEBUG - ooo Using Connection [jdbc:mysql://127.0.0.1:3306/mybatistest?characterEncoding=utf8, UserName=root@localhost, MySQL-AB JDBC Driver]
DEBUG - ==> Preparing: SELECT * FROM user WHERE id = ?
DEBUG - ==> Parameters: 1(Integer)
DEBUG - Returning JDBC Connection to DataSource
1|test1|19|beijing
抛开上面注释中MyBatis各种奇怪的表现不说,MyBatis的Lazy Loading是基于子查询select的,也是这段
<association property="user" column="user_id" select="dao.userdao.selectUserByID"/>
这个方式最大的问题是会产生N+1问题,假设article里的user是一个List<User>:
1. 使用一条SQL语句查询Article类(the 1)
2. 使用N条SQL查询Article里的List<User>
如果我们在查询Article以后需要遍历Article的List<User>,则会触发所有user的Lazy Loading
也就是说我们也无法使用JOIN去使用Lazy Loading,从而避免n+1的问题
MyBatis Lazy Loading的更多相关文章
- Angular2+typescript+webpack2(支持aot, tree shaking, lazy loading)
概述 Angular2官方推荐的应该是使用systemjs加载, 但是当我使用到它的tree shaking的时候,发现如果使用systemjs+rollup,只能打包成一个文件,然后lazy loa ...
- Lazyr.js – 延迟加载图片(Lazy Loading)
Lazyr.js 是一个小的.快速的.现代的.相互间无依赖的图片延迟加载库.通过延迟加载图片,让图片出现在(或接近))视窗才加载来提高页面打开速度.这个库通过保持最少选项并最大化速度. 在线演示 ...
- Can you explain Lazy Loading?
Introduction Lazy loading is a concept where we delay the loading of the object until the point wher ...
- [AngularJS] Lazy Loading modules with ui-router and ocLazyLoad
We've looked at lazy loading with ocLazyLoad previously, but what if we are using ui-router and want ...
- [AngularJS] Lazy loading Angular modules with ocLazyLoad
With the ocLazyLoad you can load AngularJS modules on demand. This is very handy for runtime loading ...
- iOS swift lazy loading
Why bother lazy loading and purging pages, you ask? Well, in this example, it won't matter too much ...
- Entity Framework加载相关实体——延迟加载Lazy Loading、贪婪加载Eager Loading、显示加载Explicit Loading
Entity Framework提供了三种加载相关实体的方法:Lazy Loading,Eager Loading和Explicit Loading.首先我们先来看一下MSDN对三种加载实体方法的定义 ...
- Lazy Loading | Explicit Loading | Eager Loading in EntityFramework and EntityFramework.Core
EntityFramework Eagerly Loading Eager loading is the process whereby a query for one type of entity ...
- EFCore Lazy Loading + Inheritance = 干净的数据表 (二) 【献给处女座的DB First程序猿】
前言 本篇是上一篇EFCore Lazy Loading + Inheritance = 干净的数据表 (一) [献给处女座的DB First程序猿] 前菜 的续篇.这一篇才是真的为处女座的DB Fi ...
随机推荐
- oracle配置ODBC
摘自:http://www.cnblogs.com/shelvenn/p/3799849.html 我使用的Windows 10,64位的操作系统. 1.下载驱动包 base包:instantclie ...
- 分享实用的JavaScript代码库
1 var keyCodeMap = { 2 8: 'Backspace', 3 9: 'Tab', 4 13: 'Enter', 5 16: 'Shift', 6 17: 'Ctrl', 7 18: ...
- linux下解除端口占用
1.找出占用端口进程的pid sudo lsof -i:port 2.终止进程 pid
- poj2387- Til the Cows Come Home(最短路)
此为转载:http://blog.csdn.net/wangjian8006: 题目大意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 解题思路: 模版题,这题要 ...
- strings.xml显示html格式
需求:合同协议,其中指定内容为红色 效果图: 实现如下: <string name="learn_ticket_agreement" formatted="fals ...
- JDK源码分析(三)——HashMap 上(基于JDK7)
目录 HashMap概述 内部字段及构造方法 存储元素 扩容 取出元素 删除元素 判断 总结 HashMap概述 前面我们分析了基于数组实现的ArrayList和基于双向链表实现的LinkedLi ...
- odoo打包下载
view 视图中下载按钮的编辑 <record id="action_download_zip" model="ir.actions.server"> ...
- C#管理windows服务
.NET Framework中提供了现成的类库可以很方便的实现对windows服务的安装.卸载.启动.停止.获取运行状态等功能.这些类都在System.ServiceProcess命名空间下. 安装w ...
- 冒泡排序(初级版)之C++实现
冒泡排序(初级版)之C++实现 一.源代码:BubbleSortLow.cpp /*冒泡排序思想: 从第一个元素开始,对数组中两两相邻的元素比较,将值较小的元素放在前面,值较大的元素放在后面: 一轮比 ...
- Kolibri v2.0-Buffer Overflow成功复现
Kolibri v2.0-Buffer Overflow成功复现及分析 文件下载地址:http://pan.baidu.com/s/1eS9r9lS 正文 本次讲解用JMP ESP的方法溢出 关于网上 ...