版本信息: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
复制代码