How to configure spring-boot to use file based H2 database

后端 未结 5 617
闹比i
闹比i 2020-12-23 10:51

I have successfully created a spring boot application that uses the H2 embedded database in-memory. I would now like to change this to a file based version that will persist

5条回答
  •  隐瞒了意图╮
    2020-12-23 11:33

    I am adding this answer to avoid confusion and further research.

    Actually I have the same problem and none of the answer worked for me completely rather than the mix for some answers worked.

    Here is the minimal configuration which is required to persist H2 db in spring boot.

    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
    

    Here spring.jpa.hibernate.ddl-auto=update does the trick. Nothing else is required.

    No need to add spring-boot-starter-jdbc in pom.xml

    No need to add any parameter in jdbc url.

提交回复
热议问题