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.w3cschoool. 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.w3cschoool.model in the folder src/main/java. Step 10: Create a model class in the package com.w3cschoool.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.w3cschoool.controller in the folder src/main/java. Step 12: Create a Controller class in the package com.w3cschoool.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.w3cschoool.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.w3cschoool.service. BooksService.java Step 15: Create a package with the name com.w3cschoool.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.w3cschoool.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
|
Python tutorial provides basic and advanced concepts of Python.
Vue.js is an open-source progressive JavaScript framework
HTML refers to Hypertext Markup Language. HTML is the gateway ...
Java is an object-oriented, class-based computer-programming language.
PHP is an open-source,interpreted scripting language.
Spring is a lightweight framework.Spring framework makes ...
JavaScript is an scripting language which is lightweight and cross-platform.
CSS refers to Cascading Style Sheets...
jQuery is a small and lightweight JavaScript library. jQuery ...
SQL is used to perform operations on the records stored in the database.
C programming is considered as the base for other programming languages.
JavaScript is an scripting language which is lightweight and cross-platform.
Vue.js is an open-source progressive JavaScript framework
ReactJS is a declarative, efficient, and flexible JavaScript library.
jQuery is a small and lightweight JavaScript library. jQuery ...
Node.js is a cross-platform environment and library for running JavaScript app...
TypeScript is a strongly typed superset of JavaScript which compiles to plain JavaScript.
Angular JS is an open source JavaScript framework by Google to build web app...
JSON is lightweight data-interchange format.
AJAX is an acronym for Asynchronous JavaScript and XML.
ES6 or ECMAScript 6 is a scripting language specification ...
Angular 7 is completely based on components.
jQuery UI is a set of user interface interactions built on jQuery...
Python tutorial provides basic and advanced concepts of Python.
Java is an object-oriented, class-based computer-programming language.
Node.js is a cross-platform environment and library for running JavaScript app...
PHP is an open-source,interpreted scripting language.
Go is a programming language which is developed by Google...
C programming is considered as the base for other programming languages.
C++ is an object-oriented programming language. It is an extension to C programming.
C# is a programming language of .Net Framework.
Ruby is an open-source and fully object-oriented programming language.
JSP technology is used to create web application just like Servlet technology.
The JSTL represents a set of tags to simplify the JSP development.
ASP.NET is a web framework designed and developed by Microsoft.
Perl is a cross-platform environment and library for running JavaScript...
Scala is an object-oriented and functional programming language.
VBA stands for Visual Basic for Applications.
Spring is a lightweight framework.Spring framework makes ...
Spring Boot is a Spring module that provides the RAD feature...
Django is a Web Application Framework which is used to develop web applications.
Servlet technology is robust and scalable because of java language.
The Struts 2 framework is used to develop MVC based web applications.
Hibernate is an open source, lightweight, ORM tool.
Solr is a scalable, ready-to-deploy enterprise search engine.
SQL is used to perform operations on the records stored in the database.
MySQL is a relational database management system based...
Oracle is a relational database management system.
SQL Server is software developed by Microsoft.
PostgreSQL is an ORDBMS.
DB2 is a database server developed by IBM.
Redis is a No SQL database which works on the concept of key-value pair.
SQLite is embedded relational database management system.
MongoDB is a No SQL database. It is an document-oriented database...
Memcached is a free, distributed memory object caching system.
Hibernate is an open source, lightweight, ORM tool.
PL/SQL is a block structured language that can have multiple blocks in it.
DBMS Tutorial is software that is used to manage the database.
Spark is a unified analytics engine for large-scale data processing...
IntelliJ IDEA is an IDE for Java Developers which is developed by...
Git is a modern and widely used distributed version control system in the world.
GitHub is an immense platform for code hosting.
SVN is an open-source centralized version control system.
Maven is a powerful project management tool that is based on POM.
Jsoup is a java html parser.
UML is a general-purpose, graphical modeling language.
RESTful Web Services are REST Architecture based Web Services.
Postman is one testing tools which is used for API testing.
JMeter is to analyze the performance of web application.
Jenkins builds and tests our software projects.
SEO stands for Search Engine Optimization.
MATLAB is a software package for mathematical computation, visualization...
Unity is an engine for creating games on multiple platforms.
Hadoop is an open source framework.
Pig is a high-level data flow platform for executing Map Reduce programs of Hadoop.
Spark is a unified analytics engine for large-scale data processing...
Spring Cloud is a framework for building robust cloud applications.
Spring Boot is a Spring module that provides the RAD feature...
AI is one of the fascinating and universal fields of Computer.
Cloud computing is a virtualization-based technology.
AWS stands for Amazon Web Services which uses distributed IT...
Microsoft Azure is a cloud computing platform...
IoT stands for Internet of Things...
Spring Cloud is a framework for building robust cloud applications.
Email:jjw.quan@gmail.com