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

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

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

{
"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只返回了一条记录的更多相关文章

  1. mybatis报错:查询一对多或多对多时只返回一条数据的问题

    问题: 使用映射文件实现查询一对多或多对多时只返回一条数据问题 解决方法: 导致这种情况出现的问题是因为两个表中的主键是一样所以出现了数据覆盖问题. 解决方式一:修改数据库表中的主键(这种方法比较麻烦 ...

  2. mybatis 一对多查询 集合创建空对象的问题

    在做 mybatis 一对多查询的时候, resultMap 里面用到了集合标签 collection ,后来发现 当该条数据没有子集的时候, collection 会自动创建一个属性都是null的对 ...

  3. 常见数据库SELECT结果只显示前几条记录方法汇总

    常见数据库SELECT结果只显示前几条记录方法汇总 为了查看数据表中的数据情况.经常会遇到想让查询结果只显示N行,比如只显示10行的情况.不同的数据库有不同的关键字和SELECT实现语法. 1.SQL ...

  4. mysql查询各种类型的前N条记录

    mysql查询各种类型的前N条记录,将3改为N(需查询条数)即可  (select * from event_info where event_type = 1  limit 3)union all( ...

  5. 21Mybatis_订单商品数据模型_一对多查询——resultMap方式

    这篇文章延续订单商品数据模型,这张讲述的是一对多的查询.(用resultMap) 给出几张表的内容: User表:

  6. Mybatis一对多查询得不到多方结果

    一对多查询:一个年级对应多个学生,现在要查询年级(带学生)信息. 查询结果: [main] INFO com.java1234.service.GradeTest - 查询年级(带学生)[main] ...

  7. (三)Mybatis类型转换器,接口传参类型,一对一,一对多查询resultMap配置

    Mybatis类型转换器 首先明白什么时候用到它,当数据库的字段类型和java字段类型无法默认匹配时候进行转换,比如现在数据库类型是INTEGER,而java当中类型是Boolean,true表示1, ...

  8. Mybatis一对多/多对多查询时只查出了一条数据

    问题描述: 如果三表(包括了关系表)级联查询,主表和明细表的主键都是id的话,明细表的多条数据只能查询出来第一条/最后一条数据. 三个表,权限表(Permission),权限组表(Permission ...

  9. mybatis一对多查询

    18 <!-- 19 方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集 20 封装联表查询的数据(去除重复的数据) 21 select * from class c, teacher ...

随机推荐

  1. 洛谷3176 [HAOI2015]数字串拆分 (矩阵乘法+dp)

    qwq真的是一道好题qwq自己做基本是必不可能做出来的. 首先,如果这个题目只是求一个\(f\)数组的话,那就是一道裸题. 首先,根据样例 根据题目描述,我们能发现其实同样数字的不同排列,也是属于不同 ...

  2. AES解密尾部出现乱码问题

    说明 在使用AES解密的时候我发现解密出来的字符串尾部一直都有乱码 解决方案 尾部字符串的ascii码就是删除位索引 具体代码: cryptor = AES.new('AES_KEY'.encode( ...

  3. Java(27)集合二List

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15228435.html 博客主页:https://www.cnblogs.com/testero ...

  4. 更好的 java 重试框架 sisyphus 的 3 种使用方式

    回顾 我们前面学习了 更好的 java 重试框架 sisyphus 入门简介 更好的 java 重试框架 sisyphus 配置的 2 种方式介绍 更好的 java 重试框架 sisyphus 背后的 ...

  5. Java:ArrayList类小记

    Java:ArrayList类小记 对 Java 中的 ArrayList类,做一个微不足道的小小小小记 概述 java.util.ArrayList 是大小可变的数组的实现,存储在内的数据称为元素. ...

  6. 数字在排序数组中出现的次数 牛客网 剑指Offer

    数字在排序数组中出现的次数 牛客网 剑指Offer 题目描述 统计一个数字在排序数组中出现的次数. class Solution: def GetNumberOfK(self, data, k): i ...

  7. 这一次,解决Flutter Dialog的各种痛点!

    前言 Q:你一生中闻过最臭的东西,是什么? A:我那早已腐烂的梦. 兄弟萌!!!我又来了! 这次,我能自信的对大家说:我终于给大家带了一个,能真正帮助大家解决诸多坑比场景的pub包! 将之前的flut ...

  8. nginx + tomcat 实现负载均衡

    1.环境准备 服务器A上安装 nginx 作为代理服务器 服务器B上安装 tomcat,~/webapps 下创建 /test目录,创建 /index.html 内容为T1(生产环境中一般是一样的wa ...

  9. 小白都能看懂的Spring源码揭秘之IOC容器源码分析

    目录 前言 IOC 只是一个 Map 集合 IOC 三大核心接口 IOC 初始化三大步骤 定位 加载 注册 总结 前言 在 Spring 框架中,大家耳熟能详的无非就是 IOC,DI,Spring M ...

  10. 【centos】更换yum源

    yum下载有时候很慢,可以换一下源: 步骤: 1)下载wget yum install -y wget 2)备份默认的yum mv /etc/yum.repos.d /etc/yum.repos.d. ...