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”}