mybatis学习之懒加载(延迟加载)

本文介绍了懒加载(延迟加载)的概念及其在数据库查询中的好处,如提高查询效率、减少内存消耗和性能提升。详细讲解了如何在MyBatis中实现一对一和多对一关系的懒加载,以及如何在接口、mapper文件和全局配置中启用和管理懒加载。
摘要由CSDN通过智能技术生成

懒加载(延迟加载)

我们为什么要设置懒加载?(好处)

  1. 减少不必要的数据库查询:在某些情况下,我们可能只需要使用对象的一部分数据,懒加载可以避免在获取对象时加载不需要的数据,提高了查询效率。

  2. 减少内存消耗:使用懒加载可以避免一次性加载大量数据,减少内存的消耗,尤其是在处理大量数据的情况下非常有用。

  3. 提高性能:懒加载可以根据需要动态地加载数据,不必一次性加载所有数据,可以减少对数据库的压力,提高系统的性能。

懒加载的使用场景

适合于进行关联查询时,比如一对一或多对一等的场景,在resultMap的association或collection中使用

如何使用懒加载?

  1. 先准备两张表,及其实体类

User


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


    //用户与账户的关系是一对多 一个用户可能拥有多个账户
    private List<Account> accountList;

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

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

    public Integer getId() {
        return id;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

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

    public Date getBirthday() {
        return birthday;
    }

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

    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;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", birthday=" + birthday +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                ", accountList=" + accountList +
                '}';
    }
}

Account


public class Account {
    private Integer id;
    private Integer uid;
    private Double money;
    //一对一
   // private User user;

    @Override
    public String toString() {
        return "Account{" +
                "aid=" + id +
                ", uid=" + uid +
                ", money=" + money +
                '}';
    }/*

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }*/

    public Integer getId() {
        return id;
    }

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

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }
}

  1. 在接口及mapper映射文件中写
    UserMapper
public interface UserMapper {

    /**
     * 查询所有信息包含用户所有的账户信息
     * @return
     */
    User findAll(Integer id);
}
<resultMap id="findAllResultMap" type="user">
    <id property="id" column="id"></id>
    <result property="username" column="username"></result>
    <result property="password" column="password"></result>
    <result property="birthday" column="birthday"></result>
    <result property="sex" column="sex"></result>
    <result property="address" column="address"></result>
  <!--  开启懒加载
      property:属性名
      ofType:集合的泛型
      select:懒加载时执行的查询语句
      column:查询语句所需要的参数
      fetchType:lazy局部开启懒加载
      -->
    <collection property="accountList" ofType="account"
    select="com.xxx.mapper.AccountMapper.getAccountByUid" column="id"
    fetchType="lazy">
    </collection>
</resultMap>
<select id="findAll" parameterType="int" resultMap="findAllResultMap" >
        select * from user where id=#{id}
</select>

Account Mapper

<select id="getAccountByUid" parameterType="int" resultType="account">
        select * from account where uid=#{uid}
</select>

测试

 @Test
    public void testFindAll(){
        UserMapper  userMapper= ss.getMapper(UserMapper.class);
        User user= userMapper.findAll(41);
        System.out.println(user.getUserName());
    }

注意:使用equals,clone,hashCode,toString方法时,无论是否开启懒加载都会执行你需要懒加载的内容,除非在mybatis-config.xml的配置中写“/<setting name=“lazyLoadTriggerMethods” value=“false”/">

全局配置懒加载

在mybatis-config.xml中写

<settings>
    	<!--  开启懒加载      -->
        <setting name="lazyLoadingEnabled" value="true"/>
</settings>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值