势必拿下SpringBoot 2022-01-19 16:13 采纳率: 100%
浏览 620
已结题

A component required a bean of type 'com.fish.marketgoods.dao.UserDao' that could not be found.

最近在跟着别人的视频写一个项目但是今天在试着通过控制层映射到前端的时候出现了报错,看了很多博主的帖子也没能解决问题,希望各位可以帮我指正
这是一个Maven的前后端结合的项目通过springboot和mybatis实现的项目
首先贴出项目的目录

img

以下是报错的信息

2022-01-19 15:37:10.539  INFO 10100 --- [           main] c.f.marketgoods.MarketGoodsApplication   : Starting MarketGoodsApplication using Java 1.8.0_191 on DESKTOP-FJ2DGVQ with PID 10100 (D:\market-goods\target\classes started by Administrator in D:\market-goods)
2022-01-19 15:37:10.542  INFO 10100 --- [           main] c.f.marketgoods.MarketGoodsApplication   : No active profile set, falling back to default profiles: default
2022-01-19 15:37:10.988  WARN 10100 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.fish.marketgoods.dao.UserDao]' package. Please check your configuration.
2022-01-19 15:37:11.201  INFO 10100 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 80 (http)
2022-01-19 15:37:11.209  INFO 10100 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-01-19 15:37:11.209  INFO 10100 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.54]
2022-01-19 15:37:11.295  INFO 10100 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-01-19 15:37:11.295  INFO 10100 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 716 ms
2022-01-19 15:37:11.331  WARN 10100 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'indexController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fish.marketgoods.dao.UserDao' 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=)}
2022-01-19 15:37:11.333  INFO 10100 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-01-19 15:37:11.345  INFO 10100 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-01-19 15:37:11.362 ERROR 10100 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean of type 'com.fish.marketgoods.dao.UserDao' that could not be found.


Action:

Consider defining a bean of type 'com.fish.marketgoods.dao.UserDao' in your configuration.

然后是application.yml文件的配置

server:
  port: 80 #当前运行的端口



  spring:
    datasource: #数据源配置
      type:  com.alibaba.druid.pool.DruidDataSource  #连接池的类型
      password: 123456
      username: root
      driver-class-name: com.mysql.jdbc.Driver  #数据库的驱动程序类  #如果是mysql5.*的版本 不需要加入cj!!!!!!
      url: jdbc:mysql://localhost:3306/goods_market_db?serverTimezone=GMT  #mysql8.0以上需要加入时区不然会连接不上



mybatis:
  configuration:
    map-underscore-to-camel-case: true #数据库中的下划线自动转化成驼峰
    mapper-location: classpath:mybatis/mapper/*.xml   #mapper文件的路径,这个路径下面的mapper.xml文件会被自动加载成mybatis的映射文件
     #classpath resource和view文件都是classpath  #**表示多级目录下的
                                            #所有xml文件

这是UserDao文件的代码(因为别的博主说要加入Mapper的注解所有我后来加上了但是还是报了同样的错误)

package com.fish.marketgoods.dao;

import com.fish.marketgoods.pojo.entity.User;
import org.apache.ibatis.annotations.Mapper;


@Mapper
public interface UserDao {


}

这是UserDao.xml文件的配置(因为是配置出现了错误我就不贴出功能的代码了免得占用过多的空间)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fish.marketgoods.dao.UserDao">
  <resultMap id="BaseResultMap" type="com.fish.marketgoods.pojo.entity.User">
    <id column="user_id" jdbcType="INTEGER" property="userId"/>
    <result column="nick_name" jdbcType="VARCHAR" property="nickName" />
    <result column="real_name" jdbcType="VARCHAR" property="realName" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="last_login_time" jdbcType="TIMESTAMP" property="lastLoginTime" />
    <result column="last_login_ip" jdbcType="VARCHAR" property="lastLoginIp" />
  </resultMap>

项目启动文件MarketGoodsApplication的代码(我也根据别的博主在@SpringBootApplication中加入(exclude= DataSourceAutoConfiguration.class)但是还是会报错)代码如下

package com.fish.marketgoods;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

/*这个是启动程序的主类*/
@SpringBootApplication(exclude= DataSourceAutoConfiguration.class)
public class MarketGoodsApplication {

    public static void main(String[] args) {
        SpringApplication.run(MarketGoodsApplication.class, args);
    }

}

谢谢!

  • 写回答

1条回答 默认 最新

  • 小飞LOVE霞 2022-01-19 16:22
    关注

    1、给主类MarketGoodsApplication加注解 @MapperScan("com.fish.marketgoods.dao")
    2、pom.xml文件的build节点下加入如下代码:

    <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                        <include>**/*.yml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                        <include>**/*.yml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
            </resources>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月27日
  • 已采纳回答 1月19日
  • 创建了问题 1月19日

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