![](/img/trans.png)
[英]What is wrong with my datasource settings to get Spring Boot to connect to MySQL?
[英]Spring boot - MySQL settings are not working
我正在嘗試使用 Spring Boot 和 MySQL 開發應用程序。 正如文檔所說,首先我使用Intelij Idea使用Spring initializr創建了項目,配置了application.properties
文件,並編寫了schema-mysql.sql
文件和data-mysql.sql
文件。 跑完項目,發現MySQL數據庫中沒有表,也沒有數據。 我的配置有什么問題? 請幫忙。
application.properties 文件,
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.schema=schema-mysql.sql
spring.datasource.data=data-mysql.sql
pom.xml 文件中的依賴項,
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
schema-mysql.sql 文件,
CREATE TABLE IF NOT EXISTS `SOL16_USERS` (
`USERNAME` VARCHAR(200) NOT NULL,
'PASSWORD' VARCHAR(200) NOT NULL,
PRIMARY KEY (`USERNAME`)
);
CREATE TABLE IF NOT EXISTS 'SOL16_PRIVILEGES' (
'PRIVILEGE_ID' INT(2) NOT NULL,
'PRIVILEGE' VARCHAR(15) NOT NULL,
PRIMARY KEY ('PRIVILEGE_ID')
);
CREATE TABLE IF NOT EXISTS 'SOL16_USER_PRIVILEGES' (
'USERNAME' VARCHAR(200) NOT NULL,
'PRIVILEGE_ID' VARCHAR(2) NOT NULL,
PRIMARY KEY ('USERNAME')
);
文件/目錄結構是,
src
|----main
| |----java
| |----resources
| | |----static
| | |----templates
| | |----application.properties
| | |----data-mysql.sql
| | |----schema-mysql.sql
正如 Spring 啟動文檔所提到的,解決我的問題是將spring.datasource.platform
添加到application.properties
。 這就是我使用schema-{platform}.sql
和data-{platform}.sql
初始化架構時所缺少的。
{platform} =
spring.datasource.platform
值
所以我的最終 application.properties 文件是,
spring.datasource.url = jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username = root
spring.datasource.password = password
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto = validate
spring.jpa.show-sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.platform=mysql
spring.datasource.schema=schema-mysql.sql
spring.datasource.data=data-mysql.sql
spring.datasource.initialize=true
spring.datasource.continue-on-error=true
我遇到了同樣的問題並在 spring boot 2.0 中應用以下代碼解決了它
spring.datasource.initialization-mode=always
您需要在 application.properties 中添加以下代碼並打開您的 xammp/wamp 或服務器,然后創建您的數據庫,例如在 application.properties 文件中給出相同名稱的 studentdb
spring.datasource.url=jdbc:mysql://localhost:3306/yourdatabase
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
請使用下面給出的關鍵字段---->
spring.jpa.hibernate.ddl-auto=create
或
spring.jpa.hibernate.ddl-auto=update
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.