当前位置:网站首页>Delayed loading source code analysis:
Delayed loading source code analysis:
2022-07-25 15:23:00 【The prettiest programmer】
Delay loading
Is in need of ⽤ Only when the data is available ⾏ load , Unwanted ⽤ When the data is received, the data is not loaded . Lazy loading is also called lazy loading .advantage :
First query from a single table , When necessary, the query is associated from the associated table ,⼤⼤ carry ⾼ Database performance , Because the query table needs to ⽐ It is faster to query multiple tables by association .shortcoming :
Because only when needed ⽤ When it comes to data , It's going to be ⾏ Database query , In this way ⼤ When querying batch data , Because of the inquiry ⼯ It also takes time to do , So
Can cause ⽤ The waiting time of the customer changes ⻓, cause ⽤ Decline in user experience .In multiple tables :
⼀ For more than , Many to many : Usually ⽤ Delay loading
⼀ Yes ⼀( How to ⼀): Usually ⽤⽴ The loadingBe careful :
Deferred loading is based on nested queriesRealize local delayed loading
stay association and collection There are labels in ⼀ individual fetchType attribute , By modifying its value , You can modify the local loading strategy . fetchType=“lazy”
<!-- Turn on ⼀ For more than Delay loading -->
<resultMap id="userMap" type="user">
<id column="id" property="id"></id>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<!--
fetchType="lazy" Lazy loading strategy
fetchType="eager" ⽴ Load policy
-->
<collection property="orderList" ofType="order" column="id"
select="com.lagou.dao.OrderMapper.findByUid" fetchType="lazy">
</collection>
</resultMap>
<select id="findAll" resultMap="userMap">
SELECT * FROM `user`
</select>
- Global delay loading
stay Mybatis The core of ⼼ To configure ⽂ Can make ⽤setting Tag modify global loading policy .
<settings>
<!-- Turn on the global delay loading function -->
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
- The principle of delayed loading is realized
Its principle is , send ⽤ CGLIB or Javassist( Default ) establish ⽬ The proxy object of the target object . Tune up ⽤ The delay loading property of the proxy object
getting ⽅ Legal time , Into the ⼊ Interceptor ⽅ Law .⽐ If tune ⽤ a.getB().getName() ⽅ Law , Into the ⼊ The interceptor invoke(…) ⽅ Law ,
Find out a.getB() When delayed loading is required , Then the saved query Association will be sent separately B Object's SQL , hold B Look up , then
transfer ⽤a.setB(b) ⽅ Law , therefore a object b The property has a value , Then finish a.getB().getName() ⽅ The tone of law ⽤. This is the delay
The basic principle of loading
summary : Delayed loading is mainly realized in the form of dynamic agent , Intercept the specified through the proxy ⽅ Law , Of board ⾏ Data loading .
Delay loading principle ( Source analysis )
MyBatis Delayed loading mainly makes ⽤:Javassist,Cglib Realization , Class diagram display :
Setting Configuration is loaded :
Delay load proxy object creation
Mybatis The query result of is by ResultSetHandler Pick up ⼝ Of handleResultSets()⽅ Can't handle .ResultSetHandler Pick up ⼝ Only ⼀
Implementation ,DefaultResultSetHandler, Next, let's look at the delay loading related ⼀ A nuclear ⼼ Of ⽅ Law
By default ⽤javassistProxy Into the ⾏ Proxy object creation
//aggressive ⼀ The secondary loading property requires delayed loading properties or contains trigger delayed loading ⽅ Law
if
adopt invoke Method , Judge whether the attribute data contained in the called method is configured to enable lazy loaded fields , If it is through the proxy object again load Query the database , Otherwise go straight back to .
边栏推荐
猜你喜欢
随机推荐
如何解决Visual Stuido2019 30天体验期过后的登陆问题
How much memory can a program use at most?
spark分区算子partitionBy、coalesce、repartition
Understanding the execution order of T-SQL query from the execution order of join on and where
MFC 线程AfxBeginThread基本用法,传多个参数
Process control (Part 1)
浏览器工作流程(简化)
Image cropper example
C语言函数复习(传值传址【二分查找】,递归【阶乘,汉诺塔等】)
JVM-动态字节码技术详解
MeanShift聚类-01原理分析
MySQL installation and configuration super detailed tutorial and simple database and table building method
vscode 插件篇收集
剑指Offer | 二进制中1的个数
Idea远程提交spark任务到yarn集群
任务、微任务、队列和调度(动画展示每一步调用)
mysql heap表_MySQL内存表heap使用总结-九五小庞
Pl/sql creates and executes ORALCE stored procedures and returns the result set
Solve the error caused by too large file when uploading file by asp.net
Remember that spark foreachpartition once led to oom







