[英]Spring Boot - Initialize mysql with .sql Script is failed
I need an advice for running sql script when i startup with spring project.当我启动 spring 项目时,我需要关于运行 sql 脚本的建议。
Here's where i am:这就是我所在的位置:
looks very small issue, but can't figure out what is the problem;(看起来很小的问题,但无法弄清楚是什么问题;(
Thanks in advance!提前致谢!
pom.xml 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>
application.properties应用程序属性
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
schema-mysql.sql架构-mysql.sql
DROP TABLE IF EXISTS test_db;
CREATE TABLE test_db
(
id int NOT NULL AUTO_INCREMENT,
value VARCHAR(25),
PRIMARY KEY(id)
);
data-mysql.sql数据-mysql.sql
INSERT INTO test_db(value) VALUES(100);
Flyway example.飞路示例。 Add to resources folder, for example 'scripts' and put your scripts.添加到资源文件夹,例如“脚本”并放置您的脚本。 Then add property application.properties然后添加属性 application.properties
spring.flyway.locations=classpath:/scripts
I failed to use the concept 'JDBC initialize'.我没有使用“JDBC 初始化”这个概念。 But thanks to the opinion @andrew17 and @K.Nikita, I decided to use Flyway.但是感谢@andrew17 和@K.Nikita 的意见,我决定使用Flyway。 (I'd never known flyway before, thanks again!) (我以前从不知道flyway,再次感谢!)
So Here is the result of the configuration files.所以这是配置文件的结果。
pom.xml 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>
application.properties应用程序属性
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
data file(.sql)数据文件(.sql)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.