后台数据查询接口的书写出错
报错内容:
[ 系统异常 ]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);
本文介绍了一种常见的MyBatis使用过程中遇到的问题——TooManyResultsException,并提供了解决方案。该异常通常发生在预期返回一条记录的情况下却返回了多条记录。通过调整查询逻辑和返回类型,可以有效避免此类异常。
5150

被折叠的 条评论
为什么被折叠?



