项目目录结构
父build.gradle文件如下
spring-cloud-dependencies 和 spring-cloud-alibaba-dependencies 之间有版本对应关系的。 并不是可以随意搭配的。
具体版本对应关系参考:
版本关系
本想使用WebFlux模块的,奈何openfeign 不支持。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
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 等类型的字段无法使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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]) 同时定义。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
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 对象
1 2 3 4 5 6 |
@Repository public interface PageRepository extends CrudRepository<PageDO, Integer> {
List<PageDO> findByStatus(Integer status, Pageable pageable);
} |
数据连接配置
1 2 3 4 5 6 7 8 |
## 数据库 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 的资料请关注其它相关文章!
原文链接:https://blog.csdn.net/leisurelen/article/details/118198277
查看更多关于SpringCloud的JPA连接PostgreSql的教程的详细内容...