Spring Boot数据初始化

时间:2017-09-06 03:58:18

标签: spring spring-boot

我正在尝试在Spring Boot应用程序启动后插入一些测试数据。我有以下配置。我注意到在创建JPA实体之前执行sql脚本导致insert语句失败。我在这里缺少任何其他配置吗?如我的配置中所述,我使用Spring Boot with H2和EclipseLink for JPA。

参考 https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html

配置

# H2
spring.h2.console.enabled=true
# Datasource
spring.datasource.url=jdbc:h2:mem:testdb;MODE=ORACLE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
# JPA
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.properties.eclipselink.weaving=false

data.sql 位于/ src / main / resources

日志

2017-09-05 20:37:37,989 [restartedMain  ] INFO  
o.s.j.d.e.EmbeddedDatabaseFactory         - Starting embedded database: 
url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', 
username='sa'
2017-09-05 20:37:38,141 [restartedMain  ] INFO  
j.LocalContainerEntityManagerFactoryBean  - Building JPA container 
EntityManagerFactory for persistence unit 'default'
2017-09-05 20:37:38,446 [restartedMain  ] INFO  
o.s.o.j.AbstractEntityManagerFactoryBean  - Initialized JPA 
EntityManagerFactory for persistence unit 'default'
2017-09-05 20:37:38,510 [restartedMain  ] INFO  o.s.j.d.i.ScriptUtils                     
- Executing SQL script from URL  [file:/develop/src/data-init/target/classes/data.sql]
error...

实体

@Entity
@Table(name = "TEMPLATE_CONFIG")
@Data
public class TemplateUser {

    @Id
    @Column(name = "TEMPLATE_CONFIG_ID", nullable = false)
    private Long configId;

    @Column(name = "APP_CODE", nullable = false, length = 100)
    private String appCode;
    ...
}

更新

上传了一个示例项目: https://git.io/v5SWx

2 个答案:

答案 0 :(得分:0)

Spring-Boot加载data.sql或数据 - $ {somthing} .sql

你可以这样做:

  • spring.datasource.platform = H2。
  • spring.datasource.data =的MyScript-TEST.SQL。 (可以使用此属性更改位置)

我有一个项目,正在使用h2。

如果您无法加载信息,最后的替代方案可能是您可以在应用程序文件中使用带有jdbc模板的CommandLineRunner。

答案 1 :(得分:0)

这是Spring中现有的错误,正如Spring团队在此确认的那样。

https://github.com/spring-projects/spring-boot/issues/10365

相关问题