當我啟動 spring 專案時,我需要一個運行 sql 腳本的建議。
這就是我所在的位置:
- 我不希望使用JPA(休眠) - >在pom.xml中沒有JPA依賴
- Spring 專案,mysql 運行良好,但我的資料庫中沒有資料。
看起來很小的問題,但無法弄清楚問題是什么;(
提前致謝!
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
</dependencies>
應用程式屬性
spring.datasource.url=jdbc:mysql://localhost:3306/MyDB
spring.datasource.username=root
spring.datasource.password=root-password
spring.sql.init.mode=always
spring.sql.init.platform=mysql
架構-mysql.sql
DROP TABLE IF EXISTS test_db;
CREATE TABLE test_db
(
id int NOT NULL AUTO_INCREMENT,
value VARCHAR(25),
PRIMARY KEY(id)
);
資料-mysql.sql
INSERT INTO test_db(value) VALUES(100);
uj5u.com熱心網友回復:
飛路示例。添加到資源檔案夾,例如“腳本”并放置您的腳本。然后添加屬性 application.properties
spring.flyway.user=root
spring.flyway.password=root-password
spring.flyway.schema=public
spring.flyway.url=jdbc:mysql://localhost:3306/MyDB
spring.flyway.locations=classpath:/scripts
uj5u.com熱心網友回復:
我沒有使用“JDBC 初始化”這個概念。但是感謝@andrew17 和@K.Nikita 的意見,我決定使用Flyway。(我以前從不知道flyway,再次感謝!)
所以這是組態檔的結果。
pom.xml
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<configuration>
<url>jdbc:mysql://localhost:3306/Restagram</url>
<user>root</user>
<password>qwER12#$</password>
<baselineOnMigrate>true</baselineOnMigrate>
</configuration>
</plugin>
應用程式屬性
spring.datasource.url=jdbc:mysql://localhost:3306/MyDB
spring.datasource.username=root
spring.datasource.password=root-password
spring.sql.init.mode=always
spring.sql.init.platform=mysql
資料檔案(.sql)
- 位置:資源/資料庫/遷移
- DDL 檔案名:V1__create_testDB.sql(創建表查詢在這里)
- DML 檔案名:V1.1__insert_testDB.sql(插入查詢在這里)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/407539.html
標籤:
上一篇:java-如何避免在Java中將資料存盤在mysqldb中的注冊文本欄位中輸入空白?如何避免不填寫用戶名和密碼就登錄?
下一篇:密碼登錄在MySQL中不起作用
