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

作者:zlhmeng 时间:2023-05-20 02:04:10 

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层扫描的位置不对。

来源:https://blog.csdn.net/zlhmeng/article/details/89353353

标签:springboot,mybatis,实体类
0
投稿

猜你喜欢

  • 深入理解C# 装箱和拆箱(整理篇)

    2023-10-04 02:13:13
  • 基于Java Socket实现一个简易在线聊天功能(一)

    2023-09-23 17:18:35
  • java 对称加密算法实现详解

    2021-07-29 22:34:57
  • 简单谈谈java自定义注解

    2021-07-21 18:40:53
  • SpringBoot实现过滤器拦截器的耗时对比

    2022-04-12 00:45:04
  • Java MyBatis本地缓存原理详解

    2023-01-30 18:20:36
  • Android如何防止apk程序被反编译(尊重劳动成果)

    2022-02-08 02:18:04
  • Android硬件解码组件MediaCodec使用教程

    2023-03-14 01:35:36
  • C# 反射与dynamic最佳组合示例代码

    2022-01-18 02:29:53
  • 全面解析SpringBoot文件上传功能

    2023-02-26 15:55:56
  • Java并发编程之同步容器与并发容器详解

    2023-10-17 04:06:23
  • SpringBoot接入支付宝支付的方法步骤

    2022-02-16 05:34:16
  • C# 实现Distinct将对象按条件去重

    2023-07-24 17:29:59
  • c#固定长度的随机字符串例子

    2021-10-24 06:11:10
  • 浅析SpringBoot2.4 静态资源加载问题

    2023-01-29 11:04:33
  • Entity Framework配置关系

    2023-10-15 09:45:30
  • Java设计模式之共享模式/享元模式(Flyweight模式)介绍

    2023-06-15 04:35:10
  • 基于Android本地代码生成器详解

    2022-09-15 02:52:26
  • C#多线程学习之(五)使用定时器进行多线程的自动管理

    2022-03-05 13:55:44
  • Mybatis使用大于等于或小于等于进行比较

    2021-12-25 10:21:44
  • asp之家 软件编程 m.aspxhome.com