首页 > 解决方案 > data.sql 不会执行

问题描述

我正在尝试初始化数据库并使用 spring boot 插入一些数据。根据文档,我只需要资源文件夹中的 data.sql 文件来执行我的 sql 初始数据,但由于某种原因,我的 data.sql 被忽略了。

这是我的 application.properties 文件:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/urmamma
spring.datasource.username=root
spring.datasource.password=
spring.session.jdbc.initialize-schema=always
#spring.data.jpa.repositories.bootstrap-mode=default
#spring.datasource.data=classpath:data.sql

我的 data.sql 文件:

insert into user (id, email, name) values (1, '123@gmail.com', 'Teobaldo')

使用 gradle 的依赖项:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.data:spring-data-rest-hal-explorer'
    implementation 'org.springframework.session:spring-session-jdbc'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'mysql:mysql-connector-java'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
}

标签: sqlspringspring-boot

解决方案


我在这里错过了这个配置:

spring.datasource.initialization-mode=always

在阅读文档时,我把它误认为是财产

spring.session.jdbc.initialize-schema=always

我已经有了。


推荐阅读