Mybatis一对一延迟加载实现过程解析

需求:用户和账户一对一关系,查询账户时实现用户的延迟加载

思路:根据id查询,需要延迟加载的一方

1、用户实体类

package com.yl.bean;

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

/**
 * 用户实体类
 */
public class User implements Serializable {
  private Integer id;//id
  private String username;//用户名
  private Date birthday;//生日
  private String sex;//性别
  private String address;//地址
  private Account account;//用户所拥有的账户

  public User() {
  }

  public Integer getId() {
    return id;
  }

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

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

  public Account getAccount() {
    return account;
  }

  public void setAccount(Account account) {
    this.account = account;
  }

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

2、账户实体类

package com.yl.bean;

import java.io.Serializable;

/**
 * 账户实体类
 */
public class Account implements Serializable {
  private Integer id;//账户id
  private Integer uid;//用户id
  private Double money;//余额
  private User user;//账户所属用户

  public Account() {
  }

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

  public User getUser() {
    return user;
  }

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

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

3、用户持久层接口

package com.yl.dao;

import com.yl.bean.User;

import java.util.List;

/**
 * 用户持久层接口
 */
public interface IUserDao {

  /**
   * 查询所有用户
   */
  List queryAll();

  /**
   * 根据id查询用户
   */
  User queryById(int id);

}

4、账户持久层接口

package com.yl.dao;

import com.yl.bean.Account;
import com.yl.bean.User;

import java.util.List;

/**
 * 账户持久层接口
 */
public interface IAccountDao {

  /**
   * 查询所有账户
   */
  List queryAll();
}

5、全局配置文件





  
    
    
    
  

  
  
    
  

  
  
    
    
      
      
      
      
        
        
        
        
      
    
  

  
  
    
    
  

6、用户映射配置文件





  
  
    
    
    
    
    
    
    
      
      
      
    
  

  
  
  
  

7、账户映射配置文件





  
  
    
    
    
    
    
  
  
  
  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Mybatis一对一延迟加载实现过程解析)