Home  >  Article  >  Java  >  How to solve the springBoot mybatis package scanning problem

How to solve the springBoot mybatis package scanning problem

WBOY
WBOYforward
2023-05-16 19:19:041490browse

springBoot mybatis package scan

@MapperScan(basePackages = {"com.zscat.*.dao","com.zscat.*.*.dao"})
@EnableTransactionManagement(proxyTargetClass = true)
@SpringBootApplication
@MapperScan(basePackages = {"com.zscat.*.dao","com.zscat.*.*.dao"})
public class   ShopServiceApplication {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(ShopServiceApplication.class);
        application.run(args);
    }
}

springBoot cannot scan the mybatis interface package

You only need to add annotations to the spring boot startup class and specify the interface file package path in the jar package.

@MapperScan(basePackages = "com.xx.**.dao")

If you use @Controller and @EnableAutoConfiguration annotations, you should also add another annotation: @ComponentScan.

@Controller and @EnableAutoConfiguration do not have the function of scanning annotations, and @ComponentScan is

springboot is specially used to scan @Component, @Service, @Repository, @Controller and other annotations.

Summary:

Two annotation configuration methods for using springboot to start class configuration scanning:

1, @Controller

   @EnableAutoConfiguration
   @ComponentScan

2, @SpringBootApplication

@SpringBootApplication annotation is equivalent to @Configuration, @EnableAutoConfiguration and @ComponentScan

In addition application.java ( Startup class) should also be placed in the root directory according to official recommendations, so that the Service and DAO can be scanned. Otherwise, it will cause the problem of not being able to scan the annotations.

--- Update date: 2018-10-14 ---

I recently used the latest springboot 2.0.5.RELEASE version. There is a new scanning annotation. The new version of springboot The application can be placed at any location, just add the

@ComponentScan(basePackages = {"com.oskyhang", "com.frames"})

annotation. The annotation specifies the package to be scanned, and then it can be scanned, which is more flexible and convenient.

The above is the detailed content of How to solve the springBoot mybatis package scanning problem. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete