我正在将Spring Boot和Spring JDBC与Derby一起使用。下面是初始化嵌入式数据库的代码段。
@Bean
public DataSource dataSource() {
// no need shutdown, EmbeddedDatabaseFactoryBean will take care of this
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder
.setType(EmbeddedDatabaseType.DERBY) //.H2 or .DERBY
.addScript("db/sql/create-db.sql")
.addScript("db/sql/insert-data.sql")
.build();
return db;
}
但是当我运行应用程序时,每次都会删除并创建表。因此,刷新了上次运行中插入的所有数据。我不想丢桌子。我该如何实现?
在application.properties/application.yml中设置以下属性
spring.jpa.hibernate.ddl-auto=update
根据Spring文档here
spring.jpa.hibernate.ddl-auto
DDL模式。这实际上是“ hibernate.hbm2ddl.auto”的快捷方式属性。使用嵌入式数据库时默认为“ create-drop”并且没有检测到架构管理器。否则,默认为“无”。
当您使用嵌入式数据库时,默认情况下为create-drop