springboot啟動(dòng)掃描不到dao層接口的解決方案
今天啟動(dòng)springboot項(xiàng)目時(shí)失敗了
解決
檢查原因發(fā)現(xiàn)是啟動(dòng)類(lèi)的MapperScan("")的值寫(xiě)到類(lèi)名了,改成類(lèi)所在的包名錯(cuò)誤就修復(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); } }
二、問(wèn)題:
- 提示controller和services層找不到
- 訪問(wèn)controller的方法顯示404錯(cuò)誤
解決:
1、啟動(dòng)類(lèi)放到跟目錄下面,如圖,我的controller和service分別在com.imooc2.product的product文件夾和category文件夾里面,所以啟動(dòng)類(lèi)要放在根目錄com.imooc2.product下
原因:sprigboot 會(huì)自動(dòng)掃描根目錄以下的全部包
2、有可能是緩存還是項(xiàng)目啟動(dòng)類(lèi)識(shí)別不了controller層和service層,或者你不想把啟動(dòng)類(lèi)放到根目錄下去默認(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); } }
注意:不要使用錯(cuò)誤注解
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 如何實(shí)現(xiàn)一個(gè)http服務(wù)器
這篇文章主要介紹了Java 如何實(shí)現(xiàn)一個(gè)http服務(wù)器,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下2020-11-11詳解Springboot2.3集成Spring security 框架(原生集成)
這篇文章主要介紹了詳解Springboot2.3集成Spring security 框架(原生集成),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Java定時(shí)器Timer與TimerTask的使用詳解
這篇文章主要介紹了Java定時(shí)器Timer與TimerTask的使用詳解,在JDK類(lèi)庫(kù)中Timer主要負(fù)責(zé)計(jì)劃任務(wù)的功能,也就是在指定時(shí)間執(zhí)行某一任務(wù),執(zhí)行時(shí)候會(huì)在主線程之外起一個(gè)單獨(dú)的線程執(zhí)行指定的任務(wù),該類(lèi)主要是設(shè)置任務(wù)計(jì)劃,但封裝的類(lèi)是TimerTask類(lèi),需要的朋友可以參考下2023-10-10IDEA在plugins里搜不到mybatisx插件的解決方法
本文主要介紹了IDEA在plugins里搜不到mybatisx插件的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Java通過(guò) Socket 實(shí)現(xiàn) TCP服務(wù)端
這篇文章主要介紹了Java通過(guò) Socket 實(shí)現(xiàn) TCP服務(wù)端的相關(guān)資料,需要的朋友可以參考下2017-05-05springboot logback如何從apollo配置中心讀取變量
這篇文章主要介紹了springboot logback如何從apollo配置中心讀取變量的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08詳解JAVA使用Comparator接口實(shí)現(xiàn)自定義排序
這篇文章主要介紹了JAVA使用Comparator接口實(shí)現(xiàn)自定義排序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03