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

java工程師進階之MyBatis延遲加載的使用

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

什么是延遲加載?

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

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

如何使用延遲加載?

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

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

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

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工程師進階之MyBatis延遲加載的使用的詳細內容,更多關于java之MyBatis延遲加載的資料請關注腳本之家其它相關文章!

相關文章

最新評論

邵阳市| 荣成市| 曲周县| 饶平县| 洛南县| 叶城县| 互助| 榆中县| 庆阳市| 九台市| 白山市| 岱山县| 庆安县| 靖江市| 兰西县| 廊坊市| 玉环县| 兴隆县| 赤水市| 成都市| 长岭县| 涪陵区| 拜泉县| 渝北区| 芮城县| 望城县| 靖州| 苗栗市| 孝昌县| 高邑县| 电白县| 诸城市| 塔城市| 许昌县| 镇安县| 门头沟区| 建昌县| 揭西县| 应城市| 西乌| 东阳市|