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 ...
随机推荐
- USACO 6.3 Cryptcowgraphy
CryptcowgraphyBrian Dean The cows of Farmer Brown and Farmer John are planning a coordinated escape ...
- kvm图形化管理工具
1丶windows环境下载安装以及运行xming软件 https://xming.en.softonic.com/ 链接:https://pan.baidu.com/s/1wMb2pK4WfCilS8 ...
- python之markdown转html
python之markdown转html 为了方便,定义一个markdown转html的函数,直接调用即可 import markdown def md2html(mdstr): exts = ['m ...
- redis配置新端口
为redis分配一个8888端口,操作步骤如下:1.$REDIS_HOME/redis.conf重新复制一份,重命名为redis8888.conf.2.打开redis8888.conf配置文件,找到p ...
- centos6 yum 安装nginx 不成功解决办法
转自 http://wlheihei.com/view/64 [root@51ou.com yum.repos.d]# yum install nginxLoaded plugins: fastes ...
- 数据挖掘算法:关联分析二(FP-tree算法)
三.FP-tree算法 下面介绍一种使用了与Apriori完全不同的方法来发现频繁项集的算法FP-tree.FP-tree算法在过程中没有像Apriori一样产生候选集,而是采用了更为紧凑的数据结构组 ...
- 使用 Python 读取火狐的 cookies
这事本来是有个 browsercookie 库可以做的,但是初看它就有不少问题: 不能指定要使用的火狐 profile(后来发现可以指定数据库文件). 找不到 sessionstore.js 文件时会 ...
- python使用web.py开发httpserver,解决post请求跨域问题
使用web.py做http server开发时,遇到postman能够正常请求到数据,但是浏览器无法请求到数据,查原因之后发现是跨域请求的问题. 跨域请求,就是在浏览器窗口中,和某个服务端通过某个 “ ...
- android studio 添加有趣的注释模板 佛祖保佑无bug等
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 汉化包 百度云盘 下载地址:https://pan.baidu.com/s/1pLjwy ...
- bzoj 3837 (随机过题法了解一下)
3837: [Pa2013]Filary Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 395 Solved: 74[Submit][Status] ...