mapper找不到报错:Field xxxMapper in xxx required a bean of type 'xxxMapper' that could not be found

搭建springboot项目整合ssm框架后,启动报错:

Field xxxMapper in xxx.service.impl.xxxServiceImpl required a bean of type 'xxx.mapper.xxxMapper' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

@Autowire注入Service时找不到对应的mapper,通过百度查询到两种解决办法:

一、在mapper上添加注解@Mapper,可惜并没能解决我的问题

二、在启动类Application上添加注解@MapperScan

添加后发现注解标红,找不到@MapperScan注解,这个注解是mybatis和spring整合包mybatis-spring中提供的注解,而我的pom文件中有依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>

这个依赖中已经包含了mybatis-spring,后来在检查maven denpendencies时发现mybatis-spring-boot-starter的version是unknown,再查看springboot管理的version文件,发现mybatis-spring-boot-starter不在springboot管理的版本里,于是修改mybatis-spring-boot-starter坐标:

<!-- spring-boot整合mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>${mybatis-spring-boot-starter.version}</version>
</dependency>

添加自己的版本管理:

<mybatis-spring-boot-starter.version>2.0.1</mybatis-spring-boot-starter.version>

@MapperScan注解已经可以使用,并且发现这时候启动类添加@MapperScan或者dao层添加@Mapper同样可以解决mapper类找不到的问题。

得出结论,之前启动报错的真正原因是mybatis-spring-boot-starter缺失,由于搭建项目时认为mybatis-spring-boot-starter的版本号是springboot管理,没有添加版本号,因此并没有被依赖进来,故报错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值