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

解決mybatis一對(duì)多查詢r(jià)esultMap只返回了一條記錄問題

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

問題描述:因?yàn)轭I(lǐng)導(dǎo)的一個(gè)需求,需要用到使用resultMap,很久沒使用了,結(jié)果就除了點(diǎn)意外。就記錄下這個(gè)問題
準(zhǔn)備兩個(gè)類:author(作者)和book(書),數(shù)據(jù)庫創(chuàng)建對(duì)應(yīng)的author->book一對(duì)多的數(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對(duì)應(yīng)book有兩條記錄,結(jié)果books里面只返回了一條記錄。
問題原因:2張表的主鍵都叫id,所以導(dǎo)致結(jié)果不能正確展示。
解決方法:1、主鍵使用不用的字段名。2、查詢sql時(shí)使用別名
1、主鍵使用不用的字段名,涉及到更改數(shù)據(jù)庫,只需要更改其中一個(gè)即可 。這里演示將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時(shí)使用別名。這里演示將查詢book時(shí)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一對(duì)多查詢r(jià)esultMap只返回了一條記錄的文章就介紹到這了,更多相關(guān)mybatis一對(duì)多查詢r(jià)esultMap內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

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

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

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

    feign的ribbon超時(shí)配置和hystrix的超時(shí)配置說明

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

    SpringBoot disruptor高性能隊(duì)列使用

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

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

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

    Mybatis?resultMap標(biāo)簽繼承、復(fù)用、嵌套方式

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

    Java修改PowerPoint幻燈片批注信息

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

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

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

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

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

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

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

最新評(píng)論

美姑县| 达日县| 九江县| 涿州市| 镇康县| 哈密市| 北川| 佛坪县| 公安县| 肃北| 开封县| 若羌县| 惠来县| 道孚县| 成武县| 大新县| 多伦县| 定州市| 兴安县| 渭源县| 安图县| 乌审旗| 昭平县| 通海县| 公主岭市| 米林县| 利川市| 安阳市| 定日县| 九龙县| 商河县| 建德市| 连州市| 和田县| 承德县| 武宁县| 石门县| 荔浦县| 讷河市| 甘孜县| 稻城县|