繁体   English   中英

Spring 引导未运行架构。sql

[英]Spring Boot not running schema.sql

我在运行程序时遇到了运行 schema.sql 的问题。

在我的 pom.xml 中,我已经有了 mysql 配置:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

我只有一个 class

@SpringBootApplication
public class Application {...}

在 src/resources 下,我有 schema.sql 包含:

DROP TABLE IF EXISTS test_table;

CREATE TABLE test_table (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    ...
);

在 src/resources 下,我有 application.yml 包含:

spring.datasource.driverClassName: "com.mysql.jdbc.Driver"
spring.datasource.url: "jdbc:mysql://localhost:3306/sample?useSSL=false"
spring.datasource.username: "****"
spring.datasource.password: "****"

我已经确认我能够在启动应用程序时连接到数据库“示例”,但是,它没有创建表。 请指教。

在我的情况下(Spring Boot 2.0.0+),仅当属性设置spring.datasource.initialization-mode=alwaysspring.jpa.hibernate.ddl-auto=none结合使用时,它才能按预期工作。

这是因为Spring Boot已签入DataSourceInitializer's initSchema()方法。

在此处输入图片说明

仅当您的数据库为H2,DERBY,HSQL类型时H2,DERBY,HSQL它才会执行脚本

但是,您可以通过使用application.properties中的以下设置来覆盖此行为

spring.datasource.initialization-mode=always

DataSourceInitializer.java EmbeddedDatabaseConnection.java

从 spring 启动版本 2.7 开始,这将起作用:

spring.sql.init.mode=always
spring.jpa.hibernate.ddl-auto=update

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM