mybatis延迟加载策略

本文介绍了MyBatis的延迟加载策略,包括概念、优点和缺点。详细阐述了一对一和一对多延迟加载的实现方式,如全局开关配置、fetchType属性设置,并给出了示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.1 概述

  • 我们已经知道mybatis中的一对一,一对多,多对多关系的配置及实现,可以实现对象的关联查询。实际开发过程中,我们并不需要加载用户信息时,就加载它的账户信息,而是在使用用户账号的时候,再向数据库查询,此时就是我们所说的延迟加载。

1.2 什么是延迟加载

  • 就是在需要用到数据的时候才进行加载,不需要用到数据的时候就不加载数据。延迟加载也称为懒加载。 

  • 优点:先从单表查询,需要时再从关联表去关联查询,大大提高数据库的性能,因为查询单表要比关联查询多张表的速度快很多。

  • 缺点:因为只有当需要用到数据时,才会进行数据库查询,这样在大批量数据查询时,因为查询工作也需要耗费时间,所以可能造成用户等待时间变长,造成用户体验下降。

1.3 如何实现延迟加载

  •  我们在实现多表操作时,在配置文件中使用了resultMap来实现一对一、一对多、多对多关系的操作。主要是通过association、collection具备延迟加载 功能

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.htjf.dao.IUserDao">
    
    <resultMap id="userMap" type="user">
        <id property="id" column="id"></id>
        <result property="username" column="username"></result>
        <result property="birthday" column="birthday"></result>
        <result property="sex" column="sex"></result>
        <result property="address" column="address"></result>
        <!--配置1对多映射惯性系
           property:在User里面的List<Account>的属性名=
           ofType:当前account表的java类型
       -->
        <collection property="findAll" ofType="role">
            <id property="id" column="roleId"></id>
            <result property="roleName" column="role_name"></result>
            <result property="roleDesc" column="role_desc"></result>
        </collection>
    </resultMap>
    <select id="findAll" resultMap="userMap">
        select user.*,role.id roleId,role.role_name,role.role_desc from user
        left join user_role on user.id = user_role.uid
        left join role on role.id = user_role.rid
    </select>
</mapper>

1.4 一对一实现延迟加载

方式一:配置延迟加载的全局开关,参考配置:

 配置:

测试:

 

 

 

方式二:fetchType 属性

  • 通过再association标签中配置fetchType属性为lazy开启延迟加载
  •  
    <?xml version="1.0" encoding="UTF-8"?>
    
      <!DOCTYPE mapper
    
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
      
    
      <!--namespace 对应dao接口的路径-->
    
      <mapper namespace="com.sunny.dao.IAccountDao">
    
      
    
        <!--一对一: 延迟加载查询(2) 结果集封装、以及延迟加载配置-->
    
        <resultMap id="accountResultMap" type="account">
    
            <id property="accountId" column="accountId"></id>
    
            <result property="uid" column="uid"></result>
    
            <result property="money" column="money"></result>
    
            <!--
    
            association 一对一,延迟加载
    
                一对一 配置:
    
                    property 对用account对象的user属性
    
                    javaType 就是user属性的类型(com.sunny.entity.User
    
                    column   account表的外键字段. 也会作为延迟加载select调用的方法参数自动传入
    
                延迟加载 配置:
    
                    select   延迟加载查询配置。
    
                             根据账户中用户的id,查询用户信息。
    
                             所以需要找到查询方法:com.sunny.dao.IUserDao.findById
    
                    fetchType="lazy" 配置开启延迟加载(覆盖全局的配置)
    
              -->
    
            <association
    
                    property="user" javaType="user" column="uid"
    
                    select="com.sunny.dao.IUserDao.findById" fetchType="lazy"></association>
    
        </resultMap>
    
        <!--一对一: 延迟加载查询(1)-->
    
        <select id="findAll" resultMap="accountResultMap">
            SELECT * FROM account;
        </select>
      </mapper>
    

1.5 一对多实现延迟加载

  • User用户与Account账户,是一对多关系
  • 需求查询用户时候,只查询用户信息; 再使用用户的账户信息时候才向数据库发送查询的语句。
  • 一对多,延迟加载,数据库查询实现

 

  •  IUserDao接口
    查询所有的用户:select * from user

  •   IAccountDao接口
    根据用户id,查询用户所有的账户信息
    SELECT * FROM account WHERE uid=46

  • IAccountDao.xml接口映射
     
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper

        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--namespace 对应dao接口的路径-->

<mapper namespace="com.sunny.dao.IAccountDao">

    <!--一对一: 延迟加载查询(2) 结果集封装、以及延迟加载配置-->

    <resultMap id="accountResultMap" type="account">

        <id property="accountId" column="accountId"></id>

        <result property="uid" column="uid"></result>

        <result property="money" column="money"></result>

        <!--

        association 一对一,延迟加载

            一对一 配置:

                property 对用account对象的user属性

                javaType 就是user属性的类型(com.sunny.entity.User)

                column   account表的外键字段. 也会作为延迟加载select调用的方法参数自动传入

            延迟加载 配置:

                select   延迟加载查询配置。

                         根据账户中用户的id,查询用户信息。

                         所以需要找到查询方法:com.sunny.dao.IUserDao.findById

                fetchType="lazy" 配置开启延迟加载(覆盖全局的配置)

        -->

        <association

                property="user" javaType="user" column="uid"

                select="com.sunny.dao.IUserDao.findById" fetchType="lazy"></association>

    </resultMap>

    

    <!--一对一: 延迟加载查询(1)-->

    <select id="findAll" resultMap="accountResultMap">

        SELECT * FROM account;

    </select>

    <!--根据用户查询账户-->
    <select id="findAccountsByUserId" parameterType="int" resultType="account">
        SELECT * FROM account WHERE uid=#{userId}
    </select>
</mapper>
  • IUserDao.xml

     
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper

        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace 对应dao接口的路径-->

<mapper namespace="com.sunny.dao.IUserDao">

    <!--parameterType 方法参数类型;resultType 表示返回值类型 -->

    <select id="findById" parameterType="int" resultType="user">

        SELECT * FROM USER WHERE id=#{id}

    </select>



    <!--查询用户,使用延迟加载(2 延迟加载配置-->

    <resultMap id="userResultMap" type="user">

        <!--a. 封装用户信息-->

        <id property="id" column="id"></id>

        <result property="username" column="username"></result>

        <result property="birthday" column="birthday"></result>

        <result property="sex" column="sex"></result>

        <result property="address" column="address"></result>

        <!--

        b. 一对多,延迟加载配置

        collection  一对多

            property 用户对象的accounts集合属性

            ofType   集合中元素的类型(com.sunny.entity.Account)

            column   用户的主键,会作为select对应的方法的参数id

            select   延迟加载配置。 根据用户查询账户,查询结果封装到List<Account> accounts;

            fetchType="lazy"  开启延迟加载

        -->

        <collection

                property="accounts" ofType="account" column="id"

                select="com.sunny.dao.IAccountDao.findAccountsByUserId"

                fetchType="lazy"></collection>

    </resultMap>



    <!--查询用户,使用延迟加载(1-->

    <select id="findAll" resultMap="userResultMap">

        SELECT * FROM USER

    </select>

</mapper>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hyhcloud

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值