亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解決mybatis一對多查詢resultMap只返回了一條記錄問題

 更新時間:2021年11月27日 12:27:51   作者:黑夜長行  
小編接到領(lǐng)導一個任務(wù)需求,需要用到使用resultMap相關(guān)知識,在這小編記錄下這個問題的解決方法,對mybatis一對多查詢resultMap項目知識感興趣的朋友一起看看吧

問題描述:因為領(lǐng)導的一個需求,需要用到使用resultMap,很久沒使用了,結(jié)果就除了點意外。就記錄下這個問題
準備兩個類:author(作者)和book(書),數(shù)據(jù)庫創(chuàng)建對應(yīng)的author->book一對多的數(shù)據(jù)

@Data
public class Author {
    private Integer id;
    private String name;
    private String phone;
    private String address;
    private List<Book> books;
}

@Data
public class Book {
    private Integer id;
    private String name;
    private String press;
    private BigDecimal price;
    private Integer authorId;
}

開始的Mapper.xml文件

<resultMap id="bookMap" type="com.example.demo.dto.Author">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="address" property="address"></result>
        <result column="phone" property="phone"></result>
        <collection property="books" ofType="com.example.demo.dto.Book">
            <id column="id" property="id"></id>
            <result column="name" property="name"></result>
            <result column="press" property="press"></result>
            <result column="price" property="price"></result>
            <result column="author_id" property="authorId"></result>
        </collection>
    </resultMap>
    <select id="queryAuthorInfo" parameterType="java.lang.String" resultMap="bookMap">
        select t1.*,t2.* from
        author t1 inner join book t2 on t1.id=t2.author_id
        where t1.id=#{id}
    </select>

使用postman執(zhí)行查看結(jié)果:

{
    "code": "200",
    "msg": "成功",
    "data": {
        "id": 1,
        "name": "法外狂徒張三",
        "phone": null,
        "address": null,
        "books": [
            {
                "id": 1,
                "name": "法外狂徒張三",
                "press": "人民出版社",
                "price": 10.00,
                "authorId": 1
            }
        ]
    }
}

發(fā)現(xiàn)問題:本來author對應(yīng)book有兩條記錄,結(jié)果books里面只返回了一條記錄。
問題原因:2張表的主鍵都叫id,所以導致結(jié)果不能正確展示。
解決方法:1、主鍵使用不用的字段名。2、查詢sql時使用別名
1、主鍵使用不用的字段名,涉及到更改數(shù)據(jù)庫,只需要更改其中一個即可 。這里演示將book的id更改為book_id

<resultMap id="bookMap" type="com.example.demo.dto.Author">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="address" property="address"></result>
        <result column="phone" property="phone"></result>
        <collection property="books" ofType="com.example.demo.dto.Book">
            <!---更改book類的id為bookId,數(shù)據(jù)庫book的id更改為book_id-->
            <id column="book_id" property="bookId"></id>
            <result column="name" property="name"></result>
            <result column="press" property="press"></result>
            <result column="price" property="price"></result>
            <result column="author_id" property="authorId"></result>
        </collection>
    </resultMap>
    <select id="queryAuthorInfo" parameterType="java.lang.String" resultMap="bookMap">
        select t1.*,t2.* from
        author t1 inner join book t2 on t1.id=t2.author_id
        where t1.id=#{id}
    </select>

2、查詢sql時使用別名。這里演示將查詢book時id 更改別名為 bookId

<resultMap id="bookMap" type="com.example.demo.dto.Author">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="address" property="address"></result>
        <result column="phone" property="phone"></result>
        <collection property="books" ofType="com.example.demo.dto.Book">
            <!---這里將column值id更改為別名一致bookId-->
            <id column="bookId" property="id"></id>
            <result column="name" property="name"></result>
            <result column="press" property="press"></result>
            <result column="price" property="price"></result>
            <result column="author_id" property="authorId"></result>
        </collection>
    </resultMap>
    <select id="queryAuthorInfo" parameterType="java.lang.String" resultMap="bookMap">
        <!---這里新增了t2.id as bookId-->
        select t1.*,t2.id as bookId, t2.* from
        author t1 inner join book t2 on t1.id=t2.author_id
        where t1.id=#{id}
    </select>

到此這篇關(guān)于mybatis一對多查詢resultMap只返回了一條記錄的文章就介紹到這了,更多相關(guān)mybatis一對多查詢resultMap內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring手動生成web.xml配置文件過程詳解

    Spring手動生成web.xml配置文件過程詳解

    這篇文章主要介紹了Spring手動生成web.xml配置文件過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • RocketMQ?Broker消息如何刷盤源碼解析

    RocketMQ?Broker消息如何刷盤源碼解析

    這篇文章主要為大家介紹了RocketMQ?Broker消息如何刷盤源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • 使用Springboot實現(xiàn)OAuth服務(wù)的示例詳解

    使用Springboot實現(xiàn)OAuth服務(wù)的示例詳解

    OAuth(Open Authorization)是一個開放標準,用于授權(quán)第三方應(yīng)用程序訪問用戶資源,而不需要共享用戶憑證。本文主要介紹了如何使用Springboot實現(xiàn)一個OAuth服務(wù),需要的可以參考一下
    2023-05-05
  • Spring?Native打包本地鏡像的操作方法(無需通過Graal的maven插件buildtools)

    Spring?Native打包本地鏡像的操作方法(無需通過Graal的maven插件buildtools)

    這篇文章主要介紹了Spring?Native打包本地鏡像,無需通過Graal的maven插件buildtools,本文探索一下,如果不通過這個插件來生成鏡像,結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2023-02-02
  • SpringBoot響應(yīng)處理實現(xiàn)流程詳解

    SpringBoot響應(yīng)處理實現(xiàn)流程詳解

    這篇文章主要介紹了SpringBoot響應(yīng)處理實現(xiàn)流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-10-10
  • Springboot配置圖片虛擬映射示例代碼

    Springboot配置圖片虛擬映射示例代碼

    這篇文章主要給大家介紹了關(guān)于Springboot配置圖片虛擬映射的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家學習或者使用springboot具有一定的參考學習價值,需要的朋友可以參考下
    2021-11-11
  • java使用mybatis調(diào)用存儲過程返回一個游標結(jié)果集方式

    java使用mybatis調(diào)用存儲過程返回一個游標結(jié)果集方式

    這篇文章主要介紹了java使用mybatis調(diào)用存儲過程返回一個游標結(jié)果集方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • java實現(xiàn)圖片反色處理示例

    java實現(xiàn)圖片反色處理示例

    這篇文章主要為大家詳細介紹了java實現(xiàn)圖片反色處理示例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Java編程獲取文本框的內(nèi)容實例解析

    Java編程獲取文本框的內(nèi)容實例解析

    這篇文章主要介紹了Java編程獲取文本框的值實例解析,將輸入的值保存在一個指定的 txt文件之中,具有一定的參考價值,需要的朋友可以了解。
    2017-09-09
  • Java舉例講解分治算法思想

    Java舉例講解分治算法思想

    分治算法的基本思想是將一個規(guī)模為N的問題分解為K個規(guī)模較小的子問題,這些子問題相互獨立且與原問題性質(zhì)相同。求出子問題的解,就可得到原問題的解,本篇文章我們就用分治算法來實現(xiàn)歸并排序快速排序以及二分搜索算法
    2022-04-04

最新評論