欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis延遲加載與立即加載案例教程

 更新時間:2021年07月28日 09:42:58   作者:小小張自由—>張有博  
這篇文章主要介紹了MyBatis延遲加載與立即加載案例教程,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

MyBatis入門-延遲加載與立即加載

加載策略

延遲加載

延遲加載(lazy load)是(也稱為懶加載)Hibernate3關(guān)聯(lián)關(guān)系對象默認(rèn)的加載方式,延遲加載機(jī)制是為了避免一些無謂的性能開銷而提出來的,所謂延遲加載就是當(dāng)在真正需要數(shù)據(jù)的時候,才真正執(zhí)行數(shù)據(jù)加載操作。延遲加載,可以簡單理解為,只有在使用的時候,才會發(fā)出sql語句進(jìn)行查詢。

需要在主配置文件開啟加載策略,子配置文件使用collection屬性

立即加載

所謂立即加載就是查詢時,所有的相關(guān)數(shù)據(jù)一次被讀取出來,而不是分N次。

一對一實(shí)現(xiàn)理解加載——查詢賬戶可查出用戶信息(在查詢賬戶是綁定查詢用戶信息的方法)

一對多實(shí)現(xiàn)延遲加載——查詢用戶可查出所以賬戶信息(在查詢用戶是綁定查詢賬戶信息的方法)

基礎(chǔ)數(shù)據(jù)

實(shí)體類

public class User implements Serializable {
 
    /**
     * Java實(shí)體類為什么要實(shí)現(xiàn)Serializable接口
     *  1.用于序列化與反序列化--一個類只有實(shí)現(xiàn)了Serializable接口,它的對象才能被序列化。
     *  2.Serializable接口就是Java提供用來進(jìn)行高效率的異地共享實(shí)例對象的機(jī)制,實(shí)現(xiàn)這個接口即可。
     */
 
 
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
    //一對多關(guān)系映射:主表實(shí)體應(yīng)該包含從表實(shí)體的集合引用
    private List<Account> accounts;
 
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
 
    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;
    }
 
}
 
public class Account implements Serializable {
    private Integer id;
    private Integer uid;
    private Double money;
 
    //一對一的關(guān)系中
    //從表實(shí)體應(yīng)該包含一個主表實(shí)體的對象引用
    private User user;
 
    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;
    }
 
    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", uid=" + uid +
                ", money=" + money +
                '}';
    }
}

dao層的兩個接口

/**
 * @Author: Promsing
 * @Date: 2021/4/4 - 16:21
 * @Description: 描述 形容
 * @version: 1.0
 */
public interface IUserDao {
    /**
     * 查詢所有
     * @return
     */
    List<User> findAll();
 
    /**
     * 根據(jù)id查詢
     * @param i
     * @return
     */
    User findById(Integer i);
}
 
public interface IAccountDao {
    /**
     * 查詢所有賬戶,同時還有獲取當(dāng)前賬戶所屬的用戶信息
     * @return
     */
    List<Account> findAll();
 
    /**
     * 根據(jù)用戶id查詢
     * @param i
     * @return
     */
    Account findById(Integer i);
 
}

resource下的主映射文件與子映射文件

主:SqlMapConfig_anno.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--myBatis的主配置文件 -->
<configuration>
    <settings>
        <!--開啟mybatis支持延遲加載-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
    <!--配置環(huán)境-->
    <environments default="mysql">
        <!--配置mysql環(huán)境-->
        <environment id="mysql">
            <!--配置事務(wù)的類型-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置數(shù)據(jù)源(連接池)-->
            <dataSource type="POOLED">
                <!--配置連接數(shù)據(jù)庫的基本信息-->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/eesy"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <!--映射文件 配置文件方式-->
    <mappers>
        <mapper resource="com/dynamic_annotation/dao/IAccountDao.xml"></mapper>
        <mapper resource="com/dynamic_annotation/dao/IUserDao.xml"></mapper>
    </mappers>
 
    
 
    <!--映射文件 注解方式(使用注解就要刪除源配置文件)-->
   <!-- <mappers>
        <mapper class="com.dynamic_basics.dao.IUserDao"></mapper>
    </mappers>-->
</configuration>

子:IAccountDao.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.dynamic_annotation.dao.IAccountDao">
 
    <!--定義封裝account和user的resultMap-->
    <resultMap id="accountUserMap" type="com.dynamic_annotation.domain.Account">
        <id property="id" column="id"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
       
 
        <!--立即加載-->
        <!--使用select屬性,使用其他DAO層的方法-->
     <association property="user" column="uid" javaType="com.dynamic_annotation.domain.User" select="com.dynamic_annotation.dao.IUserDao.findById">
 
     </association>
       </resultMap>
 
       <!--查詢所有 id使用方法名-->
    <select id="findAll" resultMap="accountUserMap">
        select * from Account
    </select>
 
    <!--單個查詢-->
    <select id="findById" parameterType="int" resultType="com.dynamic_annotation.domain.Account">
        select * from Account where uid=#{id}
    </select>
 
 
</mapper>

