springboot 启动报错required a bean of type ‘com.dexin.base.BaseMapper’ that could not be found
****Description:
Field userTextMapper in com.springboot.demo.service.UserTextService required a bean of type ‘com.springboot.demo.dao.UserTextMapper’ that could not be found.
Action:
Consider defining a bean of type ‘com.springboot.demo.dao.UserTextMapper’ in your configuration.****
搜了很多文章都没解决,后来通过分析原理发现,造成spring没注册成功Bean的原因有一下几种!
1.包结构的问题。
项目启动时,只有@SpringBootApplication 所在的包被扫描,也就是只扫码启动类MainApplication.java所在的这个包,需要将启动类放在外层包,跟controller、dao、service同级
2.没有自动注入导致。
你的service类上面没有@service注解或者mapper上没有@mapper注解,但是这种情况比较少见,一般不会忘记。
3.配置了mybatis,但没有指定扫描的包。(很大可能)
**方法:**启动类加注释:@MapperScan(basePackages = { “mapper所在的包路径” }, sqlSessionFactoryRef = “sqlSessionFactory”),表示扫描xx.xx.mapper包下的所有mapper。 如有多个Mapper,@MapperScan(basePackages = { “Mapper1” ,“Mapper2”,“Mapper3”}
本文分析了SpringBoot启动时出现'Bean of type 'BaseMapper' not found'错误的原因,包括包结构不正确、自动注入缺失以及Mybatis配置未指定扫描包。解决方案包括调整启动类位置,确保@Service和@Mapper注解正确使用,以及在启动类中添加@MapperScan注解指定Mapper包路径。遵循这些步骤通常可以解决此类问题。
3544

被折叠的 条评论
为什么被折叠?



