Version Information:
Spring Boot 2.1.3. RELEASE
Problem 1: Mapper Bean cannot be found
Error message:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug'Enabled. 01:56:25. 921 logback [main] ERROR O.S.B.D.L oggingFailureAnalysisReporter - * * * * * * * * * * * * * * * * * * * * * * * * * * * APPLICATION FAILED TO START *************************** Description: A component required a bean oftype 'com.example.boss.admin.dao.mapper.SeckillMapper' that could not be found.
Action:
Consider defining a bean of type 'com.example.boss.admin.dao.mapper.SeckillMapper' in your configuration.
Copy the code
The reason:
Mapper. Java and mapper. XML are stored in different jar projects. By default, Mybatis only reads Mapper beans from the same project, even if the @mapper annotation is added.
Solutions:
Add a bean to the DataSourceConfig for processing
/** * The package name of the Mapper interface, Spring will automatically find Mapper ** @return mapperScannerConfigurer
*/
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("**.mapper");
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
return mapperScannerConfigurer;
}
Copy the code
Mapper.xml cannot be found
Error message:
The reason:
By default, mapper. XML is stored in the SRC /main/resources directory, but due to the mybatis Generator, XML is stored in the SRC /main/ Java directory for easy generation and management.
Solutions:
Add the following configuration to the project’s POM.xml
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering> </resource> <! <resource> <directory> SRC /main/ Java </directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources>Copy the code
How to read mapper. XML files under multiple module projects?
Solutions:
The solution is to add an * to the end of the classpath, as in
mybatis:
mapper-locations: classpath*:mapper/*.xml
Copy the code