如何配置spring-boot使用基于文件的H2数据库

10 浏览
0 Comments

如何配置spring-boot使用基于文件的H2数据库

我已经成功创建了一个使用H2嵌入式内存数据库的Spring Boot应用程序。现在我想将其更改为基于文件的持久化版本。我尝试在我的`application.properties`文件中更改`spring.datasource.*`属性,如下所示:\n

spring.datasource.url=jdbc:h2:file:~/test;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driverClassName=org.h2.Driver

\n但是,似乎Spring Boot忽略了这些设置,因为它启动时显示如下内容:\n

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'

\n我在`pom.xml`中包含了以下与此帖子相关的依赖项:\n\n org.springframework.boot\n spring-boot-starter-parent\n 1.3.5.RELEASE\n\n....\n\n org.springframework.boot\n spring-boot-starter-web\n \n\n com.h2database\n h2\n\n\n org.springframework.boot\n spring-boot-devtools\n\n\n根据文档和一些帖子的理解,配置应该正常工作,但对我来说运气不好。为了防止一些基本错误,我尝试并检查了以下内容:\n1. 我的应用程序属性是否在类路径下。\n2. 我尝试在`@EnableAutoConfiguration`注解中排除自动配置。\n3. 我尝试使用注解`@Primary`、`@ConfigurationProperties(prefix = \"spring.datasource\")`和使用`DataSourceBuilder`以编程方式设置属性来注入`dataSource` bean。这会导致与类型为`null`相关的其他错误。\n看起来我漏掉了一些关键概念。有人可以帮忙吗?\n更新1:从我的自动配置报告中提取的信息如下:\n

Positive matches:
-----------------
    DataSourceAutoConfiguration matched
  - @ConditionalOnClass classes found: javax.sql.DataSource,org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType (OnClassCondition)
   DataSourceAutoConfiguration.DataSourceInitializerConfiguration matched
  - @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer; SearchStrategy: all) found no beans (OnBeanCondition)
   DataSourceAutoConfiguration.EmbeddedConfiguration matched
  - embedded database H2 detected (DataSourceAutoConfiguration.EmbeddedDataSourceCondition)
  - @ConditionalOnMissingBean (types: javax.sql.DataSource,javax.sql.XADataSource; SearchStrategy: all) found no beans (OnBeanCondition)
   DataSourceAutoConfiguration.JdbcTemplateConfiguration matched
  - existing auto database detected (DataSourceAutoConfiguration.DataSourceAvailableCondition)
   DataSourceAutoConfiguration.JdbcTemplateConfiguration#jdbcTemplate matched
  - @ConditionalOnMissingBean (types: org.springframework.jdbc.core.JdbcOperations; SearchStrategy: all) found no beans (OnBeanCondition)
   DataSourceAutoConfiguration.JdbcTemplateConfiguration#namedParameterJdbcTemplate matched
  - @ConditionalOnMissingBean (types: org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; SearchStrategy: all) found no beans (OnBeanCondition)
   DataSourceTransactionManagerAutoConfiguration matched
  - @ConditionalOnClass classes found: org.springframework.jdbc.core.JdbcTemplate,org.springframework.transaction.PlatformTransactionManager (OnClassCondition)
   DataSourceTransactionManagerAutoConfiguration.TransactionManagementConfiguration matched
  - @ConditionalOnMissingBean (types: org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration; SearchStrategy: all) found no beans (OnBeanCondition)
    H2ConsoleAutoConfiguration matched
  - @ConditionalOnClass classes found: org.h2.server.web.WebServlet (OnClassCondition)
  - found web application StandardServletEnvironment (OnWebApplicationCondition)
  - matched (OnPropertyCondition)
   HibernateJpaAutoConfiguration matched
  - @ConditionalOnClass classes found: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean,org.springframework.transaction.annotation.EnableTransactionManagement,javax.persistence.EntityManager (OnClassCondition)
  - found HibernateEntityManager class (HibernateJpaAutoConfiguration.HibernateEntityManagerCondition)
Negative matches:
-----------------
    DataSourceAutoConfiguration.NonEmbeddedConfiguration did not match
  - missing supported DataSource (DataSourceAutoConfiguration.NonEmbeddedDataSourceCondition)

\n更新2:添加了Actuator并查看了端点`/configprops`。有趣的是,我的配置已被采用,数据库也存在,但当应用程序运行时它不使用这个`dataSource`。

0
0 Comments

如何配置spring-boot使用基于文件的H2数据库

问题的原因:在重启SpringBoot或计算机后,想要保留数据的持久化。

解决方法:

1. 在application.properties文件中添加以下设置:

spring.datasource.name=japodb
spring.datasource.initialize=false
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:file:~/japodb;DB_CLOSE_ON_EXIT=FALSE;IFEXISTS=TRUE;DB_CLOSE_DELAY=-1;

2. 在url中添加IFEXISTS=TRUE,这样如果数据库已经存在,不会创建新的数据库。

jdbc:h2:;IFEXISTS=TRUE
spring.jpa.hibernate.ddl-auto = update

3. 在pom.xml文件中添加spring-boot-starter-jdbc依赖。

其他用户的反馈:

- 有用户尝试了以上方法,但没有生效。

- 对于某些用户来说,“spring.jpa.hibernate.ddl-auto = update”解决了问题。他们尝试了其他所有提示,但都没有成功。

以上就是如何配置spring-boot使用基于文件的H2数据库的解决方法。

0
0 Comments

问题的原因是配置的jdbc.url不正确,解决方法是修改jdbc.url为正确的格式。具体的解决方法如下:

参考http://www.h2database.com/html/cheatSheet.html

我猜问题可能是jdbc.url的问题,将其修改为以下格式:

# 从:
spring.datasource.url=jdbc:h2:file:~/test;DB_CLOSE_ON_EXIT=FALSE
# 改为:
spring.datasource.url=jdbc:h2:~/test;DB_CLOSE_ON_EXIT=FALSE

谢谢,但实际上这个配置创建了一个运行中的数据库实例并使用了正确的url(如上面的更新2中配置)。问题是应用程序没有使用它,似乎是使用了默认的'EmbeddedDatabase'。

你是否在pom.xml中添加了spring-boot-starter-jdbc依赖?这里有一个示例项目:github.com/lenicliu/eg-spring/tree/master/eg-spring-boot/…,其中使用flyway进行数据库迁移,使用h2database作为嵌入式文件模式的数据库。

将spring-boot-starter-jdbc添加到pom.xml中可以解决问题!谢谢。希望知道为什么这样修复了它。

0
0 Comments

问题的原因是作者遇到了在Spring Boot中配置基于文件的H2数据库的问题,并且之前的解答都没有完全解决他的问题。解决方法是在application.properties文件中添加以下最小配置:

# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:file:~/spring-boot-h2-db
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=update

其中,`spring.jpa.hibernate.ddl-auto=update`是关键。不需要在pom.xml中添加`spring-boot-starter-jdbc`,也不需要在jdbc url中添加任何参数。

此配置应该被接受为答案。只需要添加`spring.jpa.hibernate.ddl-auto=update`就可以使其正常工作。

如果你在`data.sql`文件中有一个初始化脚本,并且需要在应用程序启动时实例化数据库,还需要在`application.properties`中添加以下配置:`spring.sql.init.mode=always`。这个信息来自baeldung的文章,如果你需要官方文档,可以查看这个链接:[link](https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.data-initialization)

0