【发布时间】:2022-01-09 14:21:40
【问题描述】:
当我启动 spring 项目时,我需要关于运行 sql 脚本的建议。
我在这里:
- 我不想想使用 JPA(Hibernate) -> 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>
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
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);
【问题讨论】:
-
你如何尝试执行这个脚本?
-
@andrew17 我尝试在启动 spring 项目时自动运行 db 脚本。我想这是可能的,不是吗?
-
但是在哪里呢?你是用代码做的吗?通过 MySQL 连接器连接并使用 jdbc?但我没有看到 JDBC 依赖项。使用迁移工具?然后我看不到迁移依赖。你如何尝试执行你的 sql 脚本?它位于哪里?
-
@andrew17 哦,我想在运行诸如 h2 数据库初始化之类的 spring 应用程序时初始化 DB。(创建模式,插入数据..),而不是在我的 java 代码上。
-
那么运行Spring App后,你手动连接MySQL,自己在那里执行sql?
标签: java mysql spring-boot