DEV Community

Cover image for Spring Boot + PostgreSQL + Maven: CRUD example
Tien Nguyen
Tien Nguyen

Posted on • Edited on

Spring Boot + PostgreSQL + Maven: CRUD example

In this tutorial, we're gonna build a Spring Boot + PostgreSQL example with Maven that uses Spring Data JPA/Hibernate to interact with PostgreSQL database and export Rest CRUD API. You'll know:

  • How to configure Spring Data, JPA, Hibernate to work with PostgreSQL Database
  • How to define Data Models and Repository interfaces
  • Way to create Spring Rest Controller to process HTTP requests
  • Way to use Spring Data JPA to interact with PostgreSQL Database

Full Article: Spring Boot + PostgreSQL + Maven: CRUD example

Overview of Spring Boot, PostgreSQL example with Maven

We will build a Spring Boot + PostgreSQL + Rest CRUD API for a Tutorial application in that:

  • Each Tutotial has id, title, description, published status.
  • Apis help to create, retrieve, update, delete Tutorials.
  • Apis also support custom finder methods such as find by published status or by title.

These are APIs that we need to provide:

Methods Urls Actions
POST /api/tutorials create new Tutorial
GET /api/tutorials retrieve all Tutorials
GET /api/tutorials/:id retrieve a Tutorial by :id
PUT /api/tutorials/:id update a Tutorial by :id
DELETE /api/tutorials/:id delete a Tutorial by :id
DELETE /api/tutorials delete all Tutorials
GET /api/tutorials/published find all published Tutorials
GET /api/tutorials?title=[keyword] find all Tutorials which title contains keyword
  • We make CRUD operations & finder methods with Spring Data JPA's JpaRepository.
  • The database will be PostgreSQL by configuring project dependency & datasource.

Technology

  • Java 17 / 11 / 8
  • Spring Boot 3 / 2 (with Spring Web MVC, Spring Data JPA)
  • PostgreSQL
  • Maven

Maven Project Structure

spring-boot-postgresql-maven-example-crud-project-structure

Let me explain it briefly.

Tutorial data model class corresponds to entity and table tutorials.
TutorialRepository is an interface that extends JpaRepository for CRUD methods and custom finder methods. It will be autowired in TutorialController.
TutorialController is a RestController which has request mapping methods for RESTful requests such as: getAllTutorials, createTutorial, updateTutorial, deleteTutorial, findByPublished...
– Configuration for Spring Datasource, JPA & Hibernate in application.properties.
pom.xml contains dependencies for Spring Boot and PostgreSQL.

Run the Application

Run Spring Boot + PostgreSQL application with Maven: mvn spring-boot:run.

tutorials table will be automatically generated in Database.
If you check PostgreSQL for example, you can see things like this:



testdb=# \d tutorials
             Table "public.tutorials"
   Column    |          Type          | Modifiers
-------------+------------------------+-----------
 id          | bigint                 | not null
 description | character varying(255) |
 published   | boolean                |
 title       | character varying(255) |
Indexes:
    "tutorials_pkey" PRIMARY KEY, btree (id)


Enter fullscreen mode Exit fullscreen mode

Create some Tutorials:

spring-boot-postgresql-maven-example-crud-create-tutorial



testdb=# SELECT * FROM tutorials;
 id |    description    | published |            title
----+-------------------+-----------+------------------------------
  1 | Tut#1 Description | f         | Spring Boot Tut#1
  2 | Tut#2 Description | f         | PostgreSQL Tut#2
  3 | Tut#3 Description | f         | Spring Data JPA Tut#3
  4 | Tut#4 Description | f         | Maven Tut#4
  5 | Tut#5 Description | f         | Spring Boot PostgreSQL Tut#5
(5 rows)


Enter fullscreen mode Exit fullscreen mode

Update some Tutorials:

spring-boot-postgresql-maven-example-crud-update-tutorial



testdb=# SELECT * FROM tutorials;
 id |    description    | published |            title
----+-------------------+-----------+------------------------------
  3 | Tut#3 Description | f         | Spring Data JPA Tut#3
  5 | Tut#5 Description | f         | Spring Boot PostgreSQL Tut#5
  2 | Desc for Tut#2    | t         | PostgreSQL DB Tut#2
  4 | Desc for Tut#4    | t         | Maven Tut#4
  1 | Desc for Tut#1    | t         | Spring Boot Tut#1
(5 rows)


Enter fullscreen mode Exit fullscreen mode

Retrieve all Tutorials:

spring-boot-postgresql-maven-example-crud-retrieve-tutorial

Get a Tutorial by Id:

spring-boot-postgresql-maven-example-crud-retrieve-one-tutorial

Find all published Tutorials:

spring-boot-postgresql-maven-example-crud-search-field

Find all Tutorials which title contains 'ring':

spring-boot-postgresql-maven-example-crud-search-title

Delete a Tutorial:

spring-boot-postgresql-maven-example-crud-delete-tutorial



testdb=# SELECT * FROM tutorials;
 id |    description    | published |            title
----+-------------------+-----------+------------------------------
  3 | Tut#3 Description | f         | Spring Data JPA Tut#3
  5 | Tut#5 Description | f         | Spring Boot PostgreSQL Tut#5
  2 | Desc for Tut#2    | t         | PostgreSQL DB Tut#2
  1 | Desc for Tut#1    | t         | Spring Boot Tut#1
(4 rows)


Enter fullscreen mode Exit fullscreen mode

Delete all Tutorials:

spring-boot-postgresql-maven-example-crud-delete-all



testdb=# SELECT * FROM tutorials;
 id | description | published | title
----+-------------+-----------+-------
(0 rows)


Enter fullscreen mode Exit fullscreen mode

Source code

For step by step instruction and Github, please visit:
Full Article: Spring Boot + PostgreSQL + Maven: CRUD example

Further Reading

Spring Boot One To Many example with Spring JPA, Hibernate

If you want to add Pagination to this Spring project, you can find the instruction at:
Spring Boot Pagination & Filter example | Spring JPA, Pageable

To sort/order by multiple fields:
Spring Data JPA Sort/Order by multiple Columns | Spring Boot

Handle Exception for this Rest APIs is necessary:

Or way to write Unit Test for the JPA Repository:
Spring Boot Unit Test for JPA Repositiory with @DataJpaTest

Security:
Spring Boot, Spring Security, PostgreSQL: JWT Authentication example

You can also know:

  • how to deploy this Spring Boot App on AWS (for free) with this tutorial.
  • way to upload an Excel file and store the data in MySQL database with this post
  • upload CSV file and store the data in MySQL with this post.

Fullstack examples:

Top comments (0)