org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null)

问题描述
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 2

问题背景

目标:通过Collection处理一对多的映射关系——使用部门id,查询部门的相关信息,包括部门的员工列表,返回的部门信息映射到Dept实体。

疑惑:通过1个id只能查到1个部门,尽管因为有多个员工,在数据库中显示多个记录,但是也不会造成“TooManyResultsException”这样的错误。

Dept类代码

public class Dept {
    private Integer deptId;
    private String deptName;
    private List<Emp> emps;
	// 省略构造方法、get/set方法、toString方法
}

DeptMapper接口代码

public interface DeptMapper {
    /**
     * 查询部门以及部门中的员工信息
     * @param deptId
     * @return
     */
    Dept getDeptAndEmpByEmpId(@Param("deptId") Integer deptId);
}

DeptMapper.xml文件代码

<mapper namespace="com.atguigu.mybatis.mapper.DeptMapper">
    <resultMap id="deptAndEmpResultMap" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName" ></result>
        <!-- ofType设置集合类型的属性中存储的数据的类型 -->
        <collection property="emps" ofType="Emp">
            <id column="empId" property="emp_id"></id>
            <result column="empName" property="emp_name"></result>
            <result column="age" property="age"></result>
            <result column="gender" property="gender"></result>
        </collection>
    </resultMap>

    <!-- Dept getDeptAndEmpByEmpId(@Param("deptId") Integer deptId); -->
    <select id="getDeptAndEmpByEmpId" resultMap="deptAndEmpResultMap">
        SELECT *
        FROM t_dept
        LEFT JOIN t_emp
        ON t_dept.dept_id = t_emp.dept_id
        WHERE t_dept.dept_id = #{deptId}
    </select>
</mapper>

导致原因
DeptMapper.xml映射文件中的resultMap的id、result标签的property、column属性写反了。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
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 ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DANIEL'S BLOG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值