当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Pl/sql creates and executes ORALCE stored procedures and returns the result set

SPI传输出现数据与时钟不匹配延后问题分析与解决

Single or multiple human posture estimation using openpose

防抖(debounce)和节流(throttle)

密码强度验证示例

System. Accessviolationexception: an attempt was made to read or write to protected memory. This usually indicates that other memory is corrupted

Simulate setinterval timer with setTimeout

打开虚拟机时出现VMware Workstation 未能启动 VMware Authorization Service

Understanding the execution order of T-SQL query from the execution order of join on and where

延迟加载源码剖析:
随机推荐
node学习
苹果内购和Apple Pay 的区别
记一次Spark foreachPartition导致OOM
Recommend 10 learning websites that can be called artifact
密码强度验证示例
《三子棋》C语言数组应用 --n皇后问题雏形
深入:微任务与宏任务
树莓派入门:树莓派的初始设置
记一次redis超时
SPI传输出现数据与时钟不匹配延后问题分析与解决
Gbdt source code analysis of boosting
TypeScript学习2——接口
MeanShift聚类-01原理分析
dpdk 收发包问题案例:使用不匹配的收发包函数触发的不收包问题定位
What is the Internet of things
推荐10个堪称神器的学习网站
MySQL heap table_ MySQL memory table heap Usage Summary - Ninth Five Year Plan small pang
Instance Tunnel 使用
C language function review (pass value and address [binary search], recursion [factorial, Hanoi Tower, etc.))
Solve the timeout of dbeaver SQL client connection Phoenix query