当前位置:网站首页>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 .
边栏推荐
- What is the Internet of things
- Spark SQL common time functions
- 打开虚拟机时出现VMware Workstation 未能启动 VMware Authorization Service
- Remember that spark foreachpartition once led to oom
- How to finally generate a file from saveastextfile in spark
- Sublimetext-win10 cursor following problem
- Spark002 --- spark task submission, pass JSON as a parameter
- VMware Workstation fails to start VMware authorization service when opening virtual machine
- pkg_ Resources dynamic loading plug-in
- iframe嵌套其它网站页面 全屏设置
猜你喜欢
随机推荐
Simulate setinterval timer with setTimeout
Local cache --ehcache
Single or multiple human posture estimation using openpose
异步fifo的实现
Tasks, micro tasks, queues and scheduling (animation shows each step of the call)
图片的懒加载
pkg_ Resources dynamic loading plug-in
Implementation of asynchronous FIFO
spark分区算子partitionBy、coalesce、repartition
Bridge NF call ip6tables is an unknown key exception handling
mysql heap表_MySQL内存表heap使用总结-九五小庞
Is it safe to open futures online? Which company has the lowest handling charge?
Pl/sql creates and executes ORALCE stored procedures and returns the result set
Args parameter parsing
Spark AQE
Spark partition operators partitionby, coalesce, repartition
我的创作纪念日
pageHelper不生效,sql没有自动加上limit
Reflection - Notes
Spark002---spark任务提交,传入json作为参数








