前言
本文,记录了搭建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.
根据百度寻找答案,得到了修改启动类的注解@SpringBootApplication
,具体操作是:
也就是将@SpringBootApplication
改成下面
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
然后重新编译启动,报错通过@Autowired
注入mapper文件失败,找不到。
问题二:@Autowired找不到mapper文件
将@Autowired
修改成@Resource
注入,再启动。又爆出一个新的问题,还是启动失败。
问题三:mapper文件扫描不到
根据问题描述是mapper文件没有扫描到,检查了各种注解,发先问题不在这上面,具体如下
根据网上的,瞎摸着在启动类加@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
向前移动一格,重新编译启动,成功!!
总结:
- 遇到问题,先了解问题本质,没有必要急吼吼的去百度。网上的问题大部分都和你的不一样,可能一样,但是太花时间了。
成功!!
总结:
- 遇到问题,先了解问题本质,没有必要急吼吼的去百度。网上的问题大部分都和你的不一样,可能一样,但是太花时间了。
- 很多配置数据格式编译是隐性的,我们可能会忽略这方面,可以检查一下格式,缩进等。