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

Posted Bug解决者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了问题解决nested exception is org.apache.ibatis.exceptions.TooManyResultException:Expected one result相关的知识,希望对你有一定的参考价值。

项目场景

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


问题描述

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

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

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

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;

以上是关于问题解决nested exception is org.apache.ibatis.exceptions.TooManyResultException:Expected one result的主要内容,如果未能解决你的问题,请参考以下文章

随手记录关于Factory method 'eurekaApplicationInfoManager' threw exception; nested exception is j(代

SpringBoot解决Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.N

解决Exception:Could not open Hibernate Session for transaction; nested exception is java.lang.NoClassD

解决报错Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPoint

nested exception is java.lang.IllegalStateException: Method has too many Body parameters问题解决

nested exception is java.lang.IllegalStateException: Method has too many Body parameters问题解决