欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot+mybatis报错找不到实体类的问题

程序员文章站 2022-03-02 22:28:13
目录springboot+mybatis报错找不到实体类找不到实体类的错误可能有很多,接下来列举几个地方springboot+mybatis 找不到实体类问题no qualifying bean of...

springboot+mybatis报错找不到实体类

找不到实体类的错误可能有很多,接下来列举几个地方

启动类位置不对,启动类应该在你的service和dao 的上一层,因为spring是从启动类所在目录的同级目录开始扫描的,当然你也可以放在其他地方,但需要配置,具体配置可以参考网上的其他文章!

springboot+mybatis报错找不到实体类的问题

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

springboot+mybatis报错找不到实体类的问题

mapper接口类是否添加@repository注解,表示这是数据访问组件。如果采用注解的形式使用mybatis需要加@mapper

springboot+mybatis报错找不到实体类的问题

service层是否添加@service注解,将bean注入到上下文中.

springboot+mybatis报错找不到实体类的问题

启动类是否添加@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);
    }
}

项目结构:

springboot+mybatis报错找不到实体类的问题

问题出现在

@mapperscan(basepackages = “com.wj.bfsh.mapper.*”)

修改为:

@mapperscan(basepackages = “com.wj.bfsh.mapper”)

其实就是dao层扫描的位置不对。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。