广告
返回顶部
首页 > 资讯 > 数据库 >MyBatis一对多映射失败问题原因与解决方法(一对多查询返回结果只有一条)
  • 876
分享到

MyBatis一对多映射失败问题原因与解决方法(一对多查询返回结果只有一条)

mybatismysqljava 2023-09-16 20:09:20 876人浏览 薄情痞子
摘要

mybatis一对多映射失败问题原因与解决方法(一对多查询返回结果只有一条) 文章目录 MyBatis一对多映射失败问题原因与解决方法(一对多查询返回结果只有一条) @[toc

mybatis一对多映射失败问题原因与解决方法(一对多查询返回结果只有一条)


文章目录

1、问题描述


  • 前言:mybatis高级映射一对多失败原因有多种。

  • 问题描述:我这里出现的问题是:数据库中进行多表联查查询到多条记录结果是正常的一对多情况,但是mybatis映射时装载失败只保留了一条数据

    1) 数据库查询语句与结果如下

    • Mysql查询语句如下(一个用户(u.* )对应多条借阅记录(b.* )):
    select u.*,b.* from `users` as u        left join `borrow` as b        on b.`user_id` = u.`id`        where !isNull(b.book_id)
    • mysql查询结果如下:

      在这里插入图片描述

    2)mybatis映射文件如下

        <resultMap id="BaseResultMap2" type="com.wz.util.vo.BorrowListOut" >        <id column="id" property="id" jdbcType="INTEGER" />        <result column="username" property="username"                jdbcType="VARCHAR" />        <collection property="borrows" ofType="com.wz.entity.Borrow">                        <id column="id" property="id" jdbcType="INTEGER" />                         <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />            <result column="end_time" property="endTime" jdbcType="TIMESTAMP" />            <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />            <result column="ret" property="ret" jdbcType="INTEGER" />        collection>    resultMap>    <select id="findBorrows" resultMap="BaseResultMap2">        select u.*,b.* from `users` as u        left join `borrow` as b        on b.`user_id` = u.`id`        where !isNull(b.book_id)    select>

    3)代码展示

    //vo包下结果映射实体类: BorrowListOut.java======================================================================================@Datapublic class BorrowListOut extends Users {        @apiModelProperty("借阅记录")    private List<Borrow> borrows;}//test包下:测试代码======================================================================================@SpringBootTest(classes = ManagerApplication.class)public class BorrowTest {    @Autowired    private BorrowService borrowService;    @Test    void testFandBorrows() {        List<BorrowListOut> borrows = borrowService.findBorrows();                for (BorrowListOut borrowListOut : borrows) {            System.out.println("用户:"+borrowListOut.getUsername());            System.out.println("借阅条数:" + borrowListOut.getBorrows().size());            for (Borrow borrow: borrowListOut.getBorrows()) {                System.out.println(borrow);            }            System.out.println("-------------------------");        }    }}======================================================================================

    4)在mybatis实际运行日志中确实查到mysql中的三条数据:

    在这里插入图片描述

    但是最后的测试数据却只有一条(即borrowListOut.getBorrows().size()==1):

    在这里插入图片描述


2、问题原因与解决


  • 问题原因:两张表中的id字段是一样的,这样会导致sql结果集的覆盖:即user表下的id与borrow表下的id

    在这里插入图片描述

  • 解决办法:1、更改数据库中字段名 2、删去其中一个id标签


3、可能其他原因补充



来源地址:https://blog.csdn.net/qq_45507768/article/details/130057186

您可能感兴趣的文档:

--结束END--

本文标题: MyBatis一对多映射失败问题原因与解决方法(一对多查询返回结果只有一条)

本文链接: https://www.lsjlt.com/news/410302.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作