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报错内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论