版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z_demon801/article/details/79657165
***************************
APPLICATION FAILED TO START
***************************
Description:
Field mapper in com.demo.service.impl.UserServiceImpl required a bean of type 'com.demo.mapper.UserMapper' that could not be found.
Action:
APPLICATION FAILED TO START
***************************
Description:
Field mapper in com.demo.service.impl.UserServiceImpl required a bean of type 'com.demo.mapper.UserMapper' that could not be found.
Action:
Consider defining a bean of type 'com.demo.mapper.UserMapper' in your configuration.
SpringBoot启动失败,Bean配置失败
原因:在mybatis-spring-boot-autoconfigure的jar包中有一个类 MybatisAutoConfiguration,在这个类中的registerBeanDefinitions方法告诉了我们
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
logger.debug("Searching for mappers annotated with @Mapper");
ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
try {
if (this.resourceLoader != null) {
scanner.setResourceLoader(this.resourceLoader);
}
List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
if (logger.isDebugEnabled()) {
for (String pkg : packages) {
logger.debug("Using auto-configuration base package '{}'", pkg);
}
}
scanner.setAnnotationClass(Mapper.class);
scanner.registerFilters();
scanner.doScan(StringUtils.toStringArray(packages));
} catch (IllegalStateException ex) {
logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.", ex);
}
}
解决方案
1、mapper(Dao层)
添加 @Mapper
2、在启动类
@MapperScan(value = "com.merveille.dao")