解决:Field xxMapper in xx.service.impl.xxServiceImpl required a bean of type 'xx.mapper.xxMapper'
1、启动 SpringBoot项目报错,使用的是Springboot、Spring、Mybatis连接Mysql数据库,启动SpringBoot项目报错,错误如下所示:
1 _____ .__/\ .__ 2 _/ ____\____ |__)/__ __|__|____ ____ 3 \ __\/ __ \| |\ \/ / \__ \ / \ 4 | | \ ___/| | > <| |/ __ \| | \ 5 |__| \___ >__|/__/\_ \__(____ /___| / 6 \/ \/ \/ \/ 7 8 9 2020-02-28 13:50:26.771 INFO 8720 --- [ main] c.f.n.NationalpolicyApplication : Starting NationalpolicyApplication on DESKTOP-T450s with PID 8720 (D:\biehl\idea\workspace\nationalpolicy2\target\classes started by Aiyufei in D:\biehl\idea\workspace\nationalpolicy2) 10 2020-02-28 13:50:26.777 INFO 8720 --- [ main] c.f.n.NationalpolicyApplication : No active profile set, falling back to default profiles: default 11 2020-02-28 13:50:28.302 INFO 8720 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode. 12 2020-02-28 13:50:28.336 INFO 8720 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 28ms. Found 0 JDBC repository interfaces. 13 2020-02-28 13:50:28.489 WARN 8720 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.feixian.mapper]' package. Please check your configuration. 14 2020-02-28 13:50:29.271 INFO 8720 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 15 2020-02-28 13:50:29.292 INFO 8720 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 16 2020-02-28 13:50:29.294 INFO 8720 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31] 17 2020-02-28 13:50:29.449 INFO 8720 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 18 2020-02-28 13:50:29.449 INFO 8720 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2428 ms 19 2020-02-28 13:50:29.521 WARN 8720 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'repoPolicyContentController': Unsatisfied dependency expressed through field 'repoPolicyContentService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'repoPolicyContentServiceImpl': Unsatisfied dependency expressed through field 'repoPolicyContentMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.feixian.nationalpolicy.mapper.RepoPolicyContentMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 20 2020-02-28 13:50:29.525 INFO 8720 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 21 2020-02-28 13:50:29.546 INFO 8720 --- [ main] ConditionEvaluationReportLoggingListener : 22 23 Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 24 2020-02-28 13:50:29.681 ERROR 8720 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : 25 26 *************************** 27 APPLICATION FAILED TO START 28 *************************** 29 30 Description: 31 32 Field repoPolicyContentMapper in com.feixian.nationalpolicy.service.impl.RepoPolicyContentServiceImpl required a bean of type 'com.feixian.nationalpolicy.mapper.RepoPolicyContentMapper' that could not be found. 33 34 The injection point has the following annotations: 35 - @org.springframework.beans.factory.annotation.Autowired(required=true) 36 37 38 Action: 39 40 Consider defining a bean of type 'com.feixian.nationalpolicy.mapper.RepoPolicyContentMapper' in your configuration. 41 42 43 Process finished with exit code 1
2. 报错原因,是因为项目未扫描到 mapper 包。
3. 解决该报错的方法,如下所示:
在项目启动类上加注解 @MapperScan("Mapper文件所在的包"),@MapperScan 是扫描mapper类的注解,就不用在每个mapper类上加@MapperScan了。
4、重启工程成功运行。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2017-02-28 分页查询的实现原理