Spring Boot:Consider defining a bean of type '*.*.*' in your configuration解决方案

阅读更多

    解决方案一:Mapper注解 (Dao层)

 1 package com.demo.mapper;
 2 
 3 import org.apache.ibatis.annotations.Mapper;
 4 
 5 import com.demo.domain.User;
 6 
 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 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     }

 

你可能感兴趣的:(Spring Boot:Consider defining a bean of type '*.*.*' in your configuration解决方案)