springboot集成mybatis时报错如下:
*************************** APPLICATION FAILED TO START *************************** Description: Field deviceMapper in com.esagent.es.EsDataInit required a bean of type 'com.esagent.mapper.EmDeviceMapper' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.esagent.mapper.EmDeviceMapper' in your configuration.
网上提到的解决办法大概有以下几种
1. Application启动类包位置不对
2. 启动类未配置MapperScan
3. Mapper未配置注解@Mapper
4. yml文件未配置mybatis相关
mybatis: mapper-locations: classpath:com/esagent/mapper/*.xml type-aliases-package: com.esagent.bean
实际上造成这个问题的原因是很多的。我碰到的问题原因是pom依赖包之间版本的问题
原配置如下
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.3</version> </dependency>
mybatis-spring-boot-starter采用了1.3.2版本,而mybatis-spring采用了1.2.3版本。将mybatis-spring升级为1.3.2版本后问题解决