SpringCloud的JPA連接PostgreSql的教程
項(xiàng)目目錄結(jié)構(gòu)
父build.gradle文件如下
spring-cloud-dependencies
和spring-cloud-alibaba-dependencies
之間有版本對(duì)應(yīng)關(guān)系的。 并不是可以隨意搭配的。
具體版本對(duì)應(yīng)關(guān)系參考:
版本關(guān)系
本想使用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相關(guān)配置
:hibernate-types-52 和 commons-lang3 兩個(gè)模塊需要引入。 不然PSql的jsonb 等類型的字段無(wú)法使用
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() }
對(duì)象的定義 基本 對(duì)象的定義:
json 類型的字段, 必須 @Type(type = “jsonb”) @Column(columnDefinition = “jsonb”) 同時(shí)定義。
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; /** * 數(shù)據(jù)庫(kù)一些特殊類型 序列化方式 定義在底層 基類中 */ @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: 是默認(rèn)狀態(tài) * -1: 是默認(rèn)的刪除狀態(tài) */ @Column private Integer status = 0; @Type(type = "jsonb") @Column(columnDefinition = "jsonb") private Map<String, Object> attribute; }
表對(duì)象的定義:
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 { /** * 頁(yè)面的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 對(duì)象
@Repository public interface PageRepository extends CrudRepository<PageDO, Integer> { List<PageDO> findByStatus(Integer status, Pageable pageable); }
數(shù)據(jù)連接配置
## 數(shù)據(jù)庫(kù) 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
數(shù)據(jù)庫(kù)連接池等等
連接池等不用配置, 相關(guān)的包也可以不用引入, 默認(rèn)使用的是 HikariPool
連接池。
后期 我句接入Druid 連接池,并配置完善的監(jiān)控。
以上就是SpringCloud的JPA接入PostgreSql 教程的詳細(xì)內(nèi)容,更多關(guān)于SpringCloud接入PostgreSql 的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
配置springboot項(xiàng)目使用外部tomcat過(guò)程解析
這篇文章主要介紹了配置springboot項(xiàng)目使用外部tomcat過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09Java實(shí)現(xiàn)畫線、矩形、橢圓、字符串功能
本篇文章主要介紹了Java實(shí)現(xiàn)畫線、矩形、橢圓、字符串功能的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Spring?Boot統(tǒng)一處理全局異常的實(shí)戰(zhàn)教程
最近在做項(xiàng)目時(shí)需要對(duì)異常進(jìn)行全局統(tǒng)一處理,所以下面這篇文章主要給大家介紹了關(guān)于Spring?Boot統(tǒng)一處理全局異常的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12IDEA項(xiàng)目啟動(dòng)時(shí)Flyway數(shù)據(jù)庫(kù)遷移中的checksum不匹配問題及最新解決方案
面對(duì)IDEA項(xiàng)目啟動(dòng)時(shí)報(bào)出的Flyway遷移校驗(yàn)和不匹配問題,核心在于保持遷移腳本的一致性、正確管理和理解Flyway的工作機(jī)制,本文介紹IDEA項(xiàng)目啟動(dòng)時(shí)Flyway數(shù)據(jù)庫(kù)遷移中的checksum不匹配問題及最新解決方案,感興趣的朋友一起看看吧2024-01-01Spring Boot ActiveMQ如何設(shè)置訪問密碼
這篇文章主要介紹了Spring Boot ActiveMQ如何設(shè)置訪問密碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07SpringBoot使用開發(fā)環(huán)境application.properties問題
這篇文章主要介紹了SpringBoot使用開發(fā)環(huán)境application.properties問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07關(guān)于QueryWrapper,實(shí)現(xiàn)MybatisPlus多表關(guān)聯(lián)查詢方式
這篇文章主要介紹了關(guān)于QueryWrapper,實(shí)現(xiàn)MybatisPlus多表關(guān)聯(lián)查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01SpringMvc請(qǐng)求處理參數(shù)?和?響應(yīng)數(shù)據(jù)處理的示例詳解
這篇文章主要介紹了SpringMvc請(qǐng)求處理參數(shù)和響應(yīng)數(shù)據(jù)處理,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09