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

springboot+mybatis一對多查詢+懶加載實例

 更新時間:2025年11月19日 11:07:45   作者:一枚小麻瓜  
文章介紹了如何在Spring Boot和MyBatis中實現(xiàn)一對多查詢的懶加載,通過配置MyBatis的`fetchType`屬性,可以全局啟用懶加載,或者在個別需要時關(guān)閉懶加載,文章通過代碼示例和測試結(jié)果,展示了懶加載的實現(xiàn)過程

springboot+mybatis一對多查詢+懶加載

?直接上圖:

  • 父表

  • 子表

parent相關(guān)代碼

entity

public class ParentMessage implements Serializable {

    private Integer id;

    private String value;

    private List<ChildMessage> childMessages;

    get set ......
}

mapper

@Repository
public interface ParentMessageMapper {

     List<ParentMessage> findAll();

     ParentMessage findById(Integer id);
}

service

@Service
public class ParentMessageService {

    @Autowired
    ParentMessageMapper parentMessageMapper;

    public List<ParentMessage> findAll(){

        return parentMessageMapper.findAll();
    }
}

mapper.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.demo.mapper.ParentMessageMapper">

    <resultMap id="parentMessageMap" type="parentMessage">
        <id column="id" property="id"/>
        <result column="value" property="value" javaType="String"/>
        <collection fetchType="eager" property="childMessages" column="id" select="com.demo.mapper.ChildMessageMapper.findByOtherId" javaType="List" typeHandler="com.demo.mybatistypehandler.ListTypeHandler"/>
    </resultMap>

    <select id="findAll" resultMap="parentMessageMap">
        select id , value from mk_parentmessage
    </select>

    <select id="findById" resultType="parentMessage">
        select * from mk_parentmessage;
    </select>
</mapper>

child 相關(guān)代碼

entity

public class ChildMessage implements Serializable {

    private Integer id;

    private String value;

    private ParentMessage parentMessage;

}

mapper

@Repository
public interface ChildMessageMapper {

    List<ChildMessage> findByOtherId(Integer id);

    List<ChildMessage> findAll();
}

service

@Service
public class ChildMessagService {

    @Autowired
    ChildMessageMapper childMessageMapper;

    public List<ChildMessage> findAll(){

        return childMessageMapper.findAll();
    }
}

mapper.xlm

<?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.demo.mapper.ChildMessageMapper">

    <resultMap id="childMessageMap" type="childMessage">
        <id column="id" property="id"/>
        <result column="value" property="value"/>
        <association property="parentMessage" column="parent_id" select="com.demo.mapper.ParentMessageMapper.findById" javaType="ParentMessage"/>
    </resultMap>

    <select id="findByOtherId" resultType="childMessage">
        select id id, value value from mk_childmessage;
    </select>

    <select id="findAll" resultMap="childMessageMap">
        select * from mk_childmessage;
    </select>
</mapper>

Controller測試

@RestController
@RequestMapping("message")
public class MessageController {

    @Autowired
    ParentMessageService parentMessageService;

    @Autowired
    ChildMessagService childMessagService;

    @GetMapping("findAll")
    public ResponseEntity getParentMessage(){

        return ResponseEntity.ok(parentMessageService.findAll(););
    }

}

返回結(jié)果:

懶加載

配置屬性

mybatis.configuration.lazy-loading-enabled=true
#false 為按需加載
mybatis.configuration.aggressive-lazy-loading=false

這樣就實現(xiàn)了全局懶加載,若個別需要關(guān)閉,可用 fetchType=“eager”

例如下圖:

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • VerifyCodeServlet(一次性驗證碼)

    VerifyCodeServlet(一次性驗證碼)

    這篇文章主要介紹了VerifyCodeServlet一次性驗證碼的使用方法
    2017-05-05
  • 淺談Java HttpURLConnection請求方式

    淺談Java HttpURLConnection請求方式

    這篇文章主要介紹了淺談Java HttpURLConnection請求方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Java經(jīng)典算法匯總之選擇排序(SelectionSort)

    Java經(jīng)典算法匯總之選擇排序(SelectionSort)

    選擇排序也是比較簡單的一種排序方法,原理也比較容易理解,選擇排序在每次遍歷過程中只記錄下來最小的一個元素的下標(biāo),待全部比較結(jié)束之后,將最小的元素與未排序的那部分序列的最前面一個元素交換,這樣就降低了交換的次數(shù),提高了排序效率。
    2016-04-04
  • 基于mybatis逆向工程的使用步驟詳解

    基于mybatis逆向工程的使用步驟詳解

    下面小編就為大家?guī)硪黄趍ybatis逆向工程的使用步驟詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Spring AOP失效的常見場景分析

    Spring AOP失效的常見場景分析

    Spring的AOP(面向切面編程)是一種強(qiáng)大的技術(shù),用于在應(yīng)用程序中實現(xiàn)橫切關(guān)注點的模塊化,雖然Spring的AOP在大多數(shù)情況下都是有效的,但在某些場景下可能會失效,下面來分析Spring AOP失效的常見場景,需要的朋友可以參考下
    2024-01-01
  • JavaEE中關(guān)于ServletConfig的小結(jié)

    JavaEE中關(guān)于ServletConfig的小結(jié)

    ServletConfig是針對特定的Servlet的參數(shù)或?qū)傩浴ervletConfig是表示單獨的Servlet的配置和參數(shù),只是適用于特定的Servlet。從一個servlet被實例化后,對任何客戶端在任何時候訪問有效,但僅對本servlet有效,一個servlet的ServletConfig對象不能被另一個servlet訪問
    2014-10-10
  • log4j中l(wèi)ogger標(biāo)簽中additivity屬性的用法說明

    log4j中l(wèi)ogger標(biāo)簽中additivity屬性的用法說明

    這篇文章主要介紹了log4j中l(wèi)ogger標(biāo)簽中additivity屬性的用法說明,基于很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java利用trueLicense實現(xiàn)項目離線證書授權(quán)操作步驟

    Java利用trueLicense實現(xiàn)項目離線證書授權(quán)操作步驟

    文章介紹了如何使用trueLicense實現(xiàn)離線授權(quán)控制,包括生成公私鑰、創(chuàng)建證書校驗?zāi)K、生成證書模塊和測試模塊,通過這種方式,可以控制用戶使用的項目模塊、授權(quán)周期、使用的設(shè)備和服務(wù)器,感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • Java8的Stream()與ParallelStream()的區(qū)別說明

    Java8的Stream()與ParallelStream()的區(qū)別說明

    這篇文章主要介紹了Java8的Stream()與ParallelStream()的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java實現(xiàn)上傳網(wǎng)絡(luò)圖片到七牛云存儲詳解

    Java實現(xiàn)上傳網(wǎng)絡(luò)圖片到七牛云存儲詳解

    這篇文章主要為大家詳細(xì)介紹了Java如何實現(xiàn)上傳網(wǎng)絡(luò)圖片到七牛云存儲,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-12-12

最新評論

金乡县| 木兰县| 且末县| 浦城县| 皋兰县| 保山市| 永川市| 兴宁市| 平山县| 深州市| 常州市| 射洪县| 交城县| 泰宁县| 贺州市| 阳江市| 岱山县| 怀来县| 台前县| 四会市| 江阴市| 建昌县| 佛冈县| 勐海县| 文昌市| 水富县| 吐鲁番市| 开阳县| 辽阳县| 巫山县| 北流市| 大宁县| 山东| 张北县| 进贤县| 饶平县| 东平县| 绥化市| 鄂温| 渭源县| 女性|