Featured Article

Spring Boot Database Application Example: Build CRUD REST API With Ease

Kenneth Jul 13, 2026

Spring Boot, a popular framework for developing microservices and web applications, simplifies working with databases by offering comprehensive solutions for connecting and managing databases. Let's explore an example of a Spring Boot application leveraging a relational database.

👏 Spring Boot Annotations – Quick Reference with Real Use Cases! | vamsi krishna posted on the topic | LinkedIn
👏 Spring Boot Annotations – Quick Reference with Real Use Cases! | vamsi krishna posted on the topic | LinkedIn

For this example, we'll use Spring Boot in version 2.5.0 and H2, an embedded database, which is ideal for development and testing purposes. We'll create a simple CRUD (Create, Read, Update, Delete) application for managing books.

How to Connect PostgreSQL Database with Spring Boot
How to Connect PostgreSQL Database with Spring Boot

Project Setup

Begin by initializing a new Spring Boot project using start.spring.io or your favorite IDE. Add 'Web', 'H2', and 'JPA' dependencies to your project.

Spring Boot Application Properties Example - Java Code Geeks
Spring Boot Application Properties Example - Java Code Geeks

Let's configure our application.properties file for using H2. We'll set the driver class name, URL, username, and password. Also, configure Spring Boot to echo SQL queries and create the schema automatically:

```properties spring.h2.console.enabled=true spring.h2.conconsole.path=/h2-console spring.datasource.url=jdbc:h2:mem:booksdb spring.datasource.driverclassname=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true ```

Book Entity

Spring Boot Project Structure for Clean & Scalable Code | Raghvendra Pratap posted on the topic | LinkedIn
Spring Boot Project Structure for Clean & Scalable Code | Raghvendra Pratap posted on the topic | LinkedIn

Create a simple Book entity with id, title, author, publication date, and genre. Use annotations like @Entity, @Id, @Column, and @GeneratedValue for mapping the entity to the database:

```java @Entity public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; private String author; @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) private LocalDate publicationDate; private Genre genre; // Getters and setters } ```

Book Repository

Create a BookRepository interface extending JpaRepository, which provides basic CRUD methods out of the box. Add custom finder methods if needed:

Spring boot
Spring boot

```java public interface BookRepository extends JpaRepository { List findByGenre(Genre genre); } ```

Book Controller

Develop a BookController to handle HTTP requests for creating, reading, updating, and deleting books. Utilize the @RestController and @RequestMapping annotations for handling HTTP requests and responses:

```java @RestController @RequestMapping("/api/books") public class BookController { @Autowired private BookRepository bookRepository; @GetMapping public List getAllBooks() { return bookRepository.findAll(); } @PostMapping public Book createBook(@RequestBody Book book) { return bookRepository.save(book); } // Implement other CRUD operations (get, update, delete) using different HTTP methods } ```

That's it! You've created a simple Book application running on a Spring Boot application leveraging a relational database. You can now start your application and use tools like Postman or curl to interact with your REST API.

#springboot #javadeveloper #backenddevelopment #softwarearchitecture #apis #microservices #systemdesign #cloudengineering #devops #programmingtips | Ignacio C.
#springboot #javadeveloper #backenddevelopment #softwarearchitecture #apis #microservices #systemdesign #cloudengineering #devops #programmingtips | Ignacio C.

Testing the Application

Create a Book with the following properties and save it:

How To Work With MongoDB In Spring Boot Project?
How To Work With MongoDB In Spring Boot Project?
Spring Boot Project Ideas for Resume | Real-World Examples
Spring Boot Project Ideas for Resume | Real-World Examples
Spring Boot vs Django: Which One Powers Your Next Project?
Spring Boot vs Django: Which One Powers Your Next Project?
Spring Boot H2 Database
Spring Boot H2 Database
Complete Spring Framework Cheat Sheet | Spring Boot, MVC
Complete Spring Framework Cheat Sheet | Spring Boot, MVC
Spring Boot Dependency Injection Made Easy
Spring Boot Dependency Injection Made Easy
the spring boot architecture poster is shown
the spring boot architecture poster is shown
an image of a computer screen with text and numbers on it, as well as other items
an image of a computer screen with text and numbers on it, as well as other items
How To Develop REST API Using Spring Boot ?
How To Develop REST API Using Spring Boot ?

```json { "title": "The Catcher in the Rye", "author": "J.D. Salinger", "publicationDate": "1951-07-16", "genre": "Novel" } ```

You should now see the book listed when you fetch all books using /api/books. This demonstrates the successful operation of your Spring Boot application using a relational database for managing books.

Embracing this approach, you can quickly build efficient, maintainable, and scalable applications using Spring Boot's powerful database features. So, start exploring and expanding your application to suit your specific needs. Happy coding!