小记:idea中springboot无法自动装配Could not autowire. No beans of 'UserMapper' type found. more...

问题如图所示

在这里插入图片描述
如果你遇到的情况也是这样,下面的步骤将带你快速出坑

方案一

在自动转配的注解后面添加(required=false)

	
	@Autowired(required=false)
	public UserMapper userMapper;

当我们在使用@Autowired注解的时候,默认required=true,表示注入的时候bean必须存在,否则注入失败。

方案二

在这里插入图片描述

效果如图所示
在这里插入图片描述

方案三

在UserMapper上面添加
@Component(value =“userMapper”)在这里插入图片描述
虽然能解决红线的问题,
@Component注解,表明当需要创建类时,这个被注解的类是一个候选类。相当于类交给Spring管理,重新起个名字叫userMapper,但是并不推荐这样做。

在SpringBoot中集成MyBatis,可以在mapper接口上添加@Mapper注解,将mapper注入到Spring。但是如果每一给mapper都添加@mapper注解会很麻烦!!!
这时可以使用@MapperScan注解来扫描包。
所以,一般在Application上面标注了MapperScan,如图所示。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41935702/article/details/88852598