最近在使用springboot整合mybatis-plus時遇到@Autowired失效問題,導致容器無法啟動
項目結構如下:
啟動類LsTestApplication:
控制器UserController:
實現類UserServiceImpl:
UserMapper:
啟動之后一直報錯,報錯信息如下:
2019-08-23 17:16:16.457 WARN 8528 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.sand.demo.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
猜測:
1、可能是組件沒掃描到,相應的組件未加注解
2、啟動類不在掃描包的根目錄下也會導致bean注冊失敗
使用IDEA開發工具查看組件是有的,所以以上兩種可能都排除了
那就只剩下一種可能了,架包引用錯了,查看pom文件發現引用的是mybatis-plus依賴,
根據提供的文檔https://mp.baomidou.com/guide/install.html#release,springboot項目應該引用mybatis-plus-boot-starter這個依賴
然后記得將mapper掃描路徑加上,問題就解決了