最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java工程師進(jìn)階之MyBatis延遲加載的使用

 更新時(shí)間:2021年09月15日 11:46:42   作者:DrLai  
本文是java工程師進(jìn)階篇,主要介紹了java應(yīng)用開發(fā)中MyBatis延遲加載及如何使用,有需要的朋友 可以借鑒參考下,希望能夠有所幫助

什么是延遲加載?

延遲加載也叫懶加載、惰性加載,使⽤延遲加載可以提⾼程序的運(yùn)行效率,針對于數(shù)據(jù)持久層的操作, 在某些特定的情況下去訪問特定的數(shù)據(jù)庫,在其他情況下可以不訪問某些表,從⼀定程度上減少了 Java 應(yīng)⽤與數(shù)據(jù)庫的交互次數(shù)。

查詢學(xué)⽣和班級(jí)的時(shí),學(xué)生和班級(jí)是兩張不同的表,如果當(dāng)前需求只需要獲取學(xué)shengsheng的信息,那么查詢學(xué) ⽣單表即可,如果需要通過學(xué)⽣獲取對應(yīng)的班級(jí)信息,則必須查詢兩張表。 不同的業(yè)務(wù)需求,需要查詢不同的表,根據(jù)具體的業(yè)務(wù)需求來動(dòng)態(tài)減少數(shù)據(jù)表查詢的⼯作就是延遲加載。

如何使用延遲加載?

1.在 config.xml 中開啟延遲加載

<settings>
 <!-- 打印SQL-->
 <setting name="logImpl" value="STDOUT_LOGGING" />
 <!-- 開啟延遲加載 -->
 <setting name="lazyLoadingEnabled" value="true"/>
</settings>

2.將多表關(guān)聯(lián)查詢拆分成多個(gè)單表查詢

StudentRepository中

 public Student findByIdLazy(long id);

StudentRepository.xml

<resultMap id="studentMapLazy" type="entity.Student">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <association property="classes" javaType="entity.Classes" select="repository.ClassesRepository.findByIdLazy" column="cld">
        </association>
    </resultMap>
    <select id="findByIdLazy" parameterType="long" resultMap="studentMapLazy">
-- select s.id ,s.name,c.id as cid,c.name as cname from student s,classes c where s.id =1 and s.cld=c.id;
    select * from student where id=#{id};
    </select>

ClassesRepository

public Classes findByIdLazy(long id);
<resultMap id="classesMap" type="entity.Classes">
        <id column="cid" property="id"></id>
        <result column="cname" property="name"></result>
        <collection property="students" ofType="entity.Student">
            <id column="id" property="id"></id>
            <result column="name" property="name"></result>
        </collection>
    </resultMap>

以上就是java工程師進(jìn)階之MyBatis延遲加載的使用的詳細(xì)內(nèi)容,更多關(guān)于java之MyBatis延遲加載的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

紫云| 屏东市| 三都| 内丘县| 河津市| 灵璧县| 东辽县| 海南省| 长武县| 阳山县| 沾益县| 长丰县| 图木舒克市| 华蓥市| 香格里拉县| 松滋市| 金川县| 连山| 山阳县| 东乌珠穆沁旗| 北京市| 祁门县| 广东省| 都安| 怀来县| 高碑店市| 伊金霍洛旗| 合江县| 会昌县| 久治县| 东兴市| 申扎县| 斗六市| 朔州市| 南雄市| 博乐市| 潮州市| 萍乡市| 乌鲁木齐县| 北票市| 曲周县|