在mapper.xml文件中,将resultType和resultMap弄混淆

Caused by: java.lang.IllegalArgumentException: Result Maps collection does not contain
本人总结了两点原因:

  1. 在mapper.xml文件中,将resultType和resultMap弄混淆。

这个原因一般报错的第一行后面都会有这样一句话 org.apache.ibatis.builder.IncompleteElementException: Could not find result map com.xxx.xxx with root cause

注意:使用resultMap时,必须要有标签指明id和返回类型以及表字段的对应关系。

如果是直接返回一个用户定义的实体类型,则要使用resultType,弄混淆就会报错。

  1. 在mapper.xml头部namespace引入错误。
这个namespace引入的mapper文件要和你的mapper.xml文件对应。

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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值