springboot+mybatis報錯找不到實體類的問題
springboot+mybatis報錯找不到實體類
找不到實體類的錯誤可能有很多,接下來列舉幾個地方
啟動類位置不對,啟動類應該在你的service和dao 的上一層,因為Spring是從啟動類所在目錄的同級目錄開始掃描的,當然你也可以放在其他地方,但需要配置,具體配置可以參考網上的其他文章!
mapper.xml文件的路勁配置是否正確,classpath指的是resources目錄,如果不在resources目錄下,註意正確配置路勁。
mapper接口類是否添加@Repository註解,表示這是數據訪問組件。如果采用註解的形式使用mybatis需要加@Mapper
service層是否添加@Service註解,將bean註入到上下文中.
啟動類是否添加@MapperScan(掃描mapper),@EntityScan(掃描實體類),如果啟動類不在上面(1)所說的位置,則需要自己添加@ComRepositor註解,自定義掃描的路徑從中找出標識瞭需要裝配的類自動裝配到spring的bean容器中。
springboot+mybatis 找不到實體類問題
No qualifying bean of type‘com.wj.bfsh.mapper.SysUserMapper‘ available
報錯如下
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.
我的啟動類:
@SpringBootApplication @MapperScan(basePackages = "com.wj.bfsh.mapper.*") public class BfshApplication { public static void main(String[] args) { SpringApplication.run(BfshApplication.class, args); } }
項目結構:
問題出現在
@MapperScan(basePackages = “com.wj.bfsh.mapper.*”)
修改為:
@MapperScan(basePackages = “com.wj.bfsh.mapper”)
其實就是dao層掃描的位置不對。
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。
推薦閱讀:
- springboot 如何取消starter的自動註入
- mybatis那些約定的配置你真的都瞭解嗎(經驗總結)
- SpringBoot對不同Bean註解的區別和使用場景說明
- 解決SpringBoot整合MybatisPlus分模塊管理遇到的bug
- springboot配置mybatis和事務管理方式