1. 程式人生 > >SpringBoot連線PostgreSql資料庫

SpringBoot連線PostgreSql資料庫

目錄

一、介紹

1、情況說明

2、安裝軟體及依賴包

 二、配置

連線資料庫

其他情況


一、介紹

1、情況說明

在這裡我使用SpringBoot配置Mybaits連線到PostgreSql資料庫的。我的原始碼也會提供給大家(此文末尾),效果如下

資料庫:

執行效果:

2、安裝軟體及依賴包

完整搭建SpringBoot及依賴包:https://blog.csdn.net/qq_41647999/article/details/83214100

需要的SpringBoot程式碼從Spring官網下載:https://start.spring.io/

Mybaits官網:http://www.mybatis.org/mybatis-3/

最關鍵的地方是在依賴包那裡,需要引用Mybaits和PostgreSql的包。

什麼是Mybaits?

說白了和傳統的JDBC一樣,就是個連線資料庫的東西。

更多關於Mybaits,給您推薦一篇文章:https://blog.csdn.net/a909301740/article/details/78698682

將下載的專案解壓出來。

 二、配置

如果您的環境配置和我搭建SpringBoot的博文一樣的話,用eclipse開啟專案。

連線資料庫

//資料庫的地址以及埠號
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
//賬號(預設為postgres)
spring.datasource.username=postgres
//密碼
spring.datasource.password=123456ok
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.hbm2ddl.auto=update

其他情況

當然如果您的專案是按照上面的連結搭建的,也可以通過在pom.xml中新增程式碼,如圖。

這裡需要注意的一點是,要清楚您使用的Mybaits的依賴包是否符合!

        <dependency>
	    <groupId>org.mybatis.spring.boot</groupId>
	    <artifactId>mybatis-spring-boot-starter</artifactId>
	    <version>1.3.2</version>
	</dependency>
		
	<dependency>
	    <groupId>org.postgresql</groupId>
	    <artifactId>postgresql</artifactId>
	    <scope>runtime</scope>
	</dependency>

積分下載地址:https://download.csdn.net/download/qq_41647999/10739833

如果沒有積分,百度雲連結:https://pan.baidu.com/s/15UBAeg68mTIaQOy9ES7_Mw提取碼:1uuo

SpringBoot連線PostgreSql資料庫實現增刪改查,點選這裡。