关于使用spring.datasource.hikari连接数据库找不到找不到url的问题

applica.properties里配置连接数据库使用hikari

#链接数据库
spring.datasource.primary.jdbc-url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8 &serverTimezone=Asia/Shanghai
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver

出现的问题异常

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

未能配置数据源:未指定“url”属性,也无法配置嵌入式数据源。

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-09-08 18:28:06.345 ERROR 7148 --- [           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).


Process finished with exit code 1

原因:springboot连接数据库是默认的是 :spring.datasource为前缀

解决 配置重定义识别数据库连接方式为spring.datasource.primary

/**
 * 使用spring.datasource.hikari为前缀连接数据库
 * DataSource 默认为spring.datasource连接
 */
@Configuration
public class Dateresourse {

    @Bean(name = "PrimaryDataSource") //作为一个bean对象并命名
    @ConfigurationProperties(prefix = "spring.datasource.primary") //配置文件中,该数据源的前缀
    public DataSource PrimaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

完美解决

在这里插入图片描述

发布了15 篇原创文章 · 获赞 4 · 访问量 1460

猜你喜欢

转载自blog.csdn.net/qq_40791070/article/details/100634435