解决mybatis一对多查询resultMap只返回了一条记录问题
作者:黑夜长行 发布时间:2022-07-08 08:36:31
标签: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>
来源:https://www.cnblogs.com/yl97/p/15611487.html


猜你喜欢
- 很长时间以来一直代码中用的比较多的数据列表主要是List,而且都是ArrayList,感觉有这个玩意就够了。ArrayList是用于实现动态
- 注解实现自动装配@Autowire注解@Autowire注解,自动装配通过类型,名字如果Autowire不能唯一自动装配上属性,则需要通过@
- 作为最基础的引用数据类型,Java 设计者为 String 提供了字符串常量池以提高其性能,那么字符串常量池的具体原理是什么,我们带着以下三
- 废话不多说,直接给大家贴代码了,具体代码如下所示:package com.luo.wctweb.util; import java.awt
- 单一职责原则:一个类,只有一个引起它变化的原因。为什么需要单一职责原则?如果一个类有多个原因要去修改它,那么修改一个功能时,可能会让其他功能
- 节点折叠 this.treeList1.CollapseAll(); 一、简介二、属性列表1、OptionsSelection:
- 最近在学习DataBinding的使用,中间遇到了不少的坑,记录以下,帮助以后学习DataBinding的朋友。有一个 ViewModel
- 1.连接池在实际开发中都会使用连接池因为它可以减少我们获取连接所消耗的时间连接池就是用于存储连接的一个容器,容器其实就是一个集合对象,该集合
- 数组是一种数据结构,其声明方式如下:type[] arrayName;数组具有以下属性: 1.数组可以是
- Android获取系统内核版本的方法 &nbs
- 棋牌类游戏是目前比较火的游戏之一。今天本文就以实例形式实现洗牌游戏。本文实例所采用的算法是:遍历每个位置上的牌,然后与随机位置上的牌交换。运
- 本文实例讲述了Java Swing实现JTable检测单元格数据变更事件的方法。分享给大家供大家参考,具体如下:在JTable的初级教程中往
- 先记录下jdk8之前的一些帮助方法判断time是否在now的n天之内/** * 判断time是否在now的n天之内
- 1. 包装类的介绍针对八种基本数据类型定义相应的引用类型--包装类(封装类),有了类的热点后,就可以调用类中的方法2. 基本数据类型 --&
- 日期类计算器的模拟实现::1.获取某年某月的天数int GetMonthDay(int year, int month){static in
- 需求背景最近的一个项目,在项目基本完工的阶段,客户提出要将所有业务操作的日志记录到数据库中,并且要提取一些业务的关键信息(比如交易单号)体现
- PagerBottomTabStrip 是一个基本按谷歌Material Design规范完成的安卓底部导航栏控件官方设计规范:https:
- 本文实例为大家分享了C#控制台实现飞行棋小游戏的具体代码,供大家参考,具体内容如下游戏标题static void ShowTitle() &
- Spring security 重写Filter实现json登录在使用SpringSecurity中,大伙都知道默认的登录数据是通过key/
- logback-classic 使用testCompile的打包今天使用gradle打包的时候logback-classic一直打包不进去。