springboot啟動掃描不到dao層接口的解決方案
今天啟動springboot項(xiàng)目時失敗了
解決
檢查原因發(fā)現(xiàn)是啟動類的MapperScan("")的值寫到類名了,改成類所在的包名錯誤就修復(fù)了。
springboot 掃描不到dao層和controller
一、提示
A component required a bean of type ‘com.imooc2.product.category.dao.ProductCategoryDao' that could not be found即dao層找不到了
解決:使用@MapperScan 注解或者@MapperScans注解
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScans; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 @SpringBootApplication @MapperScan("com.imooc2.product.**.dao") public class ProductApplication {//extends SpringBootServletInitializer public static void main(String[] args) { SpringApplication.run(ProductApplication.class, args); } }
二、問題:
- 提示controller和services層找不到
- 訪問controller的方法顯示404錯誤
解決:
1、啟動類放到跟目錄下面,如圖,我的controller和service分別在com.imooc2.product的product文件夾和category文件夾里面,所以啟動類要放在根目錄com.imooc2.product下
原因:sprigboot 會自動掃描根目錄以下的全部包
2、有可能是緩存還是項(xiàng)目啟動類識別不了controller層和service層,或者你不想把啟動類放到根目錄下去默認(rèn)掃描,可以更新至springboot 2.0.5.RELEASE,使用自定義掃描包注解
@ComponentScan(basePackages = {“com.imooc2.product.product”, “com.imooc2.product.category”})
或者
@SpringBootApplication(scanBasePackages = {“com.imooc2.product.product”, “com.imooc2.product.category”})
二選一即可如圖所示
@SpringBootApplication(scanBasePackages = {"com.imooc2.product","com.imooc2.product.**.dao"})//二選一 @ComponentScan(basePackages = {"com.imooc2.product.product", "com.imooc2.product.category"})//二選一 @MapperScan("com.imooc2.product.**.dao") public class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class, args); } }
注意:不要使用錯誤注解
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 如何實(shí)現(xiàn)一個http服務(wù)器
這篇文章主要介紹了Java 如何實(shí)現(xiàn)一個http服務(wù)器,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下2020-11-11詳解Springboot2.3集成Spring security 框架(原生集成)
這篇文章主要介紹了詳解Springboot2.3集成Spring security 框架(原生集成),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08IDEA在plugins里搜不到mybatisx插件的解決方法
本文主要介紹了IDEA在plugins里搜不到mybatisx插件的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Java通過 Socket 實(shí)現(xiàn) TCP服務(wù)端
這篇文章主要介紹了Java通過 Socket 實(shí)現(xiàn) TCP服務(wù)端的相關(guān)資料,需要的朋友可以參考下2017-05-05springboot logback如何從apollo配置中心讀取變量
這篇文章主要介紹了springboot logback如何從apollo配置中心讀取變量的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08詳解JAVA使用Comparator接口實(shí)現(xiàn)自定義排序
這篇文章主要介紹了JAVA使用Comparator接口實(shí)現(xiàn)自定義排序,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03