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层扫描的位置不对。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
推荐阅读
-
springboot+mybatis报错找不到实体类的问题
-
关于Cookie中带有中文乱码报错的问题解决
-
ECSHOP完美解决Deprecated: preg_replace()报错的问题,ecshopdeprecated
-
解决pandas read_csv 读取中文列标题文件报错的问题
-
有关数据库找不到表的有关问题
-
MySQL-5.1.x在RH5上安装报错的问题
-
项目部署到tomcat服务器,在tomcat目录下找不到的问题
-
ORA-01455报错的问题探究
-
详解mysql密码遗忘和登陆报错的问题解决
-
解决eclipse启动时报错Failed to create the Java Virtural Machine.问题的方法