Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are CRUD operations?
3.
Standard CRUD operations
4.
How do CRUD operations work?
5.
Spring Boot CrudRepository
6.
Spring Boot JpaRepository
7.
CrudRepository Vs. JpaRepository
8.
Spring Boot CRUD operation Example
8.1.
Books.java
8.2.
BooksController.java
8.3.
BooksService.java
8.4.
BooksRepository.java
8.5.
application.properties
8.6.
SpringBootCrudOperationApplication.java
9.
Frequently Asked Questions (FAQs)
10.
Key Takeaways
Last Updated: Mar 27, 2024

Spring Boot CRUD Operations

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

There are several application development frameworks available, but one of the most popular frameworks when working with java is the Spring framework.

You will be completely fascinated with the Spring framework and its ability to manage applications.

source: Giphy

In this article, we will learn about the CRUD operations that are available in Spring Boot. There are basically four CRUD operations in Spring Boot, namely create, read, update and delete. 

Let us get into the concept of CRUD operations.

What are CRUD operations?

The CRUD stands for Create, Read/Retrieve, Update, and Delete. These are the four basic functions of persistence storage.

The CRUD operation can be defined as user interface conventions that allow view, search, and modify information through computer-based forms and reports. CRUD is data-oriented and the standardized use of HTTP action verbs. HTTP has a few important verbs.

  • POST: Creates a new resource
  • GET: Reads a resource
  • PUT: Updates an existing resource
  • DELETE: Deletes a resource

Within a database, each of these operations maps directly to a series of commands. However, their relationship with a RESTful API is slightly more complex.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Standard CRUD operations

The standard CRUD operations available in Spring Boot are:

  • CREATE Operation: It performs the INSERT statement to create a new record.
  • READ Operation: It reads table records based on the input parameter.
  • UPDATE Operation: It executes an update statement on the table. It is based on the input parameter.
  • DELETE Operation: It deletes a specified row in the table. It is also based on the input parameter.

How do CRUD operations work?

CRUD operations are at the foundation of the most dynamic websites. Therefore, we should differentiate CRUD from HTTP action verbs.

Suppose, if we want to create a new record, we should use the HTTP action verb POST. To update a record, we should use the PUT verb. Similarly, if we want to delete a record, we should use the DELETE verb. Through CRUD operations, users and administrators have the right to retrieve, create, edit, and delete records online.

We have many options for executing CRUD operations. One of the most efficient choices is to create a set of stored procedures in SQL to execute operations.

The CRUD operations refer to all major functions that are implemented in relational Database applications. Each letter of the CRUD can map to a SQL statement and HTTP methods.

Operation SQL HTTP verbs RESTful Web Service
Create INSERT PUT/POST POST
Read SELECT GET GET
Update UPDATE PUT/POST/PATCH PUT
Delete DELETE DELETE DELETE

 

Spring Boot CrudRepository

Spring Boot provides an interface called CrudRepository that contains methods for CRUD operations. It is defined in the package org.springframework.data.repository. It extends the Spring Data Repository interface. It provides generic Crud operation on a repository. If we want to use CrudRepository in an application, we have to create an interface and extend the CrudRepository.

Syntax

public interface CrudRepository<T,ID> extends Repository<T,ID>

where,

  • T is the domain type that repository manages.
  • ID is the type of the id of the entity that the repository manages.

For example:

public interface StudentRepository extends CrudRepository<Student, Integer> { } 

In the above example, we have created an interface named StudentRepository that extends CrudRepository. Where Student is the repository to manage, and Integer is the type of Id that is defined in the Student repository.

Spring Boot JpaRepository

JpaRepository provides JPA related methods such as flushing, persistence context, and deletes a record in a batch. It is defined in the package org.springframework.data.jpa.repository. JpaRepository extends both CrudRepository and PagingAndSortingRepository.

For example:

public interface BookDAO extends JpaRepository  { }

 Spring Data Repository

 

CrudRepository Vs. JpaRepository

CrudRepository JpaRepository
CrudRepository does not provide any method for pagination and sorting. JpaRepository extends PagingAndSortingRepository. It provides all the methods for implementing pagination.
It works as a marker interface. JpaRepository extends both CrudRepository and PagingAndSortingRepository.
It provides CRUD function only. For example findById(), findAll(), etc. It provides some extra methods along with the method of PagingAndSortingRepository and CrudRepository. For example, flush(), deleteInBatch().
It is used when we do not need the functions provided by JpaRepository and PagingAndSortingRepository. It is used when we want to implement pagination and sorting functionality in an application.

 

Spring Boot CRUD operation Example

Let's set up a Spring Boot application and perform CRUD operation.

Step 1: Open Spring Initializr http://start.spring.io.

Step 2: Select the Spring Boot version 2.3.0.M1.

Step 2: Provide the Group name. We have provided com.codingninjas.

Step 3: Provide the Artifact Id. We have provided spring-boot-crud-operation.

