Springboot找不到数据源的url : Failed to configure a DataSource,Failed to determine a suitable driver class

在这里插入图片描述

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-28 19:20:59.033 ERROR 8824 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

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

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

1、问题:

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

Reason: Failed to determine a suitable driver class

2、这两句话翻译过来

无法配置数据源:没有设置指定’url’属性,并且无法配置任何数据源。

原因:无法确定合适的驱动程序类别

3、我绝对是设置了url和数据源的

阿里的druid数据库连接池,url也写的很全,但是就是有问题。
在这里插入图片描述
4、解决办法

我估计是spring 扫描不到这个数据源配置,然后我一点一点找错,试了一下午,程序感觉要崩溃了后来test都运行不了了,我就又新建了一个项目,导入依赖后挨个试到底是哪个配置出了问题。

终于,功夫不负有心人,我找到错误原因了!!!

mybatis需要mapper.xml文件,这个文件我一般放到Dao包下,扫描配置文件一般是扫描resources目录下的,所以需要手动配置xml文件在项目启动时候就导入进去,防止Not found mapper.xml

正是下面这行代码我也不知道具体原因为什么导入xml,我resources下的application.yaml内的数据源属性导入不进去呢?

我猜测是springboot一般管理资源文件都在resources目录下而且springboot是约定大于配置,但是我将文件放到java目录下,并自己在maven里面导入这个资源文件可能破坏了约定,可能springboot就以为我要自己管理文件了,就没有自动装配扫描到yaml下的配置源,从而报错。
在这里插入图片描述

解决办法

  • 第一种:保留上面的段代码并自定义一个@Configurer标注的,自己手动配置数据源的全部属性,而不是springboot的自动装配。

  • 第二种:删除上面的代码将mapper.xml放到resources目录下,全自动交给springboot管理

【注意】第二种,必须在resources目录下新建和java目录下的mapper.java一样的目录位置,这样运行项目时候才能将xml和java放在一个包下,Spring才能找到并自动装配。

在这里插入图片描述
项目启动生成看一下生成的target,确实在同一个目录下,这样就成功了。
在这里插入图片描述

第三种
将这些配置文件先导入进去

        <resources>
            <!-- 防止找不到mybatis的mapper.xml文件,在项目启动时候就导入进去-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>

            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <include>**/*.yaml</include>
                </includes>
                <filtering>true</filtering>
            </resource>

        </resources>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值