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、重启工程成功运行。
解决:Field xxMapper in xx.service.impl.xxServiceImpl required a bean of type 'xx.mapper.xxMapper'