Step 5: Add the dependencies Spring Web, Spring Data JPA, and H2 Database.

Step 6: Click on the Generate button. When we click on the Generate button, it wraps the specifications in a Jar file and downloads it to the local system.

Step 7: Extract the Jar file and paste it into the STS workspace.

Step 8: Import the project folder into STS.

File -> Import -> Existing Maven Projects -> Browse -> Select the folder spring-boot-crud-operation -> Finish

It takes some time to import.

Step 9: Create a package with the name com.codingninjas.model in the folder src/main/java.

Step 10: Create a model class in the package com.codingninjas.model. We have created a model class with the name Books. In the Books class, we have done the following:

  • Define four variable bookid, bookname, author, and
  • Generate Getters and Setters.
    Right-click on the file -> Source -> Generate Getters and Setters.
  • Mark the class as an Entity by using the annotation @Entity.
  • Mark the class as Table name by using the annotation @Table.
  • Define each variable as Column by using the annotation @Column.

Books.java

package com.codingninjas.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
//mark class as an Entity  
@Entity
//defining class name as Table name 
@Table
public class Books {
   //Defining book id as primary key 
   @Id
   @Column
   private int bookid;
   @Column
   private String bookname;
   @Column
   private String author;
   @Column
   private int price;
   public int getBookid() {
       return bookid;
   }
   public void setBookid(int bookid) {
       this.bookid = bookid;
   }
   public String getBookname() {
       return bookname;
   }
   public void setBookname(String bookname) {
       this.bookname = bookname;
   }
   public String getAuthor() {
       return author;
   }
   public void setAuthor(String author) {
       this.author = author;
   }
   public int getPrice() {
       return price;
   }
   public void setPrice(int price) {
       this.price = price;
   }
}

Step 11: Create a package with the name com.codingninjas.controller in the folder src/main/java.

Step 12: Create a Controller class in the package com.codingninjas.controller. We have created a controller class with the name BooksController. In the BooksController class, we have done the following:

  • Mark the class as RestController by using the annotation @RestController.
  • Autowire the BooksService class by using the annotation @Autowired.
  • Define the following methods:
    • getAllBooks(): It returns a List of all Books.
    • getBooks(): It returns a book detail that we have specified in the path variable. We have passed bookid as an argument by using the annotation @PathVariable. The annotation indicates that a method parameter should be bound to a URI template variable.
    • deleteBook(): It deletes a specific book that we have specified in the path variable.
    • saveBook(): It saves the book detail. The annotation @RequestBody indicates that a method parameter should be bound to the body of the web request.
    • update(): The method updates a record. We must specify the record in the body, which we want to update. To achieve the same, we have used the annotation @RequestBody.

BooksController.java

package com.codingninjas.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.codingninjas.model.Books;
import com.codingninjas.service.BooksService;
//mark class as Controller 
@RestController
public class BooksController {
   //autowire the BooksService class 
   @Autowired
   BooksService booksService;
   //creating a get mapping that retrieves all the books detail from the database  
   @GetMapping("/book")
   private List < Books > getAllBooks() {
       return booksService.getAllBooks();
   }
   //creating a get mapping that retrieves the detail of a specific book 
   @GetMapping("/book/{bookid}")
   private Books getBooks(@PathVariable("bookid") int bookid) {
       return booksService.getBooksById(bookid);
   }
   //creating a delete mapping that deletes a specified book 
   @DeleteMapping("/book/{bookid}")
   private void deleteBook(@PathVariable("bookid") int bookid) {
       booksService.delete(bookid);
   }
   //creating post mapping that post the book detail in the database 
   @PostMapping("/books")
   private int saveBook(@RequestBody Books books) {
       booksService.saveOrUpdate(books);
       return books.getBookid();
   }
   //creating put mapping that updates the book detail  
   @PutMapping("/books")
   private Books update(@RequestBody Books books) {
       booksService.saveOrUpdate(books);
       return books;
   }
}

Step 13: Create a package with the name com.codingninjas.service in the folder src/main/java.

Step 14: Create a Service class. We have created a service class with the name BooksService in the package com.codingninjas.service.

BooksService.java

package com.codingninjas.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.codingninjas.model.Books;
import com.codingninjas.repository.BooksRepository;
//defining the business logic 
@Service
public class BooksService {
   @Autowired
   BooksRepository booksRepository;
   //getting all books record by using the method findaAll() of CrudRepository 
   public List < Books > getAllBooks() {
       List < Books > books = new ArrayList < Books > ();
       booksRepository.findAll().forEach(books1 - > books.add(books1));
       return books;
   }
   //getting a specific record by using the method findById() of CrudRepository 
   public Books getBooksById(int id) {
       return booksRepository.findById(id).get();
   }
   //saving a specific record by using the method save() of CrudRepository 
   public void saveOrUpdate(Books books) {
       booksRepository.save(books);
   }
   //deleting a specific record by using the method deleteById() of CrudRepository 
   public void delete(int id) {
       booksRepository.deleteById(id);
   }
   //updating a record 
   public void update(Books books, int bookid) {
       booksRepository.save(books);
   }
}

