懒加载(Lazy Loading) - MyBatis懒加载 - Spring懒加载

懒加载(Lazy Loading)

  懒加载也叫“延迟价值”,核心思想是把对象的实例化延迟到真正调用该对象的时候,这样做的好处是可以减轻大量对象在实例化时对资源的消耗,而不是在程序初始化的时候就预先将对象实例化。另外“懒加载”可以将对象的实例化代码从初始化方法中独立出来,从而提高代码的可读性,以便于代码能够更好地组织。
  特别是在web应用程序中,它能够在用户滚动页面的时候自动获取更多的数据,而新得到的数据不会影响原有数据的显示,同时最大程度上减少服务器端的资源耗用。(购买商品往下滑动才加载,一次只加载一部分,如果继续需要,再加载)【懒加载提高了系统响应时间,提升了系统性能】 - - [适用于单表查询提高效率,但是多表关联查询效率可能降低]

MyBatis中懒加载的使用

  1. MyBatis中使用懒加载需要核心配置文件中的configuration下的settings中配置以下两行
<configuration>

    <!--settings配置全局变量,这个有顺序需要放在<environment>的前面才能起作用
        lazyLoadingEnabled 配置懒加载,这里配置的是全局允许或静止懒加载,配置之后所有的任务都可以懒加载
        具体使用懒加载就是通过fetchType=lazy实现懒加载
        aggressiveLazyLoading配置为false,实现按需加载
    -->
    <settings>
        <!-- 打开懒加载的开关 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 将积极加载改为消极加载(及按需加载) -->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
</configuration>    
  1. 在使用的时候(在映射文件XxxMapper.xml中使用),通过在所需要懒加载的resultMap 的列上加上 fetchType="lazy" ,表明这个数据是懒加载实现的。
	<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>

Spring中懒加载的使用

  在Spring中,默认情况下在容器被初始化的过程中,就会去解析xml和注解,将其创建为单例的bean并存到一个map集合中。如果需要创建的bean很多,spring在启动的过程中就需要花费大量的时间去解析xml和注解来创建bean ,并花费大量的空间去存储bean,以供使用,但是在很多情况下,大部分的bean可能很久都使用不上, 所以Spring提供了懒加载机制。Spring的懒加载机制让bean不在启动容器的时候就创建,而是在第一次使用时才创建,减轻在启动容器过程中对时间的浪费和内存的消耗。
  懒加载机制只对单例bean起作用,多例bean是在使用的时候才会由容器创建,所以对于多例bean设置懒加载是没有意义的。

spring的懒加载配置方式有两种:

  • 注解配置懒加载(@Lazy)
  • xml中配置懒加载
  1. 注解配置懒加载(@Lazy)
      就是在类/成员变量/方法上加@Lazy注解,表示这个类/成员变量/方法是懒加载的,在容器启动的时候不会初始化为bean,只有在使用到的时候,才会创建。

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

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

  1. xml配置懒加载
      xml文件里面,通过配置 lazy-init="true"来启用懒加载。
    2.1 配置全局懒加载(直接在核心配置文件的beans头里面加入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 配置局部懒加载(在bean里面加入lazy-init="true")

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

如果同时设定全局懒加载和局部懒加载(bean的懒加载机制),且配置不相同的时候,则bean局部配置会覆盖全局配置。

猜你喜欢

转载自blog.csdn.net/qq_40542534/article/details/108822915