【问题标题】:Spring Boot: how to connect to external h2 databaseSpring Boot:如何连接到外部 h2 数据库
【发布时间】:2021-01-06 03:47:29
【问题描述】:

我创建了一个 Spring Boot 应用程序,但我没有使用内存中的 H2 数据库,而是为 H2 数据库安装了 exe 并在外部使用它。现在我想将我的 Spring Boot 应用程序与这个外部 H2 数据库连接起来。我已经添加了依赖项,我已经在 application.properties 文件 中添加了所有必需的属性(您可以在下面看到)。我还创建了一个具有@Entity 注释的实体类。但是,当我尝试连接数据库时,即使使用不同的 URL 和用户名,它也会连接,并且我看不到那里的实体类表。那么我在哪里做错了,我错过了哪些事情,请告诉我。

供您参考,我的 Spring boot 应用程序在端口 - 8080 上运行,H2 数据库在 8082 上运行

 spring.h2.driverClassName = org.h2.Driver

 spring.h2.url = jdbc:h2:file:~/test2;DB_CLOSE_ON_EXIT=FALSE; AUTO_RECONNECT=TRUE

 spring.h2.username = QW

 spring.h2.password = root

 spring.h2.console.enabled = true

 spring.datasource.platform = h2

【问题讨论】:

    标签: java spring-boot h2


    【解决方案1】:
    【解决方案2】:

    你可以试试这个

    spring.datasource.url=jdbc:h2:tcp://localhost:8082/~/<DB-NAME>
    spring.datasource.username=<username>
    spring.datasource.password=<password>
    spring.datasource.driverClassName=org.h2.Driver
    

    【讨论】:

      【解决方案3】:

      在属性文件中给出

      # H2
      spring.datasource.url=<your value
      spring.datasource.username=username
      spring.datasource.password=password
      spring.datasource.driverClassName=org.h2.Driver
      spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
      

      【讨论】:

        猜你喜欢
        • 2019-12-26
        • 2020-09-03
        • 2020-08-12
        • 2018-12-21
        • 2021-09-17
        • 1970-01-01
        • 2018-10-10
        • 2020-01-01
        • 1970-01-01
        相关资源
        最近更新 更多