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

相关推荐

  • MySQL 5.5中SHOW PROFILE、SHOW PROFILES语句怎么用

    技术MySQL 5.5中SHOW PROFILE、SHOW PROFILES语句怎么用这篇文章将为大家详细讲解有关MySQL 5.5中SHOW PROFILE、SHOW PROFILES语句怎么用,小编觉得挺实用的,因此

    攻略 2021年10月30日
  • Python使用协程的缺点是什么

    技术Python使用协程的缺点是什么这篇文章给大家分享的是有关Python使用协程的缺点是什么的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1、多核资源不能使用:协程的本质是单线程,它不能同时

    攻略 2021年10月27日
  • Java如何实现精美网上音乐平台

    技术Java如何实现精美网上音乐平台这篇文章主要介绍Java如何实现精美网上音乐平台,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、项目简述本系统功能包括: 音乐播放 用户登录注册 用户信息编辑

    攻略 2021年11月12日
  • golang中的原子操作atomic包

    技术golang中的原子操作atomic包 golang中的原子操作atomic包1. 概念 原子操作 atomic 包 加锁操作涉及到内核态的上下文切换,比较耗时,代价高, 针对基本数据类型我们还可以

    礼包 2021年11月10日
  • hive里面怎么查所有报表中的数据(hive大数据怎么查询所有表)

    技术报表连hive时数据量比较大如何分页查询小编给大家分享一下报表连hive时数据量比较大如何分页查询,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!Hive 提供了类似 Oracle 的 rownum

    攻略 2021年12月16日
  • bootstrap怎么设置悬浮窗

    技术bootstrap怎么设置悬浮窗这篇文章主要介绍“bootstrap怎么设置悬浮窗”,在日常操作中,相信很多人在bootstrap怎么设置悬浮窗问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家

    攻略 2021年10月29日