Caused by: java.lang.IllegalArgumentException: Result Maps collection does not contain
本人总结了两点原因:
- 在mapper.xml文件中,将resultType和resultMap弄混淆。
这个原因一般报错的第一行后面都会有这样一句话 org.apache.ibatis.builder.IncompleteElementException: Could not find result map com.xxx.xxx with root cause
注意:使用resultMap时,必须要有标签指明id和返回类型以及表字段的对应关系。
如果是直接返回一个用户定义的实体类型,则要使用resultType,弄混淆就会报错。
- 在mapper.xml头部namespace引入错误。
public interface DepartmentMapperResultMap {
//根据id查询部门信息
public Department selectDeptById(Integer id);
}
<!-- public Department selectDeptById(Integer id); -->
<select id="selectDeptById" resultType="com.atguigu.mybatis.beans.Department">
select id, dept_name from tbl_dept where id = #{id}
</select>
<!-- public Employee selectEmployeeAndDeptById(Integer id); -->
<select id="selectEmployeeAndDeptById" resultMap="selectEmployeeDept">
select e.id eid, e.last_name, e.gender, e.email, d.id did, d.dept_name
from tbl_employee e , tbl_dept d
where e.d_id = d.id
and e.id = #{id}
</select>
<!-- 自定义的映射规则
type : 结果集的封装类型
<id/> : 用于主键列的映射
column: 指定结果集中的列名
property: 指定对象的属性名
<result/>: 用于普通列的映射
-->
<resultMap type="com.atguigu.mybatis.beans.Employee" id="selectEmployeeDept">
<id column="eid" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!-- 级联属性
<result column="did" property="dept.id"/>
<result column="dept_name" property="dept.deptName"/>
-->
<!--
association: 关联,联合. 完成 一对一的映射关系.
property: 指定关联属性的名字.
javaType: 指定关联属性的类型.
-->
<association property="dept" javaType="com.atguigu.mybatis.beans.Department">
<id column="did" property="id"/>
<result column="dept_name" property="deptName"/>
</association>
</resultMap>
<!-- public Employee selectEmp loyeeAndDeptByIdStep(Integer id); -->
<!-- association分步、分段查询
需求: 查询员工并且查询员工所在的部门信息
分步:
1. 先根据员工的id查询到员工的信息(d_id)
2. 再根据d_id 查询部门的信息
-->
<select id="selectEmployeeAndDeptByIdStep" resultMap="selectEmployeeAndDeptStep">
select id, last_name, email, gender, d_id from tbl_employee where id = #{id}
</select>
<resultMap type="com.atguigu.mybatis.beans.Employee" id="selectEmployeeAndDeptStep">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<!-- association 分布查询 -->
<association property="dept" select="com.atguigu.mybatis.mapper.DepartmentMapperResultMap.selectDeptById"
column="d_id" >
</association>
</resultMap>