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.
mapper(Dao层)
8 //@Component 9 @Repository 10 public interface UserMapper { 11 12 public User gYeMian(User u); 13 14 public int sYeMian(User u); 15 16 }
解决方案一:
mapper(Dao层)
1 7 @Mapper 8 public interface UserMapper { 9 10 public User gYeMian(User u); 11 12 public int sYeMian(User u); 13 14 }
解决方案二:
Application(启动类)
1 package com.demo; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7 @SpringBootApplication 8 @MapperScan(value = "com.demo.mapper") 9 public class App 10 { 11 public static void main(String[] args) throws Exception { 12 SpringApplication.run(App.class, args); 13 } 14 }
原因:在mybatis-spring-boot-autoconfigure的jar包中有一个类 MybatisAutoConfiguration,在这个类中的registerBeanDefinitions方法告诉了我们
1 @Override 2 public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { 3 4 logger.debug("Searching for mappers annotated with @Mapper"); 5 6 ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry); 7 8 try { 9 if (this.resourceLoader != null) { 10 scanner.setResourceLoader(this.resourceLoader); 11 } 12 13 List<String> packages = AutoConfigurationPackages.get(this.beanFactory); 14 if (logger.isDebugEnabled()) { 15 for (String pkg : packages) { 16 logger.debug("Using auto-configuration base package '{}'", pkg); 17 } 18 } 19 20 scanner.setAnnotationClass(Mapper.class); 21 scanner.registerFilters(); 22 scanner.doScan(StringUtils.toStringArray(packages)); 23 } catch (IllegalStateException ex) { 24 logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.", ex); 25 } 26 }