Scanning of mapper when Spring boot and mybatis are combined

There have been many introductions on the Internet about the combination of Spring boot and mybatis.

Basically, write a configuration file, specify the bean definitions of SqlSessionFactory and TransactionManager, and specify the scan path of MapperScan.

 

In fact, this configuration file is not written, there is no problem. For TransactionManager, DataSourceTransactionManagerAutoConfiguration has been defined for you.

As for the SqlSessionFactory, MybatisAutoConfiguration also helps. All you have to do is configure it in application.properties, and it will automatically map the configuration to the class MybatisProperties.

 

For MapperScan, if it is not defined, there will also be a default AutoConfiguredMapperScannerRegistrar that can be used. It internally uses a ClassPathMapperScanner class to scan in the application path, but its method of determining whether it is a mapper is a bit too rough.

Source code ClassPathMapperScanner file

  @Override
  protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
    return beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().isIndependent();
  }

 

As long as it is an interface and not an inner class, it can be treated as a mapper, and then created with MapperFactoryBean.

So to be clear, it is better to specify the following with MapperScan.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327026655&siteId=291194637