延遲加載

延遲加載

  • 延遲加載
    就是在需要用到數據時才進行加載,不需要用到數據時就不加載數據。延遲加載也稱懶加載.
    好處:先從單表查詢,需要時再從關聯表去關聯查詢,大大提高數據庫性能,因爲查詢單表要比關聯查詢多張錶速
    度要快。
  • 壞處
    因爲只有當需要用到數據時,纔會進行數據庫查詢,這樣在大批量數據查詢時,因爲查詢工作也要消耗
    時間,所以可能造成用戶等待時間變長,造成用戶體驗下降。

使用 assocation 實現一對一延遲加載

User

public class User implements Serializable {
    private static final long serialVersionUID = 2L;//UID
    private Integer id;//用戶id
    private String username;//用戶名
    private Date birthday;//用戶出生日期
    private String sex;//用戶性別
    private  String address;//用戶地址

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

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

Account

public class Account implements Serializable {
    private static final long serialVersionUID = 3L;//UID
    private int id;//賬戶id
    private int uid;//用戶id
    private int money;//賬戶金額
    private User user;

    public User getUser() {
        return user;
    }

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

    public int getId() {
        return id;
    }

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

    public int getUid() {
        return uid;
    }

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

    public int getMoney() {
        return money;
    }

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

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

UserDao

public interface UserDao {
    public User findById(int id);
}

AccountDao

public interface AccountDao {   
    public List<Account> findAll();
}

UserDao.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">
<mapper namespace="com.itheima.dao.UserDao">

    <!-- 根據 id 查詢 -->
    <select id="findById" resultType="user" parameterType="int" >
        select * from user where id = #{uid}
    </select>

</mapper>

AccountDao.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">
<mapper namespace="com.itheima.dao.AccountDao">
    <!-- 建立對應關係 -->
    <!-- 定義封裝account和user的resultMap -->
    <resultMap id="accountUserMap" type="account">
        <id property="id" column="id"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
        <!-- 一對一的關係映射:配置封裝user的內容
        select屬性指定的內容:查詢用戶的唯一標識:
        column屬性指定的內容:用戶根據id查詢時,所需要的參數的值
        -->
        <association property="user" column="uid" javaType="user"
                     select="com.itheima.dao.UserDao.findById"></association>
    </resultMap>

    <!-- 查詢所有 -->
    <select id="findAll" resultMap="accountUserMap">
        select * from account
    </select>

</mapper>

SqlMapConfig.xml

在這裏插入圖片描述

<!-- configuration 標籤下 -->
  <settings>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

Test

public class MybatisTest {

    private InputStream in;
    private SqlSessionFactory factory;
    private SqlSession session;
    private AccountDao accountDao;


    /**
     * 初始化獲取代理對象
     * @throws IOException
     */
    @Before
    public void init() throws IOException {
        //1.讀取配置文件
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.創建SqlSessionFactory工廠
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        factory = builder.build(in);
        //3.使用工廠生產SqlSession對象
        session = factory.openSession();
        //4.使用SqlSession創建Dao接口的代理對象
        accountDao = session.getMapper(AccountDao.class);
    }

    /**
     * 釋放資源
     */
    @After
    public void destory() throws IOException {
        //事務回滾
        session.commit();
        session.close();
        in.close();
    }
    /**
     * 入門案例
     * @param
     */
    @Test
    public void findAllTest() {
        List<Account> accountDaoAll = accountDao.findAll();
        for (Account account : accountDaoAll) {
            System.out.println(account);
            //加載用戶信息
            //System.out.println(account.getUser());
        }
    }
}

使用 Collection

User


public class User implements Serializable {
    private static final long serialVersionUID = 2L;//UID
    private Integer id;//用戶id
    private String username;//用戶名
    private Date birthday;//用戶出生日期
    private String sex;//用戶性別
    private  String address;//用戶地址
    private List<Account> accounts;

    public List<Account> getAccounts() {
        return accounts;
    }

    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
    }

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

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

Account


public class Account implements Serializable {
    private static final long serialVersionUID = 3L;//UID
    private int id;//賬戶id
    private int uid;//用戶id
    private int money;//賬戶金額


    public int getId() {
        return id;
    }

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

    public int getUid() {
        return uid;
    }

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

    public int getMoney() {
        return money;
    }

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

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

UserDao


public interface UserDao {
    public List<User> findAll();
    public User findById(int id);
}

AccountDao

public interface AccountDao {
    public List<Account> findAll();
    public Account findById(int id);
}

UserDao.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">
<mapper namespace="com.itheima.dao.UserDao">

    <resultMap id="userMap" type="user">
        <id property="id" column="id"></id>
        <result property="username" column="username"/>
        <result property="birthday" column="birthday"/>
        <result property="sex" column="sex"/>
        <result property="address" column="address"/>

        <collection property="accounts" ofType="account" column="id"
                    select="com.itheima.dao.AccountDao.findById"></collection>

    </resultMap>
    <!-- 根據 id 查詢 -->
    <select id="findAll" resultMap="userMap"  >
        select * from user
    </select>

</mapper>


AccountDao.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">
<mapper namespace="com.itheima.dao.AccountDao">
    <!-- 建立對應關係 -->
    <!-- 定義封裝account和user的resultMap -->

    <!-- 查詢所有 -->
    <select id="findById" resultType="account" parameterType="int">
        select * from account where uid = #{uid}
    </select>

</mapper>

Test

 @Test
    public void findAllTest() {
        List<User> userDaoAll = userDao.findAll();
        for (User user : userDaoAll) {
            System.out.println(user);
            System.out.println(user.getAccounts());
            //加載用戶信息
            //System.out.println(account.getUser());
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章