[英]Spring boot data source error during mysql connection
當我在家里使用我的電腦時,它運行良好,沒有任何錯誤,但是當我通過 git 將項目下載到我的筆記本電腦時,出現如下錯誤。
2022-08-01 00:12:43.419 INFO 25004 --- [ main] c.p.r.mysqlcheck.MySqlConnectionTest : The following 1 profile is active: "config"
***************************
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
2
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 (the profiles config are currently active).
我在谷歌上搜索,它說這個錯誤是由於錯誤的數據源信息而發生的。 但是我檢查了幾次數據源,但沒有任何問題。
所以我認為配置配置文件沒有正確注冊。
我怎么解決這個問題??
我將在下面顯示我的代碼。
應用程序屬性
#Important information will be placed in here
spring.profiles.include=config
## I'm going to hide this file beacause it has personal information
#mybatis mapper location
mybatis.type-aliases-package=com.prac.react.model.dto
mybatis.mapper-locations=classpath:mappers/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
應用程序-config.properties
spring.datasource.url=jdbc:mysql://localhost:3306/kculter?serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=id
spring.datasource.password=1234
##db를 로컬 서버로 돌렸고 username과 pwd는 제 로컬에서 사용하는걸로 변경했습니다.
##db 서버 연결 테스트코드를 위한 변수를 만들었습니다.
mysqlusername=id
pwd=1234
測試代碼
@SpringBootTest
public class MySqlConnectionTest {
private final String DRIVER = "com.mysql.jdbc.Driver"; //mysql 드라이버 생성 주소?
private final String URL = "jdbc:mysql://localhost:3306/kculter"; //mysql 주소
@Autowired
Mysql mysql;
Logger logger = LoggerFactory.getLogger(MySqlConnectionTest.class);
@Test
@DisplayName("MySql 연결 확인 테스트")
public void testConnection() throws Exception{
boolean flag = true;
logger.info(mysql.toString());
Class.forName(DRIVER); //위에서의 정보르들을 가지고 해당 driver를 JVM에 등록시키는것
try(Connection con = DriverManager.getConnection(URL,mysql.getUsername(),mysql.getPwd())){
logger.info(con.toString()); //콘솔창에서 연결정보
}catch(Exception e) {
logger.error("연결 실패");
flag = false;
assertTrue(flag);
}
}
}
Mysql.java
package com.prac.react.model.dto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Mysql {
@Value("${mysqlusername}")
private String username;
@Value("${pwd}")
private String pwd;
public Mysql() {}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return this.pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "{" +
" username='" + getUsername() + "'" +
", pwd='" + getPwd() + "'" +
"}";
}
}
spring 引導應用程序的 JPA 自動配置功能嘗試使用 JPA 數據源建立數據庫連接。 JPA DataSource bean 需要數據庫驅動程序才能連接到數據庫。
數據庫驅動程序應作為 pom.xml 文件中的依賴項提供。 For the external databases such as Oracle, SQL Server, MySql, DB2, Postgres, MongoDB etc requires the database JDBC connection properties to establish the connection. H2、HSQL、Derby 等內存數據庫將建立沒有 JDBC 連接屬性的連接,因為它是 spring 引導應用程序的一部分。
如果您在 pom.xml 文件中配置了 spring-boot-starter-data-jpa 依賴項並且您沒有在 application.properties 中定義任何數據源 url ,則會發生此錯誤
我遇到了同樣的問題,我通過全新安裝解決了它,然后右鍵單擊項目,maven/update 項目。
如果這不能解決您的問題,請確保您的application.properties包含完整的配置。 還要確保您的 pom.xml 文件具有適用於您的 DB 的 JDBC 驅動程序
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
應用程序屬性
spring.datasource.url=jdbc:mysql://localhost:3306/kculter
spring.datasource.username=id
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.