此文章发布于67
个月前,部分信息可能已经过时
,请自行斟酌确认。
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
注解指定要扫描的包名
方法二:Mapper 类上增加@Mapper
注解