mapper文件无法扫描问题

前言

本文,记录了搭建Springboot项目脚手架中接入数据库操作遇到的问题

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)
Description:

A component required a bean of type 'com.shine.demo.infrastructure.db.mapper.CoinInsertMapper' that could not be found.


Action:

Consider defining a bean of type 'com.shine.demo.infrastructure.db.mapper.CoinInsertMapper' in your configuration.

内容:

在进行Springboot项目脚手架的搭建过程中,接入数据库,引入了Mybatis-Plus实现mapper功能。

其中探究需要的各个依赖,在启动项目的途中遇到了配置数据源失败问题

问题一:数据源配置失败

Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.

image-20231226180118279

根据百度寻找答案,得到了修改启动类的注解@SpringBootApplication,具体操作是:

也就是将@SpringBootApplication改成下面

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

image-20231226180640358

然后重新编译启动,报错通过@Autowired 注入mapper文件失败,找不到。

问题二:@Autowired找不到mapper文件

image-20231226181252708

@Autowired修改成@Resource注入,再启动。又爆出一个新的问题,还是启动失败。

问题三:mapper文件扫描不到

image-20231226181445549

根据问题描述是mapper文件没有扫描到,检查了各种注解,发先问题不在这上面,具体如下

image-20231226181900308

根据网上的,瞎摸着在启动类加@MapperScan,也不行。目前的话整体的项目启动就停止在这里了,找了很多解决方案,但是都无法解决。

但是其实,我们回到遇到的第一个问题那。

解决问题:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.数据源配置失败

ok呀,那让我们来检查一下数据库的配置文件。

原:

spring:
  application:
    name: demo
    datasource:
      #敏感数据脱敏掉了
      url: jdbc:mysql://xxxxx
      username: root
      password: mysql8@root
      driver-class-name: com.mysql.cj.jdbc.Driver
      druid:
        minimum-idle: 5
        maximum-pool-size: 20
        idle-timeout: 300000
        connection-timeout: 60000
        max-lifetime: 900000

正常文件:

spring:
  application:
    name: demo
  datasource:
  	#敏感数据脱敏掉了
    url: jdbc:mysql://xxx
    username: root
    password: mysql8@root
    driver-class-name: com.mysql.cj.jdbc.Driver
    druid:
      minimum-idle: 5
      maximum-pool-size: 20
      idle-timeout: 300000
      connection-timeout: 60000
      max-lifetime: 900000

其实我乍一看的话,其实看不出什么问题,然后和其他可以正常运行的项目的配置文件去对比。才发现是datasource整体向后缩了一格!!!!

将整体的datasource向前移动一格,重新编译启动,成功!!

image-20231226183340155

总结:

  1. 遇到问题,先了解问题本质,没有必要急吼吼的去百度。网上的问题大部分都和你的不一样,可能一样,但是太花时间了。
    成功!!在这里插入图片描述

总结:

  1. 遇到问题,先了解问题本质,没有必要急吼吼的去百度。网上的问题大部分都和你的不一样,可能一样,但是太花时间了。
  2. 很多配置数据格式编译是隐性的,我们可能会忽略这方面,可以检查一下格式,缩进等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值