怎么解决mybatis一对多查询resultMap只返回了一条记录问题

技术怎么解决mybatis一对多查询resultMap只返回了一条记录问题本篇内容介绍了“怎么解决mybatis一对多查询resultMap只返回了一条记录问题”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困

本篇内容介绍了”怎么解决框架一对多查询结果图只返回了一条记录问题”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

问题描述:因为领导的一个需求,需要用到使用结果图,很久没使用了,结果就除了点意外。就记录下这个问题

准备两个类:作者(作者)和书(书),数据库创建对应的作者书籍一对多的数据

@数据

publicclassAuthor{

privateIntegerid

私有字符串名称

privateStringphone

私有字符串地址

私人书籍

}

@数据

publicclassBook{

privateIntegerid

私有字符串名称

privateStringpress

privateBigDecimalprice

privateIntegerauthorId

}开始的Mapper.xml文件

resultMapid=’ bookMap ‘ type=’ com。例子。演示。dto。作者

idcolumn=’id’property=’id’/id

结果列=’ name ‘属性=’ name ‘/结果

resultcolumn=“地址”属性=’地址’/结果

结果列=’ phone ‘属性=’ phone ‘/结果

集合属性=类型为“com”的“books”。例子。演示。去。“书”

idcolumn=’id’property=’id’/id

结果列=’ name ‘属性=’ name ‘/结果

结果列=’ press ‘属性=’ press ‘/result

n

bsp;   <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执行查看结果:

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

发现问题:本来author对应book有两条记录,结果books里面只返回了一条记录。
问题原因:2张表的主键都叫id,所以导致结果不能正确展示。
解决方法:1、主键使用不用的字段名。2、查询sql时使用别名
1、主键使用不用的字段名,涉及到更改数据库,只需要更改其中一个即可 。这里演示将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,数据库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只返回了一条记录问题”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/123882.html

(0)

相关推荐

  • oracle表优化方法教程

    技术oracle表优化方法教程这篇文章主要讲解了“oracle表优化方法教程”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“oracle表优化方法教程”吧!1、建立实验表c

    攻略 2021年11月11日
  • 梦想当老师的英语作文,我的梦想老师英语作文50字左右

    技术梦想当老师的英语作文,我的梦想老师英语作文50字左右《My dream is to be a teacher》i have a dream,i want to be a english teacher.first,e

    生活 2021年10月28日
  • 汇编 | 8086 DEBUG调试学习笔记

    技术汇编 | 8086 DEBUG调试学习笔记 汇编 | 8086 DEBUG调试学习笔记在8086汇编中DEBUG是个非常实用的工具,并且可以非常明了的查看每一步指令每一个段的相对状态,有利于学习。下

    礼包 2021年11月13日
  • 家养狗狗排行榜,有哪些适合家养的中型狗狗

    技术家养狗狗排行榜,有哪些适合家养的中型狗狗有哪些适合家养的中型犬家养狗狗排行榜?种类有很多~其中有几种是本人了解的想养的也有养过的~不懂的就不给您乱说啦[捂脸]第一种(排名不分前后)史宾格犬:这种犬类属于猎犬,也多用于

    生活 2021年10月21日
  • 如何用Python和Pygame模块构建一个游戏框架

    技术如何用Python和Pygame模块构建一个游戏框架如何用Python和Pygame模块构建一个游戏框架,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。通

    攻略 2021年10月26日
  • css又称什么

    技术css又称什么这篇文章主要介绍“css又称什么”,在日常操作中,相信很多人在css又称什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”css又称什么”的疑惑有所帮助!接下来,请跟着小

    攻略 2021年11月17日