org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.example.demo.dao.impl.DepartmentDaoImpl.getById
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.example.demo.dao.impl.DepartmentDaoImpl.getById

产生这种错误的原因一般有二点:

1.是mapper.xml文件的namespace没有与实现接口相对应。

在这里插入图片描述

2.在接口的实现方法中没有正确填写对应xml文件的namespace。

在这里插入图片描述

3.还有一个原因是 spring没有扫描到mapper.xml。这一般与mybatis的 mapper-locations 配置相关。

在这里插入图片描述

(1)如果pom.xml使用的是: mybatis-spring-boot-starter 则yml文件中应该配置 mybatis:mapper-locations: classpath:mapper/*.xml
<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>2.2.1</version>
 </dependency>
(2)如果如果pom.xml使用的是:mybatis-plus-boot-starter 则yml中应该配置 mybatis-plus:mapper-locations: classpath:mapper/*.xml
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.3.1</version>
</dependency>
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