SpringBoot 中JPA集成PostgreSql(详细步骤)避坑!

本文详细介绍了如何在SpringBoot应用中集成PostgreSQL数据库,包括JPA的使用、依赖配置、实体定义、DAO与Service的创建,以及在遇到问题时的解决方案,如确保实体类包含唯一字段和避免null值异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot 中JPA集成PostgreSql(详细步骤)

什么是JPA?(Java Persistence API)

Spring Data JPA, part of the larger Spring Data family, makes it easy to easily implement JPA based repositories. This module deals with enhanced support for JPA based data access layers. It makes it easier to build Spring-powered applications that use data access technologies.

Implementing a data access layer of an application has been cumbersome for quite a while. Too much boilerplate code has to be written to execute simple queries as well as perform pagination, and auditing. Spring Data JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that’s actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.

Features

  • Sophisticated support to build repositories based on Spring and JPA
  • Support for Querydsl predicates and thus type-safe JPA queries
  • Transparent auditing of domain class
  • Pagination support, dynamic query execution, ability to integrate custom data access code
  • Validation of @Query annotated queries at bootstrap time- Support for XML based entity mapping- JavaConfig based repository configuration by introducing @EnableJpaRepositories.

国内说法

JPA 是指 Java Persistence API,即 Java 的持久化规范,一开始是作为 JSR-220 的一部分。 JPA 的提出,主要是为了简化 Java EE 和 Java SE 应用开发工作,统一当时的一些不同的 ORM 技术。 一般来说,规范只是定义了一套运作的规则,也就是接口,而像我们所熟知的Hibernate 则是 JPA 的一个实现(Provider)。

JPA 定义了什么,大致有:

  • ORM 映射元数据,用来将对象与表、字段关联起来- 操作API,即完成增删改查的一套接口- JPQL 查询语言,实现一套可移植的面向对象查询表达式

PostgreSQL简介

PostGreSQL是一个功能强大的开源对象关系数据库管理系统(ORDBMS),号称世界上最先进的开源关系型数据库 经过长达15年以上的积极开发和不断改进,PostGreSQL已在可靠性、稳定性、数据一致性等获得了很大的提升。 对比时下最流行的 MySQL 来说,PostGreSQL 拥有更灵活,更高度兼容标准的一些特性。 此外,PostGreSQL基于MIT开源协议,其开放性极高,这也是其成为各个云计算大T 主要的RDS数据库的根本原因。

JPA中使用PostgreSQL

一、添加依赖包

Spring Boot Mavn项目 Pom.xml文件中添加依赖包

<!--jpa与postgresql-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

二、配置PostgreSQL和jpa

application-dev.yml(开发环境)中添加如下:

server:
  port: 8088
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://192.168.0.30:5432/thingsboard
    username: root
    password: 123456
    hikari:
      minimum-idle: 5
      maximum-pool-size: 15
      auto-commit: true
      idle-timeout: 30000
      connection-timeout: 60000
      validation-timeout: 10000
  jpa:
    show-sql: true
  redis:
    host: 127.0.0.1
    database: 0
    port: 6379
    password:
    lettuce:
      pool:
        max-active: 10
        max-idle: 10
        max-wait: 5ms
tb:
  url: https://nybx-ljyz.cpic.com.cn/aims

logging:
  level: debug
  le
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值