MyBatis 延迟加载(懒加载)—— association 延迟加载、collection 延迟加载

本文介绍了MyBatis的延迟加载(懒加载)机制,重点讲解了association(一对多)和collection(多对一)的延迟加载配置与实现步骤,包括实体类封装、持久层接口定义、配置文件设置以及映射文件编写和测试。通过延迟加载,可以提升数据库查询性能,但可能增加用户等待时间。
摘要由CSDN通过智能技术生成

一、概述

1、延迟加载:

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

2、好处:

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

3、坏处:

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

4、MyBatis 多表查询
在多表查询时,使用了resultMap来实现一对一,一对多,多对多关系的操作。主要是通过 associationcollection 实现一对一及一对多映射。associationcollection 具备延迟加载功能。

二、association 延迟加载、collection 延迟加载

association : 用于一对多(一个用户对应多个账号:User ——> Account)
collection :用于多对一

第一步:封装对数据库映射的实体类 User.java、Account.java
package cn.lemon.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class User implements Serializable {
   
    private Integer id;
    private Date birthday;
    private String username;
    private String sex;
    private String address;

    private List<Account> accountList = new ArrayList<>();

    public Integer getId() {
   
        return id;
    }

    public void setId(Integer id) {
   
        this.id = id;
    }

    public Date getBirthday() {
   
        return birthday;
    }

    public void setBirthday(Date birthday) {
   
        this.birthday = birthday;
    }

    public String getUsername() {
   
        return username;
    }

    public void setUsername(String username) {
   
        this.username = username;
    }

    public String getSex() {
   
        return sex;
    }

    public void setSex(String sex) {
   
        this.sex = sex;
    }

    public String getAddress() {
   
        return address;
    }

    public void setAddress(String address) {
   
        this.address = address;
    }

    public List<Account> getAccountList() {
   
        return accountList;
    }

    public void setAccountList(List<Account> accountList) {
   
        this.accountList = accountList;
    }

    @Override
    public String toString() {
   
        return "User{" +
                "id=" + id +
                ", birthday=" + birthday +
                ", username='" + username + '\'' +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
package cn.lemon.domain;

import java.io.Serializable;

public class Account implements Serializable {
   
    private Integer id;
    private Integer uid;
    private Double money;

    private User user;//关联属性,记录关联关系

    public Integer getId() {
   
        return id;
    }

    public void setId(Integer id) {
   
        this.id 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值