mybatis一对多查询resultMap只返回了一条记录
问题描述:因为领导的一个需求,需要用到使用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只返回了一条记录的更多相关文章
- mybatis报错:查询一对多或多对多时只返回一条数据的问题
问题: 使用映射文件实现查询一对多或多对多时只返回一条数据问题 解决方法: 导致这种情况出现的问题是因为两个表中的主键是一样所以出现了数据覆盖问题. 解决方式一:修改数据库表中的主键(这种方法比较麻烦 ...
- mybatis 一对多查询 集合创建空对象的问题
在做 mybatis 一对多查询的时候, resultMap 里面用到了集合标签 collection ,后来发现 当该条数据没有子集的时候, collection 会自动创建一个属性都是null的对 ...
- 常见数据库SELECT结果只显示前几条记录方法汇总
常见数据库SELECT结果只显示前几条记录方法汇总 为了查看数据表中的数据情况.经常会遇到想让查询结果只显示N行,比如只显示10行的情况.不同的数据库有不同的关键字和SELECT实现语法. 1.SQL ...
- mysql查询各种类型的前N条记录
mysql查询各种类型的前N条记录,将3改为N(需查询条数)即可 (select * from event_info where event_type = 1 limit 3)union all( ...
- 21Mybatis_订单商品数据模型_一对多查询——resultMap方式
这篇文章延续订单商品数据模型,这张讲述的是一对多的查询.(用resultMap) 给出几张表的内容: User表:
- Mybatis一对多查询得不到多方结果
一对多查询:一个年级对应多个学生,现在要查询年级(带学生)信息. 查询结果: [main] INFO com.java1234.service.GradeTest - 查询年级(带学生)[main] ...
- (三)Mybatis类型转换器,接口传参类型,一对一,一对多查询resultMap配置
Mybatis类型转换器 首先明白什么时候用到它,当数据库的字段类型和java字段类型无法默认匹配时候进行转换,比如现在数据库类型是INTEGER,而java当中类型是Boolean,true表示1, ...
- Mybatis一对多/多对多查询时只查出了一条数据
问题描述: 如果三表(包括了关系表)级联查询,主表和明细表的主键都是id的话,明细表的多条数据只能查询出来第一条/最后一条数据. 三个表,权限表(Permission),权限组表(Permission ...
- mybatis一对多查询
18 <!-- 19 方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集 20 封装联表查询的数据(去除重复的数据) 21 select * from class c, teacher ...
随机推荐
- 洛谷4322 SHOI2014 三叉神经树(LCT+思维)
好久之前做的题了QWQ 现在来补一发博客 一道神仙题啊..qwq 首先,我们可以看出来,我们如果对于每个点维护一个\(val\),表示他的直系儿子中有几个表现为1的. 那么\(val[x]>&g ...
- 【分享】 一款自用的Anki卡片模板:黄子涵单词卡片 v1
[分享] 一款自用的Anki卡片模板:黄子涵单词卡片 v1 说明 第一代的功能 主要有两部分组成:英文和含义,目前主要是为自己记忆Web前端一些常用的单词而服务 有美美哒背景图,本来想修改为随机背景图 ...
- [NOIP2013 提高组] 华容道 P1979 洛谷
[NOIP2013 提高组] 华容道 P1979 洛谷 强烈推荐,更好的阅读体验 经典题目:spfa+bfs+转化 题目大意: 给出一个01网格图,和点坐标x,y空格坐标a,b,目标位置tx,ty要求 ...
- SignalR 在React/GO技术栈的生产应用
哼哧哼哧半年,优化改进了一个运维开发web平台. 本文记录SignalR在react/golang 技术栈的生产小实践. 1. 背景 有个前后端分离的运维开发web平台, 后端会间隔5分钟同步一次数据 ...
- Arp欺骗和DNS投毒的实验性分析
1.中间人攻击之Arp欺骗/毒化 本文涉及网络安全攻击知识,随时可能被永久删除.请Star我的GitHub仓库 实现原理: 这种攻击手段也叫做中间人攻击MITM(Man-in-the-Middle) ...
- 一文彻底搞通TCP之send & recv原理
接触过网络开发的人,大抵都知道,上层应用使用send函数发送数据,使用recv来接收数据,而send和recv的实现原理又是怎样的呢? 在前面的几篇文章中,我们有提过,TCP是个可靠的.全双工协议.其 ...
- VS Code Just My Code Debugging
VS Code Just My Code Debugging VS Code for C++ doesn't support Just My Code Refer here: Add support ...
- Noip模拟21(持续翻车)2021.7.20
读题总是读错是不是没救了... T1 Median 中位数:按顺序排列的一组数据中居于中间位置的数. 能用上的高亮符号都用上了... 当时忘了就离谱.... 理解什么是中位数(真是个憨憨)后就可以开始 ...
- docker 存储驱动(storage driver)知识总结
http://www.sohu.com/a/101016494_116235 一,先看docker镜像是如何构建和存储. 下面是ubuntu:15.04的镜像分层.一共是4层,每一层都由一些只读并且描 ...
- "迷途"的野指针,都快找不着北了
指针,C语言开发者表示很淦,指针的使用,很多人表示不敢直面ta,不像Java一样,有垃圾自动回收功能,我们不用担心那么多内存泄漏等问题,那C语言里边呢,指针又分为了"野指针",&q ...