springboot+mybatis報(bào)錯(cuò)找不到實(shí)體類的問題
springboot+mybatis報(bào)錯(cuò)找不到實(shí)體類
找不到實(shí)體類的錯(cuò)誤可能有很多,接下來列舉幾個(gè)地方
啟動(dòng)類位置不對(duì),啟動(dòng)類應(yīng)該在你的service和dao 的上一層,因?yàn)镾pring是從啟動(dòng)類所在目錄的同級(jí)目錄開始掃描的,當(dāng)然你也可以放在其他地方,但需要配置,具體配置可以參考網(wǎng)上的其他文章!

mapper.xml文件的路勁配置是否正確,classpath指的是resources目錄,如果不在resources目錄下,注意正確配置路勁。

mapper接口類是否添加@Repository注解,表示這是數(shù)據(jù)訪問組件。如果采用注解的形式使用mybatis需要加@Mapper

service層是否添加@Service注解,將bean注入到上下文中.

啟動(dòng)類是否添加@MapperScan(掃描mapper),@EntityScan(掃描實(shí)體類),如果啟動(dòng)類不在上面(1)所說的位置,則需要自己添加@ComRepositor注解,自定義掃描的路徑從中找出標(biāo)識(shí)了需要裝配的類自動(dòng)裝配到spring的bean容器中。
springboot+mybatis 找不到實(shí)體類問題
No qualifying bean of type‘com.wj.bfsh.mapper.SysUserMapper‘ available
報(bào)錯(cuò)如下
2021-02-18 09:45:58,826 - Starting BfshApplication on DESKTOP-AAPVN38 with PID 10552 (F:\IdeaIu\work_place\bfsh\target\classes started by AoDexiusi in F:\IdeaIu\work_place\bfsh)
2021-02-18 09:45:58,828 - Running with Spring Boot v2.3.7.RELEASE, Spring v5.2.12.RELEASE
2021-02-18 09:45:58,829 - No active profile set, falling back to default profiles: default
2021-02-18 09:45:58,868 - Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-02-18 09:45:58,868 - For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-02-18 09:45:59,438 - No MyBatis mapper was found in '[com.wj.bfsh.mapper.*]' package. Please check your configuration.
2021-02-18 09:45:59,809 - Tomcat initialized with port(s): 8888 (http)
2021-02-18 09:45:59,816 - Starting service [Tomcat]
2021-02-18 09:45:59,816 - Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-02-18 09:45:59,925 - Initializing Spring embedded WebApplicationContext
2021-02-18 09:45:59,925 - Root WebApplicationContext: initialization completed in 1057 ms
2021-02-18 09:45:59,969 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysUserController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysUserServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.wj.bfsh.mapper.SysUserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
2021-02-18 09:45:59,971 - Stopping service [Tomcat]
2021-02-18 09:45:59,978 - The web application [bfsh] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:40)
2021-02-18 09:45:59,986 -Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-02-18 09:46:00,105 -***************************
APPLICATION FAILED TO START
***************************Description:
A component required a bean of type 'com.wj.bfsh.mapper.SysUserMapper' that could not be found.
Action:Consider defining a bean of type 'com.wj.bfsh.mapper.SysUserMapper' in your configuration.
我的啟動(dòng)類:
@SpringBootApplication
@MapperScan(basePackages = "com.wj.bfsh.mapper.*")
public class BfshApplication {
public static void main(String[] args) {
SpringApplication.run(BfshApplication.class, args);
}
}
項(xiàng)目結(jié)構(gòu):

問題出現(xiàn)在
@MapperScan(basePackages = “com.wj.bfsh.mapper.*”)
修改為:
@MapperScan(basePackages = “com.wj.bfsh.mapper”)
其實(shí)就是dao層掃描的位置不對(duì)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Spring依賴注入的三種方式以及優(yōu)缺點(diǎn)
IoC?和?DI?是?Spring?中最重要的兩個(gè)概念,其中?IoC(Inversion?of?Control)為控制反轉(zhuǎn)的思想,而?DI(Dependency?Injection)依賴注入為其(IoC)具體實(shí)現(xiàn)。那么?DI?實(shí)現(xiàn)依賴注入的方式有幾種?這些注入方式又有什么不同?本文就來和大家一起詳細(xì)聊聊2022-08-08
Feign遠(yuǎn)程調(diào)用Multipartfile參數(shù)處理
這篇文章主要介紹了Feign遠(yuǎn)程調(diào)用Multipartfile參數(shù)處理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java微服務(wù)Filter過濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過程詳解
這篇文章主要介紹了Java微服務(wù)Filter過濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過程,首先Sentinel規(guī)則的存儲(chǔ)默認(rèn)是存儲(chǔ)在內(nèi)存的,應(yīng)用重啟之后規(guī)則會(huì)丟失。因此我們通過配置中心Nacos保存規(guī)則,然后通過定時(shí)拉取Nacos數(shù)據(jù)來獲取規(guī)則配置,可以做到動(dòng)態(tài)實(shí)時(shí)的刷新規(guī)則2023-02-02
JeecgBoot框架升級(jí)至Spring?Boot3的實(shí)戰(zhàn)步驟
本文主要介紹了JeecgBoot框架升級(jí)至Spring?Boot3的實(shí)戰(zhàn)步驟,從?2.7.10升級(jí)到3.1.5有以下幾個(gè)點(diǎn)需要注意,下面就來詳細(xì)的介紹一下,感興趣的可以了解一下2024-04-04
詳解使用Maven構(gòu)建多模塊項(xiàng)目(圖文)
這篇文章主要介紹了詳解使用Maven構(gòu)建多模塊項(xiàng)目(圖文),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09
SpringBoot數(shù)據(jù)脫敏的實(shí)現(xiàn)示例
數(shù)據(jù)脫敏主要應(yīng)用在客戶安全數(shù)據(jù)或商業(yè)性敏感數(shù)據(jù)的情況,本文主要介紹了SpringBoot數(shù)據(jù)脫敏的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
Java實(shí)現(xiàn)二分查找算法實(shí)例分析
這篇文章主要介紹了Java實(shí)現(xiàn)二分查找算法,實(shí)例分析了二分查找算法的原理與相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
Java調(diào)用C++動(dòng)態(tài)庫超詳細(xì)步驟講解(附源碼)
C語言因其高效和接近硬件的特性,時(shí)常會(huì)被用在性能要求較高或者需要直接操作硬件的場(chǎng)合,這篇文章主要介紹了Java調(diào)用C++動(dòng)態(tài)庫的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04

