Spring Boot CRUD OperationsWhat is the CRUD operation?The CRUD stands for Create, Read/Retrieve, Update, and Delete. These are the four basic functions of the 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.
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. Standard CRUD Operation
How CRUD Operations WorksCRUD operations are at the foundation of the most dynamic websites. Therefore, we should differentiate CRUD from the HTTP action verbs. Suppose, if we want to create a new record, we should use 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.
Spring Boot CrudRepositorySpring 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 where,
For example: 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 JpaRepositoryJpaRepository 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: Why should we use these interfaces?
CrudRepository vs. JpaRepository
Spring Boot CRUD Operation ExampleLet'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.javatpoint. 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.javatpoint.model in the folder src/main/java. Step 10: Create a model class in the package com.javatpoint.model. We have created a model class with the name Books. In the Books class, we have done the following:
Books.java Step 11: Create a package with the name com.javatpoint.controller in the folder src/main/java. Step 12: Create a Controller class in the package com.javatpoint.controller. We have created a controller class with the name BooksController. In the BooksController class, we have done the following:
BooksController.java Step 13: Create a package with the name com.javatpoint.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.javatpoint.service. BooksService.java Step 15: Create a package with the name com.javatpoint.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.javatpoint.repository. It extends the Crud Repository interface. BooksRepository.java 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 Note: Do not forget to enable the H2 console.After creating all the classes and packages, the project directory looks like the following. Now we will run the application. Step 18: Open SpringBootCrudOperationApplication.java file and run it as Java Application. SpringBootCrudOperationApplication.java Note: In the next steps we will use rest client Postman. So, ensure that the Postman application is already installed in your system.Step 19: Open the Postman and do the following:
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. Let's access the H2 console to see the data. Step 20: Open the browser and invoke the URL http://localhost:8080/h2-console. Click on the Connect button, as shown below. After clicking on the Connect button, we see the Books table in the database, as shown below. 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. 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. 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. Similarly, we can also update a record by sending a PUT request. Let's update the price of the book whose id is 6321.
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. Next TopicSpring Boot Thymeleaf View |