SpringCloud的JPA連接PostgreSql的教程
項目目錄結構
父build.gradle文件如下
spring-cloud-dependencies
和spring-cloud-alibaba-dependencies
之間有版本對應關系的。 並不是可以隨意搭配的。
具體版本對應關系參考:
版本關系
本想使用WebFlux模塊的,奈何openfeign 不支持。
buildscript { ext { springBootVersion = '2.1.13.RELEASE' springBootManagementVersion = '1.0.8.RELEASE' } repositories { mavenLocal() maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } mavenCentral() maven { url 'https://repo.spring.io/snapshot' } maven { url 'https://repo.spring.io/milestone' } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") classpath("io.spring.gradle:dependency-management-plugin:${springBootManagementVersion}") } } subprojects { apply plugin: "idea" apply plugin: "java" apply plugin: 'org.springframework.boot' apply plugin: "io.spring.dependency-management" sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 group "xyz.xiezc.mzix" version "1.0.0" repositories { mavenLocal() maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } mavenCentral() maven { url 'https://repo.spring.io/snapshot' } maven { url 'https://repo.spring.io/milestone' } } dependencies{ compile group: 'cn.hutool', name: 'hutool-all', version: '5.6.6' compileOnly "org.projectlombok:lombok:1.18.20" compile 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config' compile 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery' compile 'org.springframework.boot:spring-boot-starter-actuator' compile 'org.springframework.boot:spring-boot-starter-aop' compile 'org.springframework.cloud:spring-cloud-starter-openfeign' annotationProcessor("org.projectlombok:lombok:1.18.20") } dependencyManagement { imports { mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Greenwich.SR6' mavenBom "com.alibaba.cloud:spring-cloud-alibaba-dependencies:2.1.4.RELEASE" } } } repositories { mavenLocal() maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } mavenCentral() maven { url 'https://repo.spring.io/snapshot' } maven { url 'https://repo.spring.io/milestone' } }
Schedule模塊引入JPA相關配置
:hibernate-types-52 和 commons-lang3 兩個模塊需要引入。 不然PSql的jsonb 等類型的字段無法使用
dependencies { implementation project(":common") compile('org.springframework.boot:spring-boot-starter-data-redis-reactive') compile('org.springframework.boot:spring-boot-starter-web') implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.20' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.apache.commons:commons-lang3:3.12.0' implementation 'com.vladmihalcea:hibernate-types-52:2.10.3' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' } test { useJUnitPlatform() }
對象的定義 基本 對象的定義:
json 類型的字段, 必須 @Type(type = “jsonb”) @Column(columnDefinition = “jsonb”) 同時定義。
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import com.vladmihalcea.hibernate.type.array.IntArrayType; import com.vladmihalcea.hibernate.type.array.StringArrayType; import com.vladmihalcea.hibernate.type.json.JsonBinaryType; import com.vladmihalcea.hibernate.type.json.JsonNodeBinaryType; import com.vladmihalcea.hibernate.type.json.JsonNodeStringType; import com.vladmihalcea.hibernate.type.json.JsonStringType; import lombok.Data; import org.hibernate.annotations.*; import javax.persistence.Column; import javax.persistence.MappedSuperclass; import java.time.LocalDateTime; import java.util.Map; /** * 數據庫一些特殊類型 序列化方式 定義在底層 基類中 */ @TypeDefs({ @TypeDef(name = "string-array", typeClass = StringArrayType.class), @TypeDef(name = "int-array", typeClass = IntArrayType.class), @TypeDef(name = "json", typeClass = JsonStringType.class), @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class), @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class), @TypeDef(name = "json-node", typeClass = JsonNodeStringType.class), }) @Data @MappedSuperclass public class BaseEntity { @Column @CreationTimestamp private LocalDateTime createTime; @Column @UpdateTimestamp private LocalDateTime updateTime; /** * 0: 是默認狀態 * -1: 是默認的刪除狀態 */ @Column private Integer status = 0; @Type(type = "jsonb") @Column(columnDefinition = "jsonb") private Map<String, Object> attribute; }
表對象的定義:
Psql的schema 和 mysql的schema的略有不同。 @Table中必須指定schema
import lombok.Data; import lombok.EqualsAndHashCode; import org.hibernate.annotations.Type; import javax.persistence.*; import java.util.Map; @Data @EqualsAndHashCode(callSuper = true) @Entity @Table(name = "t_page", schema = "xiezc") public class PageDO extends BaseEntity { /** * 頁面的id */ @Id @GeneratedValue Long id; @Column String code; @Column(unique = true) String url; @Column String contentType; @Type(type = "jsonb") @Column(columnDefinition = "jsonb") Map<String, Object> request; @Type(type = "jsonb") @Column(columnDefinition = "jsonb") Map<String, Object> response; }
Repository 對象
@Repository public interface PageRepository extends CrudRepository<PageDO, Integer> { List<PageDO> findByStatus(Integer status, Pageable pageable); }
數據連接配置
## 數據庫 spring.datasource.url=jdbc:postgresql://psotgres:5432/postgres spring.datasource.username=xiezc spring.datasource.password=1234567 spring.datasource.driverClassName=org.postgresql.Driver spring.jpa.generate-ddl=true spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update
數據庫連接池等等
連接池等不用配置, 相關的包也可以不用引入, 默認使用的是 HikariPool
連接池。
後期 我句接入Druid 連接池,並配置完善的監控。
以上就是SpringCloud的JPA接入PostgreSql 教程的詳細內容,更多關於SpringCloud接入PostgreSql 的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- SpringBoot零基礎入門之基本操作與概念
- Spring Boot四大神器之CLI的具體使用
- JPA如何設置表名和實體名,表字段與實體字段的對應
- 用Spring Native將SpringBoot程序轉換為GraalVM
- Gradle的基本使用