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

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

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

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

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

相關文章

  • 詳解Java的文件與目錄管理以及輸入輸出相關操作

    詳解Java的文件與目錄管理以及輸入輸出相關操作

    這篇文章主要介紹了詳解Java的文件與目錄管理以及輸入輸出相關操作,是Java入門學習中的基礎知識,需要的朋友可以參考下
    2015-09-09
  • 雙token實現(xiàn)token超時策略示例

    雙token實現(xiàn)token超時策略示例

    用于restful的app應用無狀態(tài)無sesion登錄示例,需要的朋友可以參考下
    2014-02-02
  • feign的ribbon超時配置和hystrix的超時配置說明

    feign的ribbon超時配置和hystrix的超時配置說明

    這篇文章主要介紹了feign的ribbon超時配置和hystrix的超時配置說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • SpringBoot disruptor高性能隊列使用

    SpringBoot disruptor高性能隊列使用

    這篇文章主要介紹了SpringBoot disruptor高性能隊列使用,Disruptor是英國外匯交易公司LMAX開發(fā)的一個高性能隊列,研發(fā)的初衷是解決內(nèi)存隊列的延遲問題
    2023-02-02
  • Java多線程之搞定最后一公里詳解

    Java多線程之搞定最后一公里詳解

    Java 給多線程編程提供了內(nèi)置的支持。 一條線程指的是進程中一個單一順序的控制流,一個進程中可以并發(fā)多個線程,每條線程并行執(zhí)行不同的任務,多線程是多任務的一種特別的形式,但多線程使用了更小的資源開銷
    2021-10-10
  • Mybatis?resultMap標簽繼承、復用、嵌套方式

    Mybatis?resultMap標簽繼承、復用、嵌套方式

    這篇文章主要介紹了Mybatis?resultMap標簽繼承、復用、嵌套方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java修改PowerPoint幻燈片批注信息

    Java修改PowerPoint幻燈片批注信息

    這篇文章主要介紹了Java修改PowerPoint幻燈片批注信息,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • Java實現(xiàn)按中文首字母排序的具體實例

    Java實現(xiàn)按中文首字母排序的具體實例

    這篇文章主要介紹了Java實現(xiàn)按中文首字母排序的具體實例,有需要的朋友可以參考一下
    2013-12-12
  • 關于工廠方法模式的Java實現(xiàn)

    關于工廠方法模式的Java實現(xiàn)

    這篇文章主要介紹了關于工廠方法模式的Java實現(xiàn)講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringCloud2020整合Nacos-Bootstrap配置不生效的解決

    SpringCloud2020整合Nacos-Bootstrap配置不生效的解決

    這篇文章主要介紹了SpringCloud2020整合Nacos-Bootstrap配置不生效的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01

最新評論

高淳县| 平果县| 合川市| 邢台市| 吉木萨尔县| 广灵县| 甘肃省| 丹寨县| 益阳市| 岗巴县| 保山市| 鱼台县| 本溪市| 马尔康县| 平果县| 绍兴市| 观塘区| 金湖县| 开原市| 武宣县| 乌拉特中旗| 遵义市| 湘乡市| 绥棱县| 尤溪县| 沂水县| 新泰市| 昌图县| 寻乌县| 诸城市| 东港市| 余江县| 辽宁省| 南召县| 平顺县| 宁城县| 扎鲁特旗| 理塘县| 绩溪县| 祁阳县| 鄯善县|