Spring Boot:如何连接到外部h2数据库

huangapple go评论89阅读模式
英文:

Spring Boot: how to connect to external h2 database

问题

我已经创建了一个Spring Boot应用程序,并且没有使用内存中的H2数据库,而是安装了H2数据库的可执行文件并在外部使用它。现在我想要将我的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

(代码部分不进行翻译。)

英文:

I have created one Spring boot application and I am not using in-memory H2 database, instead I have installed exe for H2 database and using it externally. Now I want to connect my Spring boot app with this external H2 database. I have added the dependencies, I have added all the required properties in application.properties file (you can see below). Also I have created one Entity class having @Entity annotation. But when I am trying to connect with the database it is connecting even with different URL and username and I cannot see my Entity class table over there. So where am I doing a mistake and what are the things which I have missed, please address me on this.

For your information, my Spring boot app is running on port - 8080 and H2 database is running on 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

答案1

得分: 1

你可以尝试这样做:

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

注意:我会保留代码部分的原文,只翻译非代码部分。

英文:

You can try this

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

答案2

得分: 0

在属性文件中提供

# H2
spring.datasource.url=<你的值>
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
英文:

Give in properties file

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

答案3

得分: 0

这是教程链接:

https://howtodoinjava.com/spring-boot2/h2-database-example/
https://www.baeldung.com/spring-boot-h2-database

英文:

Check this tutorial below link

https://howtodoinjava.com/spring-boot2/h2-database-example/
https://www.baeldung.com/spring-boot-h2-database

huangapple
  • 本文由 发表于 2020年9月19日 20:19:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63968714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定