H2 Console Cant see tables created by JAVA

后端 未结 7 1820
北海茫月
北海茫月 2021-02-02 11:18

I have downloaded the H2 console from http://www.h2database.com/html/download.html
and I have configured the URL in my jdbc.properties file
to jdbc:h2

相关标签:
7条回答
  • 2021-02-02 12:09

    One tricky thing is that the H2 console will not give you an error if you try to connect to a JDBC URL that doesn't exist. It will instead create a new database at that URL! To connect to the in memory DB, use this JDBC URL (http://localhost:8080/h2-console is the default console):

    jdbc:h2:mem:testdb
    

    If you were to enter something like jdbc:h2:~/test then a test.mv file would be created under your home directory. But your application would still be using the in memory database.

    The console is available if you have the h2 dependency in your pom, and also the spring developer tools dependency. If you don't have the tools dependency, then you can also see it by having the h2 dependency and adding the following to your application.properties file:

    spring.h2.console.enabled=true  #not needed if you have spring-boot-devtools dependency
    

    If you want the db as a file, and not in memory, add the following to applications.properties:

    spring.datasource.url=jdbc:h2:~/test_db  #You will see the file in your home directory.
    

    H2 isn't meant for persisted data, but if you want to persist for testing purposes, then add:

    spring.jpa.hibernate.ddl-auto = update
    

    Then start up the app, and at the console, use this JDBC URL:

    jdbc:h2:~/test_db
    

    In case you were wondering, I only have 1 entry in application.properties (for the database file) and here are my dependencies:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
    
    0 讨论(0)
提交回复
热议问题