[ 系统异常 ]nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one resul

本文介绍了一种常见的MyBatis使用过程中遇到的问题——TooManyResultsException,并提供了解决方案。该异常通常发生在预期返回一条记录的情况下却返回了多条记录。通过调整查询逻辑和返回类型,可以有效避免此类异常。

后台数据查询接口的书写出错

报错内容:

[ 系统异常 ]nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 2

查询语句查出多条数据,但是返回的类型只能是一条

解决方案(改后代码):


/**ReportServiceImpl.java*/
@Override
public List<Report> findReportById(Integer id) {

    List<Report> report = reportMapper.findReportById(id);

    return report;
}
/**ReportController.java*/
@GetMapping("find_report_by_id")
public JsonData findReportById(Integer id){

    List<Report> report = reportService.findReportById(id);

    return JsonData.buildSuccess(report);

}
/**ReportMapper.java*/
List<Report> findReportById(@Param("id") Integer id);
/**ReportService*/
List<Report> findReportById(@Param("id") Integer id);

 

nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 2 是因为在执行sql语句查询时,返回了多条结果,但代码中只期望返回一条结果。这导致了问题的出现。解决这个问题有两种方法可以尝试。 方法一是在Dao层找到对应方法的Sql语句,将该语句复制到数据库中执行。根据注释中所需的字段,传入相应的参数运行Sql语句,查看查询到的数据。如果发现有多余的数据,可以删除这些多余的字段。然后重新发送请求,问题就可以解决了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [ nested exception is org.apache.ibatis.exceptions.TooManyResultsException:Expected one result](https://blog.csdn.net/m0_61682705/article/details/125029333)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result](https://blog.csdn.net/gonnabehappy/article/details/124348041)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值