java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > MyBatis嵌套查询collection报错

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

作者:永不服输的coder

本文主要介绍了MyBatis嵌套查询collection报错:org.apache.ibatis.exceptions.TooManyResultsException,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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报错内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文