怎么解决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)

相关推荐

  • 关于爱情的英语句子,求一些英文的关于爱情的短句子。

    技术关于爱情的英语句子,求一些英文的关于爱情的短句子。哈哈这个我知道关于爱情的英语句子,我帮你吧~ 1我的世界不允许你的消失,不管结局是否完美. No matter the ending is perfect or no

    生活 2021年10月25日
  • 黄芩功效,黄芩的功效与作用是什么

    技术黄芩功效,黄芩的功效与作用是什么黄芩的功效与作用是什么黄芩功效?黄芩的功效和作用:具有清热燥湿、泻火解毒、止血、安胎的功效;具有解热、抑菌、抗炎、缓解气管收缩、保肝、利胆、降压、抑制血小板聚集、降血脂、抗氧化、抗肿瘤

    生活 2021年10月26日
  • 全国商品防伪查询中心,怎么办理315防伪标签

    技术全国商品防伪查询中心,怎么办理315防伪标签全国315产品防伪查询中心 第一步:企业要提供企业资质,如公司营业执照、税务登记证、商标注册证、生产许可证等复印件加盖公章全国商品防伪查询中心。 第二步:提供品牌的商标图案

    生活 2021年10月30日
  • 使用代理ip为什么要选择高质量正规动态ip

    技术使用代理ip为什么要选择高质量正规动态ip这篇文章主要讲解了“使用代理ip为什么要选择高质量正规动态ip”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“使用代理ip为什

    攻略 2021年11月2日
  • Java学习笔记102——StringBuffer类的成员方法—替换功能

    技术Java学习笔记102——StringBuffer类的成员方法—替换功能 Java学习笔记102——StringBuffer类的成员方法—替换功能StringBuffer类的成员方法——替换功能
    S

    礼包 2021年12月17日
  • kafka分区数量设置多少合适(kafka 分区个数与集群大小的关系)

    技术如何为Kafka集群确定合适的分区数以及分区数过多带来的弊端如何为Kafka集群确定合适的分区数以及分区数过多带来的弊端,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到

    攻略 2021年12月15日