溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用Mybatis實現(xiàn)一對多延遲加載

發(fā)布時間:2020-10-28 02:17:35 來源:億速云 閱讀:287 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)使用Mybatis實現(xiàn)一對多延遲加載,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

需求:一個用戶可以有多個賬戶,查詢用戶時實現(xiàn)賬戶的延遲加載

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 List<Account> accountList;//用戶所擁有的賬戶

  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 List<Account> getAccountList() {
    return accountList;
  }

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

  @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<User> queryAll();

  /**
   * 根據(jù)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<Account> queryAll();

  /**
   * 根據(jù)用戶id查詢賬戶
   */
  List<Account> queryByUid(int uid);
}

5、全局配置文件

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

  <settings>
    <!--開啟延遲加載-->
    <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="aggressiveLazyLoading" value="false"/>
  </settings>

  <!--配置別名-->
  <typeAliases>
    <package name="com.yl.bean"/>
  </typeAliases>

  <!--配置mybatis環(huán)境-->
  <environments default="mysql">
    <!--配置mysql環(huán)境-->
    <environment id="mysql">
      <!--事務(wù)類型-->
      <transactionManager type="JDBC"></transactionManager>
      <!--數(shù)據(jù)源-->
      <dataSource type="POOLED">
        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis&#63;characterEncoding=utf8&amp;serverTimezone=GMT%2B8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
      </dataSource>
    </environment>
  </environments>

  <!--指定映射配置文件位置-->
  <mappers>
    <mapper resource="IUserDao.xml"></mapper>
    <mapper resource="IAccountDao.xml"></mapper>
  </mappers>
</configuration>

6、用戶映射配置文件

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.yl.dao.IUserDao">
  <!--配置用戶實體類和數(shù)據(jù)庫的對應(yīng)信息-->
  <resultMap id="userMap" type="com.yl.bean.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>
    <!--根據(jù)id查詢賬戶-->
    <collection property="accountList" ofType="com.yl.bean.Account" select="com.yl.dao.IAccountDao.queryByUid" column="id"></collection>
  </resultMap>

  <!--查詢所有用戶-->
  <select id="queryAll" resultMap="userMap">
    SELECT * FROM USER
  </select>
  <!--根據(jù)id查詢用戶-->
  <select id="queryById" resultType="user" parameterType="int">
    select * from user where id=#{id}
  </select>
</mapper>

7、賬戶映射配置文件

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.yl.dao.IAccountDao">
  <!--配置Account屬性對應(yīng)數(shù)據(jù)庫信息-->
  <resultMap id="accountUserMap" type="account">
    <id property="id" column="id"></id>
    <result property="uid" column="uid"></result>
    <result property="money" column="money"></result>
    <!--通過id查詢用戶-->
    <association property="user" column="uid" javaType="user" select="com.yl.dao.IUserDao.queryById"></association>
  </resultMap>
  
  <!--查詢所有賬戶-->
  <select id="queryAll" resultMap="accountUserMap">
    SELECT * FROM account
  </select>

  <!--根據(jù)用戶id查詢賬戶-->
  <select id="queryByUid" resultType="account" parameterType="int">
    select * from account where uid=#{uid}
  </select>
</mapper>

關(guān)于使用Mybatis實現(xiàn)一對多延遲加載就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI