Spring Boot 启动报错:Field testMapper in *** required a bean of type ‘***‘ that could not be found.

Spring Boot 工程构建时报错:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-03-23 10:10:52.451 ERROR 55881 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field testMapper in com.example.demo.server.impl.TestServerImpl required a bean of type 'com.example.demo.dao.mapper.TestMapper' 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.dao.mapper.TestMapper' in your configuration.

原因:

没有声明 mapper 接口文件扫描路径导致,只需要在入口文件声明即可:

@SpringBootApplication(scanBasePackages = {
    
    "com.example.demo"})
// 声明 mapper 接口文件扫描路径导致
@MapperScan(value = "com.example.demo.dao.mapper")
public class DemoApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(DemoApplication.class, args);
    }

}

补充

同样实体类包路径和xml文件路径也需要指定:

# application.xml
# mybatis config
mybatis:
  # 指定实体类包扫描路径
  typeAliasesPackage: com.example.demo.dao.po
  # 指定xml文件扫描路径
  mapper-locations: classpath*:mapper/**/*.xml

猜你喜欢

转载自blog.csdn.net/yu97271486/article/details/115111111