当前位置: 代码网 > it编程>编程语言>Java > MyBatis嵌套查询collection报错:org.apache.ibatis.exceptions.TooManyResultsException

MyBatis嵌套查询collection报错:org.apache.ibatis.exceptions.TooManyResultsException

2024年09月27日 Java 我要评论
1、目标本文的主要目标是研究resultmap的collection更新字段但是resultmap不更新字段报错的原因和源码分析2、resultmap的collection更新字段,resultmap

1、目标

本文的主要目标是研究resultmap的collection更新字段但是resultmap不更新字段报错的原因和源码分析

2、resultmap的collection更新字段,resultmap不更新字段会报错

<?xml version="1.0" encoding="utf-8"?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.apache.mybatisdemo.classmapper">
    <resultmap id="classmap" type="org.apache.mybatisdemo.class">
        <!--<id property="id" column="classid"/>-->
        <!--<result property="name" column="classname"/>-->
        <!--<result property="createtime" column="create_time"/>-->
        <collection property="stulist" javatype="java.util.list" oftype="org.apache.mybatisdemo.stu">
            <result property="id" column="stuid"/>
            <result property="name" column="stuname"/>
            <result property="age" column="age"/>
        </collection>
    </resultmap>

    <select id="getclass" resultmap="classmap">
        select c.id classid, c.name classname, c.create_time, s.id stuid, s.name stuname, s.age
        from `class` c inner join `stu` s on c.id = s.class_id
        where c.`name` = #{classname}
    </select>
</mapper>

如果不更新resultmap是classmap的字段只更新resultmap的collection字段会报错

在这里插入图片描述

报错信息是不能返回多个记录,因为调用了selectone方法只返回1个记录,那为什么会返回多个记录呢,因为封装成多个class班级对象了

源码分析:

在这里插入图片描述

查询数据库得到多个记录后会调用handlerowvaluesfornestedresultmap方法处理嵌套属性

在这里插入图片描述

会循环遍历查询数据库的每个记录,并封装成class班级对象

在这里插入图片描述

计算combinedkey的时候会判断collection的更新字段至少为1并且父节点resultmap的更新字段至少为1才会更新combinedkey,否则更新combinedkey是null_cache_key

这里由于resultmap(classmap)的更新字段为0,因此combinedkey是null_cache_key

在这里插入图片描述

调用getrowvalue方法查询数据库的记录

在这里插入图片描述

会调用objectfactory对象的create方法实例化一个class班级对象

在这里插入图片描述

这里判断combinedkey等于null_cache_key,因此不会将这个class班级对象放到nestedresultobjects这个map中

在这里插入图片描述

while循环查询数据库的第二条记录的时候,从nestedresultobjects这个map中没有找到class班级对象就会创建一个新的class班级对象,这样会返回多个class班级对象

在这里插入图片描述

list集合中添加嵌套王五2stu对象的class班级对象,这样的话会返回3个class班级对象

在这里插入图片描述

最终查询数据库返回3个class班级对象

在这里插入图片描述

调用selectone方法查询数据库记录是3个会抛出异常:期望1个返回值结果返回多个

3、resultmap的collection和resultmap都更新字段不会报错

<?xml version="1.0" encoding="utf-8"?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.apache.mybatisdemo.classmapper">
    <resultmap id="classmap" type="org.apache.mybatisdemo.class">
        <id property="id" column="classid"/>
        <result property="name" column="classname"/>
        <result property="createtime" column="create_time"/>
        <collection property="stulist" javatype="java.util.list" oftype="org.apache.mybatisdemo.stu">
            <result property="id" column="stuid"/>
            <result property="name" column="stuname"/>
            <result property="age" column="age"/>
        </collection>
    </resultmap>

    <select id="getclass" resultmap="classmap">
        select c.id classid, c.name classname, c.create_time, s.id stuid, s.name stuname, s.age
        from `class` c inner join `stu` s on c.id = s.class_id
        where c.`name` = #{classname}
    </select>
</mapper>

resultmap更新字段大于1,并且resultmap的collection的更新字段也大于1

在这里插入图片描述

最后输出结果正确,是一个class班级对象,同时封装了三个stu对象

源码分析:

public cachekey clone() throws clonenotsupportedexception {
    cachekey clonedcachekey = (cachekey) super.clone();
    clonedcachekey.updatelist = new arraylist<>(updatelist);
    return clonedcachekey;
  }

执行cachekey的clone方法可以深拷贝cachekey对象,这里cachekey重写了clone方法,因为cachekey有一个list集合的属性updatelist,需要手动深拷贝复制list集合属性

在这里插入图片描述

生成combinedkey的时候会判断resultmap的collection的更新字段至少有一个,并且resultmap的更新字段至少有一个,才会更新combinedkey,否则combinedkey设置成默认key即null_cache_key

这里combinedkey=-1902729450:-2918070140:org.apache.mybatisdemo.classmapper.mapper_resultmap[classmap]_collection[stulist]:stuid:4:stuname:张三2:age:15:680594160:-729303444:org.apache.mybatisdemo.classmapper.classmap:classid:2

它由两部分组成,第一部分是collection的key和value,第二部分是classmap这个class班级对象

在这里插入图片描述

如果combinedkey不为默认key即null_cache_key,才会将查询数据库的记录缓存到nestedresultobjects这个map中

在这里插入图片描述

查询数据库的第二条记录的时候才会从nestedresultobjects这个map中获取缓存的class班级对象,同时这个class班级对象还嵌套了第一条记录即张三2stu对象

在这里插入图片描述

查询的数据库记录是一个记录,它是class班级对象,它包含了stulist属性,它是一个list集合类型,它包括3个stu对象

4、总结

在这里插入图片描述

总结一句话:resultmap的collection更新字段至少为1个,并且resultmap的更新字段至少为1个,才会返回一个嵌套了多个stu对象的class班级对象,否则会返回多个class班级对象

到此这篇关于mybatis嵌套查询collection报错:org.apache.ibatis.exceptions.toomanyresultsexception的文章就介绍到这了,更多相关mybatis嵌套查询collection报错内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com