spring boot连接postgresql
时间: 2023-08-01 21:11:02 浏览: 62
在Spring Boot中连接PostgreSQL有两种方式可以选择。第一种是使用JPA方式,需要在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
```
然后在application.yml文件中配置数据源和JPA相关的属性,例如:
```yaml
spring:
application:
name: shipcenter
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://xxxxx:5432/leadtrans
username: xxx
password: xxx
jpa:
generate-ddl: false
hibernate:
ddl-auto: none
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
database: postgresql
show-sql: false
```
第二种方式是使用MyBatis-Plus,需要在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
```
然后在application.yml文件中配置数据源和MyBatis-Plus相关的属性,例如:
```yaml
spring:
application:
name: shipcenter
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://xxxxx:5432/leadtrans
username: xxxx
password: xxxx
mybatis-plus:
configuration:
map-underscore-to-camel-case: false
global-config:
db-config:
update-strategy: ignored
```
以上是两种连接PostgreSQL的方式,你可以根据自己的需求选择其中一种来配置连接。
#### 引用[.reference_title]
- *1* *2* [springboot 连接 postgresql](https://blog.csdn.net/hanjun0612/article/details/124979184)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [IDEA spring boot 连接Postgresql配置 【已解决】](https://blog.csdn.net/qq_40651609/article/details/88366664)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]