Lazy Loading-MyBatis Lazy Loading-Spring Lazy Loading

Lazy Loading

  Lazy loading is also called "delayed value". The core idea is to delay the instantiation of an object until the object is actually called. The advantage of this is that it can reduce the consumption of resources when a large number of objects are instantiated, instead of being initialized in the program. The object is instantiated beforehand. In addition, "lazy loading" can separate the instantiation code of the object from the initialization method, thereby improving the readability of the code, so that the code can be better organized.
  Especially in web applications , it can automatically obtain more data when the user scrolls the page, and the newly obtained data will not affect the display of the original data, and at the same time minimize the resource consumption on the server side. (Swipe down to load the purchased product, load only a part at a time, if you continue to need it, load it again) [Lazy loading improves system response time and improves system performance]--[Suitable for single-table query to improve efficiency, but multi-table association Query efficiency may be reduced]

The use of lazy loading in MyBatis

  1. Using lazy loading in MyBatis requires the following two lines to be configured in the settings under the configuration in the core configuration file
<configuration>

    <!--settings配置全局变量,这个有顺序需要放在<environment>的前面才能起作用
        lazyLoadingEnabled 配置懒加载,这里配置的是全局允许或静止懒加载,配置之后所有的任务都可以懒加载
        具体使用懒加载就是通过fetchType=lazy实现懒加载
        aggressiveLazyLoading配置为false,实现按需加载
    -->
    <settings>
        <!-- 打开懒加载的开关 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 将积极加载改为消极加载(及按需加载) -->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
</configuration>    
  1. When using it (used in the mapping file XxxMapper.xml), by adding it to the column of the resultMap that needs to be lazily loaded fetchType="lazy", it indicates that this data is realized by lazy loading.
	<resultMap id="personMap" type="com.xgf.correlation.one_to_one.bean.Person" autoMapping="true">
       <id property="id" column="id"/>
       <result property="username" column="username"/>
		<!-- fetchType="lazy" 懒加载实现数据加载-->
       <association property="card" javaType="com.xgf.correlation.one_to_one.bean.Card" fetchType="lazy">
           <id property="id" column="id"/>
           <result property="code" column="code"/>
       </association>
    </resultMap>


	<resultMap id="orderMap" type="com.xgf.mybatis.correlation.many_to_many.bean.Order">
        <id column="oid" property="id"/>
        <result property="description" column="description"/>
        <!-- fetchType="lazy" 懒加载实现数据加载-->
        <collection property="productList"  fetchType="lazy" ofType="com.xgf.mybatis.correlation.many_to_many.bean.Product">
            <id column="pid" property="id"/>
            <result property="name" column="name"/>
        </collection>
    </resultMap>

The use of lazy loading in Spring

  In Spring, by default, when the container is initialized, xml and annotations are parsed, and they are created as singleton beans and stored in a map collection. If there are a lot of beans that need to be created, spring needs to spend a lot of time parsing xml and annotations to create beans during the startup process, and spends a lot of space to store beans for use, but in many cases, most The bean may not be used for a long time, so Spring provides a lazy loading mechanism. Spring's lazy loading mechanism allows the bean to be created not when the container is started, but only when it is used for the first time, which reduces the waste of time and memory consumption in the process of starting the container.
  The lazy loading mechanism only works for singleton beans, and multiple beans are created by the container when they are used, so it is meaningless to set lazy loading for multiple beans.

There are two ways to configure spring lazy loading:

  • Annotation configuration lazy loading ( @Lazy)
  • Configure lazy loading in xml
  1. Annotation configuration lazy loading ( @Lazy)
      is to add @Lazyannotations to the class/member variable/method, indicating that this class/member variable/method is lazily loaded and will not be initialized as a bean when the container is started, only when it is used Will be created.

//懒加载  --  类上
@Lazy
public class User {
    
    
    private Integer id;
    private String username;
    private String password;

	//懒加载 -- 方法上
    //@Lazy
    public User(){
    
    
        System.out.println("初始化User的bean");
    }
}

  1. xml configuration lazy loading
      xml file inside, by configuring lazy-init="true"to enable lazy loading.
    2.1 Configure global lazy loading (add directly to the beans header of the core configuration file default-lazy-init="true")
<!-- 全局懒加载,在applicationContext.xml核心配置文件里面的beans里加default-lazy-init="true"-->
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

</beans>

2.2 Configure partial lazy loading (add it to the bean lazy-init="true")

<bean id="User" class="com.xgf.bean.User" lazy-init="true"></bean>

If both global lazy loading and local lazy loading (bean lazy loading mechanism) are set at the same time, and the configuration is not the same, the bean local configuration will override the global configuration.

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/108822915