引出
1.PostgreSQL數(shù)據(jù)庫初始,開源;
2.Docker版本的postgres安裝,以及掛載啟動(dòng);
3.Navicat連接方式+導(dǎo)入向?qū)褂?,?dǎo)入csv文件;
4.SpringBoot結(jié)合Jpa使用PostgreSQL初步;
postgres數(shù)據(jù)庫
PostgreSQL是一種開源的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)(RDBMS),它是一種高度可擴(kuò)展的、可靠的、功能豐富的數(shù)據(jù)庫系統(tǒng)。以下是關(guān)于PostgreSQL的一些介紹:
開源性:PostgreSQL是一個(gè)開源項(xiàng)目,可以免費(fèi)使用和修改。它的源代碼可以被任何人查看、修改和貢獻(xiàn)。
關(guān)系型數(shù)據(jù)庫:PostgreSQL是一種關(guān)系型數(shù)據(jù)庫,它使用表格來存儲(chǔ)和組織數(shù)據(jù)。它支持SQL查詢語言,可以進(jìn)行復(fù)雜的數(shù)據(jù)操作和查詢。
可擴(kuò)展性:PostgreSQL具有良好的可擴(kuò)展性,可以處理大量的數(shù)據(jù)和高并發(fā)訪問。它支持水平擴(kuò)展和垂直擴(kuò)展,可以通過添加更多的服務(wù)器節(jié)點(diǎn)或增加硬件資源來提高性能。
可靠性:PostgreSQL具有高度可靠性和數(shù)據(jù)完整性。它支持事務(wù)處理和ACID(原子性、一致性、隔離性和持久性)特性,可以確保數(shù)據(jù)的一致性和可靠性。
功能豐富:PostgreSQL提供了許多高級(jí)功能,如復(fù)雜查詢、索引、觸發(fā)器、視圖、存儲(chǔ)過程、外鍵約束等。它還支持全文搜索、地理空間數(shù)據(jù)處理、JSON數(shù)據(jù)類型等先進(jìn)的功能。
跨平臺(tái)性:PostgreSQL可以在多種操作系統(tǒng)上運(yùn)行,包括Windows、Linux、Mac OS等。
Docker版本的postgres安裝
docker search postgres
docker pull postgres
運(yùn)行創(chuàng)建容器
docker run -it \
--name postgres \
--privileged \
-e POSTGRES_PASSWORD=123 \
-p 5432:5432 \
-v /usr/local/software/postgres/data:/var/lib/postgresql/data \
-d postgres
docker run --name postgres -e POSTGRES_PASSWORD=Abc1234% -p 5432:5432 -v /home/zx/postgres/data:/var/lib/postgresql/data -d postgres
- –name postgres:指定容器的名稱;
- -e POSTGRES_PASSWORD=Abc1234%:設(shè)置環(huán)境變量,這里為設(shè)定PostgreSQL數(shù)據(jù)庫的密碼;
- -p 5432:5432:指定端口映射,前者為宿主機(jī)訪問端口,后者為容器內(nèi)端口。如果不指定端口映射,只有在容器內(nèi)可以訪問數(shù)據(jù)庫,外部是無法訪問的;
- -v /home/zx/postgres/data:/var/lib/postgresql/data:v是volume的簡(jiǎn)寫,即綁定一個(gè)卷,冒號(hào)前的路徑為宿主機(jī)的路徑(如果指定路徑不存在會(huì)自動(dòng)創(chuàng)建目錄),冒號(hào)后為容器內(nèi)路徑。容器會(huì)把宿主機(jī)的目錄映射到容器內(nèi)的路徑,這樣容器運(yùn)行中生成的數(shù)據(jù)實(shí)際上就是寫到了宿主機(jī)的指定路徑上,即使容器刪除了,此路徑的文件也不會(huì)刪除,這樣就做到了數(shù)據(jù)庫的持久化存儲(chǔ)。還可以通過docker volume提供的相關(guān)命令顯式地創(chuàng)建volume,然后再掛載到容器上,這里不做介紹,請(qǐng)自行查詢相關(guān)內(nèi)容;
- -d:表示后臺(tái)運(yùn)行容器;
- postgres:表示鏡像的名稱,docker基于上步拉取的鏡像創(chuàng)建出容器;
查看日志
docker logs postgres
容器內(nèi)連接數(shù)據(jù)庫
docker exec -it postgres /bin/bash
- exec:在容器中運(yùn)行命令
- postgres:為容器名稱
- -it:表示在當(dāng)前終端為容器分配一個(gè)偽終端
- /bin/bash:容器內(nèi)運(yùn)行/bin/bash終端
[root@localhost data]# docker exec -it postgres bash
root@3283f4dbe299:/# su postgres
postgres@3283f4dbe299:/$ psql
psql (14.1 (Debian 14.1-1.pgdg110+1))
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=#
Navicat連接數(shù)據(jù)庫
創(chuàng)建連接
新建數(shù)據(jù)庫
編碼方式UTF-8
新建模式
輸入模式名
導(dǎo)入向?qū)?/h4>
選擇csv文件
LF (Line Feed):換行符,’\n’,表示切換到下一行,ASCII碼為10
CR (Carriage Return):回車符,’\r’,表示回到當(dāng)前行的開頭,ASCII碼為13
CRLF:一個(gè)回車符和一個(gè)換行符,“\r\n”
-
Windows系統(tǒng)
使用 CRLF 進(jìn)行換行 -
Unix系統(tǒng)
及類Unix系統(tǒng)
采用 LF 進(jìn)行換行
關(guān)于分隔符
日期排序方式
支持修改表名
選擇字段,修改導(dǎo)入后字段名字
導(dǎo)入模式
開始導(dǎo)入
SpringBoot結(jié)合Jpa使用postgres初步
導(dǎo)入依賴
<!-- Jpa的包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- postgresql數(shù)據(jù)庫-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
創(chuàng)建實(shí)體類
javax
-
@Entity
@Table
- @Id
- @GeneratedValue(strategy) AUTO/IDENTITY(數(shù)據(jù)庫自己的主鍵自增長(zhǎng)策略???)
- @Column
GenerationType.AUTO:會(huì)多一張表,記錄鍵
GenerationType.IDENTITY:用數(shù)據(jù)庫自增的主鍵
@GeneratedValue(strategy = GenerationType.IDENTITY) // 用數(shù)據(jù)庫自增長(zhǎng)策略
package com.tianju.book.jpa;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Data
@NoArgsConstructor
@AllArgsConstructor
// 表示這個(gè)實(shí)體類是和數(shù)據(jù)庫表對(duì)應(yīng)的
@Entity
@Table(name = "identity_card") // 對(duì)應(yīng)的表名
public class Identity {
@Id // 是主鍵
// GenerationType.AUTO:會(huì)多一張表,記錄鍵
// GenerationType.IDENTITY:用數(shù)據(jù)庫自增的主鍵
@GeneratedValue(strategy = GenerationType.IDENTITY) // 用數(shù)據(jù)庫自增長(zhǎng)策略
@Column(name = "id")
private String id;
@Column(name = "distinct")
private String distinct;
}
dao根據(jù)區(qū)域模糊查詢
JpaRepository<Identity,String>:實(shí)體類以及主鍵的類型
package com.tianju.book.jpa;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* JpaRepository<Identity,String>:實(shí)體類以及主鍵的類型
*/
@Repository
public interface IdentityDao extends JpaRepository<Identity,String> {
List<Identity> findByDistinctLike(String distinct);
}
配置類
在jpa中,ddl-auto共分為四種:
spring.jpa.hibernate.ddl-auto = create ----每次啟動(dòng)SpringBoot程序時(shí),沒有表會(huì)新建表格,表內(nèi)有數(shù)據(jù)會(huì)清空;
spring.jpa.hibernate.ddl-auto = create-drop ----每次啟動(dòng)SpringBoot程序時(shí),會(huì)清空表數(shù)據(jù);
spring.jpa.hibernate.ddl-auto = update ---- 每次啟動(dòng)SpringBoot程序時(shí),沒有表格會(huì)新建表格,表內(nèi)有數(shù)據(jù)不會(huì)清空,只會(huì)更新;
spring.jpa.hibernate.ddl-auto = validate ---- 每次啟動(dòng)SpringBoot程序時(shí),會(huì)校驗(yàn)實(shí)體類字段與數(shù)據(jù)庫字段的類型是否相同,不同則會(huì)報(bào)錯(cuò);
server:
port: 9099
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://192.168.111.130:5432/test-db?useSSL=false
username: postgres
password: 123
jpa:
# 允許顯示sql
show-sql: true
hibernate:
# 自動(dòng)對(duì)表進(jìn)行增刪改查的操作,創(chuàng)建表
# 可以開始的時(shí)候打開,等表創(chuàng)建好之后關(guān)閉
ddl-auto: validate
使用其他模式
在Spring Boot的application.yml文件中,可以使用以下方式指定連接PostgreSQL數(shù)據(jù)庫中的模式:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/database_name?currentSchema=schema_name
username: your_username
password: your_password
在上述配置中,database_name
是要連接的數(shù)據(jù)庫名稱,schema_name
是要使用的模式名稱。通過在URL中添加currentSchema=schema_name
參數(shù),可以指定要使用的模式。
另外,如果要在代碼中指定模式,可以使用spring.jpa.properties.hibernate.default_schema
屬性,如下所示:
spring:
jpa:
properties:
hibernate:
default_schema: schema_name
這樣,Hibernate將使用指定的模式來創(chuàng)建和查詢數(shù)據(jù)庫對(duì)象。
進(jìn)行測(cè)試
文章來源:http://www.zghlxwxcb.cn/news/detail-758059.html
package com.tianju.book.mapper;
import com.tianju.book.jpa.Identity;
import com.tianju.book.jpa.IdentityDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class JpaDemoTest {
@Autowired
private IdentityDao identityDao;
@Test
public void findByDistinctLike(){
List<Identity> distinctLike = identityDao.findByDistinctLike("北京%");
distinctLike.forEach(identity->{
System.out.println(identity);
});
}
}
總結(jié)
1.PostgreSQL數(shù)據(jù)庫初始,開源;
2.Docker版本的postgres安裝,以及掛載啟動(dòng);
3.Navicat連接方式+導(dǎo)入向?qū)褂?,?dǎo)入csv文件;
4.SpringBoot結(jié)合Jpa使用PostgreSQL初步;文章來源地址http://www.zghlxwxcb.cn/news/detail-758059.html
到了這里,關(guān)于PostgreSQL數(shù)據(jù)庫——Docker版本的postgres安裝 & Navicat連接方式+導(dǎo)入向?qū)褂?& SpringBoot結(jié)合Jpa使用PostgreSQL初步的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!