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>
「其他文章」
- Python-多线程及生产者与消费者
- git文件管理与索引,深入理解工作原理
- 【SpringCloud-Alibaba】环境搭建以及注意事项
- 小白也能看懂的Redis教学基础篇——做一个时间窗限流就是这么简单
- 谱聚类原理总结
- rocketmq实现延迟队列精确到秒级实现(总结编)
- 开发 IDEA Plugin 引入探针,基于字节码插桩获取执行SQL
- Chrome插件:提醒你正在摸鱼,摸鱼的时候知道自己在摸鱼,减少摸鱼的时间和频率。
- HttpRunner3的用例是怎么运行起来的
- RocketMQ 原理:消息存储、高可用、消息重试、消息幂等性
- k8s-pv-pvc
- JUC并发编程与高性能内存队列disruptor实战-下
- 谈谈最近做的一个自动化平台(二)
- 浅谈23种设计模式之单例设计模式
- Python 为什么不设计 do-while 循环结构?
- 【高并发】深入理解线程的执行顺序
- java日志打印使用指南
- 使用Rainbond打包业务模块,实现业务积木式拼装
- JS定时器不可靠的原因及解决方案
- 编程进阶之路,虽无捷径但有长短