【问题标题】:Can I use Spring Data JPA with PostgreSQL?我可以将 Spring Data JPA 与 PostgreSQL 一起使用吗?
【发布时间】:2021-11-23 06:32:00
【问题描述】:
我希望能够通过 Spring Data JPA 使用 PostgreSQL 数据库。我可以这样做吗?
为了澄清,我希望能够像 CrudRepository 或 JpaRepository 一样使用东西,而不是挂钩到 H2 内存数据库,而是挂钩到 PostgreSQL 数据库。
我正在关注本教程,它使用 H2 数据库:https://spring.io/guides/tutorials/rest/
感谢您的帮助!
【问题讨论】:
标签:
spring
postgresql
spring-data-jpa
spring-data
postgresql-9.5
【解决方案1】:
是的,当然。
您必须执行多个步骤
-
添加postgresql依赖
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
-
在application.properties 或application.yml 中配置数据库详细信息,如下所示;
spring.datasource.url= jdbc:postgresql://<HOST>:<PORT>/<DATABASE>
spring.jpa.database=POSTGRESQL
spring.datasource.username=<USERNAME>
spring.datasource.password=<PASSWORD>
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL94Dialect #Or any other dialect if you're using updated.
-
将 替换为您的 postgres 详细信息