Mybatis报错—— Mapped Statements collection does not contain value for com.mapper.DepartmentMapper
来源:互联网 发布:hive 数据倾斜 编辑:程序博客网 时间:2024/05/19 14:38
Mybatis报错—— Mapped Statements collection does not contain value for com.mapper.DepartmentMapper.getDeptById
前言:
如果用getMapper()或其他方法,直接获取Mapper接口时,对应的映射文件没有在全局配置文件中注册,报错比较简单(EmployeeMapperPlus is not known to the MapperRegistry),今天关联查询时报了一个异常,也是映射文件没在全局配置文件中注册,报错不太一样,关键是这段:
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.mapper.DepartmentMapper.getDeptById
报错如下:
org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.mapper.DepartmentMapper.getDeptById### The error may exist in com/mapper/EmployeeMapperPlus.xml### The error may involve com.mapper.EmployeeMapperPlus.getEmpByIdStep### The error occurred while handling results### SQL: select * from tbl_employee where id = ?### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.mapper.DepartmentMapper.getDeptById at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148) ... 29 more
代码如下:
<!-- 使用association进行分步查询 1.先按照员工id查询员工信息 2.根据查询员工信息中的d_id值去部门表查出部门信息 3.部门设置到员工中:--> <resultMap type="com.bean.Employee" id="MyEmpByStep"> <id column="id" property="id" /> <result column="last_name" property="lastName" /> <result column="gender" property="gender"/> <result column="email" property="email"/> <!-- association定义关联的封装规则 select:表明当前属性是调用select指定的方法查出的结果 全类名+方法名; column:指定将哪一列的值传给这个方法 流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性 --> <association property="dept" select="com.mapper.DepartmentMapper.getDeptById" column="d_id"> </association> </resultMap>
错误原因:
在使用<association>
标签进行关联分步查询时,select属性中DepartmentMapper接口所对应的映射文件,没有在全局配置文件中配置,如下:
<association property="dept" select="com.mapper.DepartmentMapper.getDeptById" column="d_id"></association>
所以报错 Mapped Statements collection does not contain value for com.mapper.DepartmentMapper.getDeptById,就是说映射声明集合中不包含com.mapper.DepartmentMapper.getDeptById(select的值,集它指向的方法)
问题解决:
只要在全局配置文件中配置对应的映射文件即可,代码如下:
<mappers> <mapper resource="com/mapper/DepartmentMapper.xml" /></mappers>
补充:
如果用getMapper()或其他方法,直接获取Mapper接口时,对应的映射文件没有注册,报错比较简单(EmployeeMapperPlus is not known to the MapperRegistry),代码和报错信息如下:
代码:
EmployeeMapperPlus Mapper =
openSession.getMapper(EmployeeMapperPlus.class);报错信息:
org.apache.ibatis.binding.BindingException: Type interface com.mapper.EmployeeMapperPlus is not known to the
MapperRegistry.