Mybatis报错—— Mapped Statements collection does not contain value for com.mapper.DepartmentMapper

9 篇文章 0 订阅
9 篇文章 0 订阅

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.

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值