Step 15: Create a package with the name com.codingninjas.repository in the folder src/main/java.

Step 16: Create a Repository interface. We have created a repository interface with the name BooksRepository in the package com.codingninjas.repository. It extends the Crud Repository interface.

BooksRepository.java

package com.codingninjas.repository;
import org.springframework.data.repository.CrudRepository;
import com.codingninjas.model.Books;
//repository that extends CrudRepository 
public interface BooksRepository extends CrudRepository < Books, Integer > {}

Now we will configure the datasource URL, driver class name, username, and password, in the application.properties file.

Step 17: Open the application.properties file and configure the following properties.

application.properties

spring.datasource.url=jdbc:h2:mem:books_data 
spring.datasource.driverClassName=org.h2.Driver 
spring.datasource.username=sa 
spring.datasource.password= 
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 
#enabling the H2 console 
spring.h2.console.enabled=true 

Note: Do not forget to enable the H2 server

Now we will run the application.

Step 18: Open SpringBootCrudOperationApplication.java file and run it as Java Application.

SpringBootCrudOperationApplication.java

package com.codingninjas;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootCrudOperationApplication {
   public static void main(String[] args) {
       SpringApplication.run(SpringBootCrudOperationApplication.class, args);
   }
}

Step 19: Open the Postman and do the following:

  • Select the POST
  • Invoke the URL http://localhost:8080/books.
  • Select the Body
  • Select he Content-Type JSON (application/json).
  • Insert the data. We have inserted the following data in the Body:
{ 
 "bookid": "5433", 
 "bookname": "Core and Advance Java", 
 "author": "R. Nageswara Rao", 
 "price": "800" 
}  
  • Click on the Send

When the request is successfully executed, it shows the Status:200 OK. It means the record has been successfully inserted in the database.

Similarly, we have inserted the following data.

{ 
 "bookid": "0982", 
 "bookname": "Programming with Java", 
 "author": "E. Balagurusamy", 
 "price": "350" 
}  
{ 
 "bookid": "6321", 
 "bookname": "Data Structures and Algorithms in Java", 
 "author": "Robert Lafore", 
 "price": "590" 
}  
{ 
 "bookid": "5433", 
 "bookname": "Effective Java", 
 "author": "Joshua Bloch", 
 "price": "670" 
} 

Step 20: Open the browser and invoke the URL http://localhost:8080/h2-console. Click on the Connect button, as shown below.

Spring Boot CRUD Operations

Source: Link

After clicking on the Connect button, we see the Books table in the database, as shown below.

Spring Boot CRUD Operations

Source: Link

Step 21: Click on the Books table and then click on the Run button. The table shows the data that we have inserted in the body.

Spring Boot CRUD Operations

Source: Link

Step 22: Open the Postman and send a GET request with the URL http://localhost:8080/books. It returns the data that we have inserted in the database.

Spring Boot CRUD Operations

Source: Link

Let's send a GET request with the URL http://localhost:8080/book/{bookid}. We have specified the bookid 6830. It returns the detail of the book whose id is 6830.

Similarly, we can also send a DELETE request to delete a record. Suppose we want to delete a book record whose id is 5433.

Select the DELETE method and invoke the URL http://localhost:8080/book/5433. Again, execute the Select query in the H2 console. We see that the book whose id is 5433 has been deleted from the database.

Spring Boot CRUD Operations

Source: Link

Similarly, we can also update a record by sending a PUT request. Let's update the price of the book whose id is 6321.

  • Select the PUT
  • In the request body, paste the record which you want to update and make the changes. In our case, we want to update the record of the book whose id is 6321. In the following record, we have changed the price of the book.
{ 
 "bookid": "6321", 
 "bookname": "Data Structures and Algorithms in Java", 
 "author": "Robert Lafore", 
 "price": "500" 
} 
  • Click on the Send

Now, move to the H2 console and see the changes have reflected or not. We see that the price of the book has been changed, as shown below.

Spring Boot CRUD Operations

Source: Link

Must Read Spring Tool Suite

Frequently Asked Questions (FAQs)

1.What is Spring?

The Spring framework is one of the most popular application development frameworks of Java. The important feature of the spring framework is dependency injection or the Inversion of Control.

2.What is Spring Boot?

Spring Boot is one of the modules of the Spring Framework. It helps us to build a stand-alone application with very minimal or almost zero configurations.

3.What are CRUD operations?

The four basic CRUD operations are Create, Read, Update and Delete.

Key Takeaways

We learned about the various frameworks of Spring. In this article, we learned about the various CRUD operations in detail. We also learned about the detailed example to demonstrate CRUD operations in Spring Boot.

You can also consider our Spring Boot Course to give your career an edge over others.

Happy Learning!

 

Live masterclass