子:IUserDao.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.dynamic_annotation.dao.IUserDao">
 
    <!--定義封裝account和user的resultMap-->
    <resultMap id="userAccountMap" type="com.dynamic_annotation.domain.User">
        <id property="id" column="id"></id>
        <result property="username" column="username"></result>
        <result property="address" column="address"></result>
        <result property="sex" column="sex"></result>
        <result property="birthday" column="birthday"></result>
      
       <!-- collection 是用于建立一對多中集合屬性的對應(yīng)關(guān)系
            ofType 用于指定集合元素的數(shù)據(jù)類型
            select 是用于指定查詢賬戶的唯一標(biāo)識(賬戶的 dao 全限定類名加上方法名稱)
            column 是用于指定使用哪個字段的值作為條件查詢
        -->
        <!--延遲加載-->
        <collection property="accounts" ofType="com.dynamic_annotation.domain.Account" select="com.dynamic_annotation.dao.IAccountDao.findById" column="id"></collection>
    </resultMap>
 
    <!--查詢所有 id使用方法名-->
    <select id="findAll" resultMap="userAccountMap">
        select * from User
    </select>
 
    <!--根據(jù)id查詢-->
    <select id="findById" parameterType="int" resultType="com.dynamic_annotation.domain.User">
        select * from user where id=#{id}
    </select>
 
 
</mapper>

測試類

public class AnnotationTest {
 
    private InputStream in;
    private SqlSession sqlSession;
    private IAccountDao accountDao;
    private IUserDao userDao;
 
    @Before
    public void init()throws Exception{
        //1.讀取配置文件  Resources是myBatis封裝的類
        in= Resources.getResourceAsStream("SqlMapConfig_anno.xml");
        //2.創(chuàng)建SQLSessionFactory工廠
        //  SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory factory=builder.build(in);
        //3.使用工廠生產(chǎn)SQLSession對象
        sqlSession = factory.openSession();
        //4.使用SQLSession創(chuàng)建DAO接口的代理對象
        accountDao = sqlSession.getMapper(IAccountDao.class);
        userDao=sqlSession.getMapper(IUserDao.class);
    }
 
    @After
    public void destroy()throws Exception{
        //6.釋放資源
        //提交事務(wù)
        sqlSession.commit();
        sqlSession.close();
        in.close();
    }
 
    //入門案例
    @Test
    public void testFindAllAccount(){
        List<Account> accounts = accountDao.findAll();
        System.out.println("------每個account的信息------");
        for (Account account : accounts) {
            System.out.println(account);
            System.out.println(account.getUser());
 
        }
    }
    @Test
    public void testFindAccount(){
           Account account = accountDao.findById(46);
        System.out.println(account);
    }
 
 
    @Test
    public void testFindAllUser(){
        List<User> users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
            System.out.println(user.getAccounts());
        }
    }
    @Test
    public void testFindUser(){
        User user = userDao.findById(49);
        System.out.println(user);
    }
}

到此這篇關(guān)于MyBatis延遲加載與立即加載案例教程的文章就介紹到這了,更多相關(guān)MyBatis延遲加載與立即加載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot解決XSS存儲型漏洞問題

    springboot解決XSS存儲型漏洞問題

    這篇文章主要介紹了springboot解決XSS存儲型漏洞問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Spring切入點(diǎn)表達(dá)式配置過程圖解

    Spring切入點(diǎn)表達(dá)式配置過程圖解

    這篇文章主要介紹了Spring切入點(diǎn)表達(dá)式配置過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(8)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(8)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07
  • Java中Scanner用法實(shí)例解析

    Java中Scanner用法實(shí)例解析

    Scanner?指的是java.util包下的Scanner類,可以接收控制臺輸入的數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Java中Scanner用法實(shí)例的相關(guān)資料,文中通過實(shí)例代碼以及圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • 手寫java性能測試框架的實(shí)現(xiàn)示例

    手寫java性能測試框架的實(shí)現(xiàn)示例

    這篇文章主要為大家介紹了java實(shí)現(xiàn)性能測試框架示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • Java中的接口以及常見的Cloneable接口用法

    Java中的接口以及常見的Cloneable接口用法

    這篇文章主要介紹了Java中的接口以及常見的Cloneable接口用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • spring中@Reference注入為空的解決方法

    spring中@Reference注入為空的解決方法

    今天上線遇到了問題,所以抽空記錄一下,本文主要介紹了spring中@Reference注入為空的解決方法,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • 詳解JAVA類加載機(jī)制

    詳解JAVA類加載機(jī)制

    這篇文章主要介紹了JAVA類加載機(jī)制的相關(guān)知識,文中代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 淺談Java對象禁止使用基本類型

    淺談Java對象禁止使用基本類型

    本文主要介紹了淺談Java對象禁止使用基本類型,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Java模擬死鎖發(fā)生之演繹哲學(xué)家進(jìn)餐問題案例詳解

    Java模擬死鎖發(fā)生之演繹哲學(xué)家進(jìn)餐問題案例詳解

    這篇文章主要介紹了Java模擬死鎖發(fā)生之演繹哲學(xué)家進(jìn)餐問題,結(jié)合具體演繹哲學(xué)家進(jìn)餐問題的案例形式詳細(xì)分析了死鎖機(jī)制與原理,需要的朋友可以參考下
    2019-10-10

最新評論