SpringBoot 自动注入失败:Consider defining a bean of type 'xxx.UserMapper' in your configuration

spring boot 自动注入来有可能会出现下面的错误。

***************************
APPLICATION FAILED TO START
***************************

Description:
Field userMapper in cn.lttc.springbootmybatisdemo.controller.HelloController required a bean of type 'cn.lttc.springbootmybatisdemo.mapper.UserMapper' 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 'cn.lttc.springbootmybatisdemo.mapper.UserMapper' in your configuration.
Process finished with exit code 0

基础知识

正常情况下 Spring 的 @Component、@Service、@Controller、@ Repository 注解的类会被 Spring 自动扫描识别注册到 spring 容器中,并且默认只扫描与启动类在同一个包下以及其子包下的类。

解决方案

方法一:启动类上增加@MapperScan注解指定要扫描的包名

2019041211534.png

方法二:Mapper 类上增加@Mapper注解

2019041211539.png

最后修改:2019 年 04 月 12 日 11 : 06 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论