简体   繁体   English

Spring 启动 - 用.sql 初始化 mysql 脚本失败

[英]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:这就是我所在的位置:

  • I don't want to use JPA(Hibernate) -> No JPA dependency in pom.xml不想使用 JPA(Hibernate) -> 在 pom.xml 中没有 JPA 依赖项
  • Spring Project, mysql are running very well, But No Data in my DB. Spring 项目,mysql 运行良好,但我的数据库中没有数据

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)

  • location: resources/db/migration位置:资源/数据库/迁移
  • DDL file Name: V1__create_testDB.sql (create table query is in here) DDL文件名:V1__create_testDB.sql(这里是创建表查询)
  • DML file Name: V1.1__insert_testDB.sql (insert query is in here) DML文件名称:V1.1__insert_testDB.sql(插入查询在这里)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用RabbitMQ的Spring Boot初始化失败 - Spring Boot with RabbitMQ failed to initialize Spring Boot-无法在WebLogic 12c中初始化ServletRegistrationBean - Spring Boot - Failed to initialize ServletRegistrationBean in WebLogic 12c Spring 引导数据库初始化失败:SimpleDriverDataSource 无法初始化 class - Spring Boot Database initialization failed: SimpleDriverDataSource could not initialize class Spring 使用 JPARepository (MySQL) 启动应用程序无法启动 - Spring Boot App with JPARepository (MySQL) failed to start Spring boot data.sql 不会在 Postgresql 中初始化数据 - Spring boot data.sql does not initialize data in Postgresql Spring Boot 无法运行 data.sql 来初始化数据库 - Spring boot not able to run data.sql to initialize DB Hibernate 延迟加载不适用于 Spring 启动 =&gt; 无法延迟初始化角色集合无法初始化代理 - 没有 Session - Hibernate Lazy loading not working with Spring Boot => failed to lazily initialize a collection of role could not initialize proxy - no Session Spring Boot,无法延迟初始化角色集合,无法初始化代理 - 没有会话 - Spring Boot, Failed to lazily initialize a collection of role, could not initialize proxy - no Session 使用Spring Boot运行脚本时出现SQL错误 - SQL error while running script with spring boot Spring启动没有执行schema.sql脚本 - Spring boot not executing schema.sql script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM