Java bookmark #SpringBoot+MyBatis uses @MapperScan to annotate multiple packages

1. Today Bookmark

In the SpringBoot integrated MyBatis project, how to annotate and scan the DAO interface classes under multiple packages at one time?
In other words, how to use @MapperScan to annotate multiple DAO mapping classes in different package paths?
In other words, what are the three setting methods for @MapperScan annotation scanning interface class?

The project technology stack upgrades SpringBoot 2 + MyBatis 3. Talking about the pits we have stepped on, we look at the sword in our dreams.
 

2. Explore flowers in dreams

If you want to add on each DAO interface class @Mappernotes, this is not only cumbersome, but also very complicated. In SpringBoot MyBatis integrated projects, with @MapperScanannotations, you can achieve multi-path multi-packet scanning.

1) Can be used @MapperScan(basePackages = {"", ""}), such as:

@Configuration
@MapperScan(basePackages = {
    
    "com.meiwei.tan.dao.mall", "com.meiwei.ping.dao.crm"})
public class MyBatisConfiguration {
    
    

2) It can also be used @MapperScan(value = {"", ""})with the same effect:

@Configuration
@MapperScan(value = {
    
    "com.meiwei.tan.dao.mall", "com.meiwei.ping.dao.crm"})
public class MyBatisConfiguration {
    
    

3) There is also a by using a @MapperScan(basePackageClasses = {xx1.class, xx2.class})specified class multiple scans, although rarely used in this way:

@Configuration
@MapperScan(basePackageClasses = {
    
    OrderDao.class, UserDao.class})
public class MyBatisConfiguration {
    
    

The above one @MapperScan 注解,三种设置方式can finally achieve the effect of the target being scanned. Look at the source code for a clear view, here is a paragraph:

Insert picture description here

 

3. Bad operation

If the interface class under the configuration package path is still not scanned, please confirm whether the following configuration is OK:
1) Whether org.mybatis.spring.boot depends on

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

 
2) Whether the dependency version of Mybatis is compatible with the version of SpringBoot For
example: the dependency version of org.mybatis.spring.boot is 1.3.2, and springboot uses version 2.0.2. When the @mapper annotation is used in the POJO entity class, it works normally Works, but annotating @mapper and using @mapperScan may cause an error.
 

3) Whether the system configuration or instance is loaded and created.
If you use test cases to run locally, you must first ensure that the system configuration is loaded and initialized normally. Compared with the various environments and system configurations of SpringMVC, SpringBoot's zero-configuration features make some places The configuration is easily overlooked or forgotten. Checklist:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)
public class JUnitTestBase {
    
    

    @BeforeClass
    public static void beforeClass(){
    
    
        System.setProperty("app.id","meiwei");
        System.setProperty("apollo.bootstrap.enabled","true");
        System.setProperty("apollo.bootstrap.namespaces","application,db,dubbo,redis,mq");
    }
}
@EnableAutoConfiguration()
@Configuration()
@ComponentScan(value = {
    
    "com.meiwei.tan.service", "com.meiwei.*.service"})
@EnableTransactionManagement
@EnableAspectJAutoProxy
public class App {
    
    
}
public class OrderServiceTest extends JUnitTestBase {
    
    

    @Autowired
    IOrderService iOrderService;

    @Test
    public void testOrderDetail() {
    
    
        OrderDetailQuery orderDetailQuery = new OrderDetailQuery();
        orderDetailQuery.setBizChannel(0);
        OrderDO orderDO = iOrderService.orderDetail(orderDetailQuery);
        System.out.println(JSON.toJSONString(orderDO));
    }
}

Guess you like

Origin blog.csdn.net/itanping/article/details/108648668