1、启动 SpringBoot项目报错,使用的是Springboot、Spring、Mybatis连接Mysql数据库,启动SpringBoot项目报错,错误如下所示:
_____ .__/\ .__
_/ ____\____ |__)/__ __|__|____ ____
\ __\/ __ \| |\ \/ / \__ \ / \
| | \ ___/| | > <| |/ __ \| | \
|__| \___ >__|/__/\_ \__(____ /___| /
\/ \/ \/ \/ -- ::26.771 INFO --- [ main] c.f.n.NationalpolicyApplication : Starting NationalpolicyApplication on DESKTOP-T450s with PID (D:\biehl\idea\workspace\nationalpolicy2\target\classes started by Aiyufei in D:\biehl\idea\workspace\nationalpolicy2)
-- ::26.777 INFO --- [ main] c.f.n.NationalpolicyApplication : No active profile set, falling back to default profiles: default
-- ::28.302 INFO --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
-- ::28.336 INFO --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 28ms. Found JDBC repository interfaces.
-- ::28.489 WARN --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.feixian.mapper]' package. Please check your configuration.
-- ::29.271 INFO --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): (http)
-- ::29.292 INFO --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
-- ::29.294 INFO --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.]
-- ::29.449 INFO --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
-- ::29.449 INFO --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in ms
-- ::29.521 WARN --- [ 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 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
-- ::29.525 INFO --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
-- ::29.546 INFO --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
-- ::29.681 ERROR --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : ***************************
APPLICATION FAILED TO START
*************************** Description: Field repoPolicyContentMapper in com.feixian.nationalpolicy.service.impl.RepoPolicyContentServiceImpl required a bean of type 'com.feixian.nationalpolicy.mapper.RepoPolicyContentMapper' that could not be found. The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.feixian.nationalpolicy.mapper.RepoPolicyContentMapper' in your configuration. Process finished with exit code
2. 报错原因,是因为项目未扫描到 mapper 包。
3. 解决该报错的方法,如下所示:
在项目启动类上加注解 @MapperScan("Mapper文件所在的包"),@MapperScan 是扫描mapper类的注解,就不用在每个mapper类上加@MapperScan了。
4、重启工程成功运行。