SpringBoot项目启动报错:Field Mapper in ServiceImpl required a bean of type ‘Mapper‘ that could not be found
1.报错:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-06-15 15:39:21.068 ERROR 3720 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userMapper in com.example.demo.service.impl.UserServiceImpl required a bean of type 'com.example.demo.mapper.UserMapper' 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.example.demo.mapper.UserMapper' in your configuration.
Process finished with exit code 1
2.原因:没有找到mapper包
3.解决:启动类加入注解@MapperScan ,路径是mapper所在的包
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.demo.mapper")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4.重新启动,问题解决
2021-06-15 16:46:04.370 INFO 9908 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-06-15 16:46:05.090 INFO 9908 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-06-15 16:46:05.096 INFO 9908 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-06-15 16:46:05.096 INFO 9908 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.46]
2021-06-15 16:46:05.144 INFO 9908 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-06-15 16:46:05.144 INFO 9908 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 735 ms
2021-06-15 16:46:05.385 INFO 9908 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index
2021-06-15 16:46:05.507 INFO 9908 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-06-15 16:46:05.515 INFO 9908 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.443 seconds (JVM running for 1.942)
2021-06-15 16:46:05.516 INFO 9908 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state LivenessState changed to CORRECT
2021-06-15 16:46:05.517 INFO 9908 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC