【问题解决】nested exception is org.apache.ibatis.exceptions.TooManyResultException:Expected one result

简介: 【问题解决】nested exception is org.apache.ibatis.exceptions.TooManyResultException:Expected one result

项目场景

今天在调试项目的时候,控制台出现了如下的报错信息,从下面报错信息可以明白大致的意思是:出现了过多的结果返回值,只希望得到一个值,但是现在返回了2个。

\[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kHr8bJ3j-1677378165143)(%E9%A1%B9%E7%9B%AE%E5%9C%BA%E6%99%AF.image/image-20230224212340187.png)\]


问题描述

通过控制台打印的SQL,在数据库里面执行,显然出现两个结果返回值。

select post_id, post_code, post_name, post_sort, status, create_by, create_time, remark from sys_post where post_code = 'zongjingli';

\[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1dlQjKtn-1677378165144)(%E9%A1%B9%E7%9B%AE%E5%9C%BA%E6%99%AF.image/image-20230224203155879.png)\]

再来看看我们定义的接口是怎样的?

SysPostMapper.java

SysPost selectPostByPostCode(String postCode);

SysPostMapper.xml

<select id="selectPostByPostCode" parameterType="String" resultMap="SysPostResult">
         <include refid="selectPostVo"/>
      where post_code = #{postCode}
</select>

原因分析

通过SQL查询出来的结果用于接收的是单个对象,但sql查询出来的结果却是一个列表(多个) 导致无法接收所有的查询结果值。


解决方案

下面给予了两种解决方案:

  • 把Mybatis对应的mapper接口的返回值修改为List
List<SysPost> selectPostByPostCode(String postCode);
  • 根据业务场景对查询SQL语句做返回数量的限制
select post_id, post_code, post_name, post_sort, status, create_by, create_time, remark from 
sys_post where post_code = 'zongjingli' limit 1;
相关文章
|
29天前
|
Java Spring
上传文件出现 aximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.
上传文件出现 aximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.
11 0
|
6月前
|
Java Apache Maven
【异常解决】Handler dispatch failed;nested exception is java.lang.NoClassDefFoundError: org/apache/common
【异常解决】Handler dispatch failed;nested exception is java.lang.NoClassDefFoundError: org/apache/common
1136 0
|
6月前
|
Java 数据库连接 mybatis
【Mybatis异常】nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter
【Mybatis异常】nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter
178 0
|
9月前
|
SQL Java 数据库连接
nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘‘ not found. Ava
用mybatis写注解SQL的执行报错,这个报错有很多原因就不说了,说一下我的问题 同一个mapper中方法有重名的,虽然编译没报错,相当于重载了,但是执行的时候就报错了 方法写的太多了都没注意
|
10月前
org.apache.ibatis.builder.IncompleteElementException: Could not find result map com.dao.IndexDao.Use
org.apache.ibatis.builder.IncompleteElementException: Could not find result map com.dao.IndexDao.Use
61 0
|
10月前
|
XML Java 数据库连接
nested exception is org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps
nested exception is org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps
118 0
|
Java 应用服务中间件 Apache
nested exception is java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim
nested exception is java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim
253 0
|
2月前
|
消息中间件 Kafka Apache
Apache Flink 是一个开源的分布式流处理框架
Apache Flink 是一个开源的分布式流处理框架
482 5
|
1月前
|
消息中间件 API Apache
官宣|阿里巴巴捐赠的 Flink CDC 项目正式加入 Apache 基金会
本文整理自阿里云开源大数据平台徐榜江 (雪尽),关于阿里巴巴捐赠的 Flink CDC 项目正式加入 Apache 基金会。
1416 1
官宣|阿里巴巴捐赠的 Flink CDC 项目正式加入 Apache 基金会
|
1月前
|
SQL Java API
官宣|Apache Flink 1.19 发布公告
Apache Flink PMC(项目管理委员)很高兴地宣布发布 Apache Flink 1.19.0。
1355 1
官宣|Apache Flink 1.19 发布公告

推荐镜像

更多