版本信息:java
Spring Boot 2.1.3.RELEASEsql
錯誤信息:bash
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.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean of type '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.
複製代碼
Mapper.java 和 Mapper.xml 放在不一樣的 jar 包工程裏,默認狀況下 mybatis 只讀取同一個工程裏的 Mapper Bean,即使加上了 @Mapper 註釋也不行。mybatis
在 DataSourceConfig 裏增長一個 bean 來處理app
/**
* Mapper接口所在包名,Spring會自動查找其下的Mapper
*
* @return mapperScannerConfigurer
*/
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("**.mapper");
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
return mapperScannerConfigurer;
}
複製代碼
錯誤信息:ide
默認狀況下,Mapper.xml 文件是放在 src/main/resources 目錄下的,但因爲用了 mybatis generator 的緣由,把 xml 放在了 src/main/java 目錄下,方便生成和管理。ui
在項目的 pom.xml 增長如下配置spa
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<!-- resources配置解決mybatis 的mapperXml配置在java路徑不被掃描的問題 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
複製代碼
解決辦法就是在classpath後加一個*就解決了,如debug
mybatis:
mapper-locations: classpath*:mapper/*.xml
複製代碼