解决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一对多查询resultMap内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • mybatis高级映射一对多查询实现代码

    1.需求分析: 在开发中会遇到这样一个问题,查询订单信息,级联查询出用户信息和订单明细信息 2.sql语句实现 2.1确定主查询表:订单表 2.2确定关联查询表:用户表, 订单明细表 sql语句如下: select orders.*, t_user.address, t_user.name, t_user.brithday, orderdetail.id orderdetail_id, orderdetail.orderid, orderdetail.itemsid from orders, t

  • Mybatis自关联查询一对多查询的实现示例

    注:代码已托管在GitHub上,地址是:https://github.com/Damaer/Mybatis-Learning ,项目是mybatis-13-oneself-one2many,需要自取,需要配置maven环境以及mysql环境(sql语句在resource下的test.sql中),觉得有用可以点个小星星. docsify文档地址在:https://damaer.github.io/Mybatis-Learning/#/ 所谓自关联查询,是指自己既然充当一方,又充当多方.比如新闻栏目

  • 解析mybatis-plus中的resultMap简单使用

    不一致,那么用来接收查询出来的result对应的数据将会是Null,如果不使用resultMap,那么一般为了避免pojo对象对应的属性为Null,会采用SQL语句中的别名,将查询出的数据库中的字段as pojo对象属性,而且,resultMap支持延迟加载 但是为了避免sql语句看着 臃肿,所以就使用了resultMap   简单使用  1.    选定你要进行的resultMap映射的model,如下是我要进行映射的model,model=======>Category.java[进行res

  • mybatis通过中间表实现一对多查询功能

    需求: 通过一个学生的id查询出该学生所学的所有科目. 使用到的表格: 1.student:学生表 2.subject:科目表 3.stu_sub:学生-科目表(这里的成绩字段没用到,不用管) 实体类( get.set方法省略): 1.student public class Student implements Serializable { private int id; private String stuNum; //学号 private String password; private

  • Mybatis一对多查询的两种姿势(值得收藏)

    前言 最近碰到了Mybatis一对多查询的场景,在这里总结对比下常见的两种实现方式. 本文以常见的订单表和订单详情表来举例说明: 数据库表准备 订单表 tbl_order 订单详情表 tlb_order_detail ps: 一个订单关联多个订单详情,通过order_no订单号关联: 实例演示 方法一:联合查询ResultMap映射 sql直接关联查询,然后结果集通过resultMap的collection映射 例如 查询订单列表,包括订单详情 Order.java 中新增字段orderDeta

  • mybatis一对多查询功能

    首先,我们还是先给出一个需求:根据订单id查询订单明细--我们知道,一个订单里面可以有多个订单的明细(需求不明确的同学,请留言或者去淘宝网上的订单处点一下就知道了).这个时候,一个订单,对应多个订单的id.这种需求出现的时候,我们应该如何查询呢? 此时我们的数据模型如下图(左)由于查询用户也是我们的需求,所以就在原有的基础上进行扩展,数据模型如下(右): 很显然,如果用resultType的方式去实现的话,是不合理的了.因为我们需要创建一个既有订单又有订单明细的pojo然后呢,我们的mybati

  • 解决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> book

  • 解决mybatis一对多关联查询多条数据只显示一条的问题

    一对多,如果多个表字段名相同,要记住使用别名,否则多条数据只显示一条 <resultMap type="com.example.demo.model.TuserModel" id="extendMapper"> <id column="id" property="id" /> <result column="user_name" property="userName&

  • 解决mybatis三表连接查询数据重复的问题

    此问题的产生,主要是数据库的字段名一样导致 三张表 DOCTOR JOB OBJECT 有问题的查询语句和查询结果是: SELECT d.*,j.*,o.* from (select d.*,rownum r from DOCTOR d where rownum<=6) d join job j on d.job_id=j.id join object o on o.id=d.object_id where r>0 <img src="https://img-blog.csdn

  • 基于mybatis一对多查询内层排序的问题

    目录 mybatis一对多查询内层排序 mybatis多排序问题 mybatis一对多查询内层排序 <!--根据板块id查询所有主题->指标->维度--> <resultMap id="TitleDimensionMap" type="com.etouch.admincenter.bean.ZhmdDiagnosisTitleBean"> <id column="title_id" property=&q

  • 解决mybatis执行SQL语句部分参数返回NULL问题

    今天在写代码的时候发现一个问题:mybatis执行sql语句的时候返回bean的部分属性为null,在数据库中执行该sql语句能够正常返回,把相关代码反反复复翻了个遍,甚至都重启eclipse了,依旧没解决问题,后来网上搜了一下,还真有类似的问题. 闲话少说,直接说问题,该sql语句是自己写的,resultType直接用了该bean全名称,最终导致部分属性显示为null, 原来的写法: <select id="selectByArticle" parametertype=&quo

  • 解决mybatis plus 分页查询有条数,total和pages都是零的问题

    一. 问题还原 1. Controller代码部分 Page<FixedAssetsEntity> pageForPlus = getPage(); Page<FixedAssetsEntity> fixedAssetsEntityPage = fixedAssetsService.selectPage(pageForPlus); 2.spring-mybatis.xml中的sqlSessionFactory配置 <bean id="sqlSessionFactor

  • SQL Server实现查询每个分组的前N条记录

    SQL语句查询每个分组的前N条记录的实现方法: 1.生成测试数据: #T if object_id('tempdb.dbo.#T') is not null drop table #T; create table #T (ID varchar(3), GID int, Author varchar(29), Title varchar(39), Date datetime); insert into #T select '001', 1, '邹建', '深入浅出SQLServer2005开发管理

随机推荐