我的 Spring 数据库 (H2) 未持久化
My Spring Database (H2) is not Persisting
这是我的配置文件
@Configuration
@ComponentScan
public class Config {
@Bean
public DataSource datasource() {
return new EmbeddedDatabaseBuilder().setName("MyDB").setType(EmbeddedDatabaseType.H2).addScript("schema.sql").build();
}
@Bean
public JdbcOperations jdbcTemplate(DataSource ds) {
return new JdbcTemplate(ds);
}
}
我运行程序后,找不到"MyDB"数据库。
我知道这是一个内存数据库。如何使其嵌入,以便当我关闭程序时,数据库中的数据仍然存在,我可以在项目文件夹中找到 "MyDB"。
@Bean
public DataSource h2DataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
driver: org.h2.Driver
url: jdbc:h2:file:${java.io.tmpdir}/database/db_name;AUTO_SERVER=TRUE
username: user
password: pass
这是我的配置文件
@Configuration
@ComponentScan
public class Config {
@Bean
public DataSource datasource() {
return new EmbeddedDatabaseBuilder().setName("MyDB").setType(EmbeddedDatabaseType.H2).addScript("schema.sql").build();
}
@Bean
public JdbcOperations jdbcTemplate(DataSource ds) {
return new JdbcTemplate(ds);
}
}
我运行程序后,找不到"MyDB"数据库。
我知道这是一个内存数据库。如何使其嵌入,以便当我关闭程序时,数据库中的数据仍然存在,我可以在项目文件夹中找到 "MyDB"。
@Bean
public DataSource h2DataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
driver: org.h2.Driver
url: jdbc:h2:file:${java.io.tmpdir}/database/db_name;AUTO_SERVER=TRUE
username: user
password: pass