【问题标题】:Spring Boot - Initialize mysql with .sql Script is failedSpring Boot - 使用 .sql 脚本初始化 mysql 失败
【发布时间】: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


【解决方案1】:

飞行路线示例。添加到资源文件夹,例如“脚本”并放置您的脚本。然后添加属性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

【讨论】:

    【解决方案2】:

    我未能使用“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>
    

    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
    

    数据文件(.sql)

    • 位置:资源/数据库/迁移
    • DDL 文件名:V1__create_testDB.sql(这里是创建表查询)
    • DML 文件名:V1.1__insert_testDB.sql(此处插入查询)

    【讨论】:

      猜你喜欢
      • 2017-01-29
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 2018-06-10
      • 1970-01-01
      • 2018-06-09
      • 2017-03-17
      相关资源
      最近更新 更多