指定schema.sql和data.sql后,无法使用Spring Boot创建MySQL表并在其中加载初始数据

时间:2019-01-20 05:50:54

标签: mysql spring spring-boot spring-data-jpa

尽管在schema.sql中同时指定了data.sqlsrc/main/resources,但是我运行Spring Boot的{​​{1}}应用程序无法创建表架构并在其中加载数据

在我看来,这似乎是application.properties中的一些属性配置问题,但不确定是哪一个。

请指导。

pom.xml

mvn clean install

src / main / resources / application.properties

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

src / main / resources / schema.sql

spring.datasource.url=jdbc:mysql://localhost:3306/agrisell-db
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.generate-ddl=true

src / main / resources / data.sql

CREATE TABLE state
(
  state_cd           varchar(2)   NOT NULL,
  state_name         varchar(100) NOT NULL,
  create_modified_by varchar(50)  NOT NULL DEFAULT 'admin',
  create_modified_dt datetime     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  active             char(1)      NOT NULL DEFAULT 'Y',
  PRIMARY KEY (state_cd),
  UNIQUE KEY state_name (state_name)
);

1 个答案:

答案 0 :(得分:0)

添加

spring.datasource.initialization-mode=always 
application.propertie

解决了这个问题。

下面列出了完整的application.properties文件。

# ==============================================================
# = Data Source
# ==============================================================
spring.datasource.url=jdbc:mysql://localhost:3306/agrisell-db
spring.datasource.username=root
spring.datasource.password=123456
# ==============================================================
# = Hibernate ddl auto (create, create-drop, update)
# ==============================================================
spring.jpa.hibernate.ddl-auto=none
# ==============================================================
# = The SQL dialect makes Hibernate generate better SQL for the chosen database
# ==============================================================
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
# ==============================================================
# = Initialize the database using data.sql script
# ==============================================================
spring.datasource.initialization-mode=always
# ==============================================================
# = Show or not log for each sql query
# ==============================================================
spring.jpa.show-sql=true
# ==============================================================
# = Keep the connection alive if idle for a long time (needed in production)
# ==============================================================
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1