Crud Repository

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Student: Aphar

Group: 682.20E Faculty: ITM


Magarramova

Specialty: Subject: Object


Teacher: Nubar
Information Oriented
Kishiyeva
Technologies Programming
CrudRepository
Context
• Introduction to CRUD operations
• What is a CrudRepository?
• How to implement a CrudRepository
• Advanced features of CrudRepository
• Best practices and tips
Introduction to CRUD
operations
CRUD refers to the four basic operations a software application should
be able to perform – Create, Read, Update, and Delete.
In such apps, users must be able to create data, have access to the data
in the UI by reading the data, update or edit the data, and delete the
data.
In full-fledged applications, CRUD apps consist of 3 parts: an API (or
server), a database, and a user interface (UI).
The API contains the code and methods, the database stores and helps
the user retrieve the information, while the user interface helps users
interact with the app.
Each letter in the CRUD acronym has a corresponding HTTP request
method.
In CRUD, the create operation
Create Operation
does what the name implies. It
means creating an entry. This
entry could be an account, user
information, a post, or a task.
The HTTP protocol that
implements a CREATE operation is
the POST method.
In a SQL database, to create is to
INSERT. In a NoSQL database like
MongoDB, you create with the
insert() method.
Read Operation
The READ operation means getting access to the inputs
or entries in the UI. That is, seeing it. Again, the entry
could be anything from user information to social media
posts, and others.
This access could mean the user getting access to the
created entries right after creating them or searching for
them. Searching is implemented to allow the user to
filter out the entries they don’t need.
The HTTP protocol that implements a READ operation is
the GET method.
In a SQL database, to read is to SELECT an entry. In a
NoSQL database like MongoDB, you read with the find()
or findById() method.
Update Operation
UPDATE is the operation that allows you to
modify existing data. That is, editing the data.
Unlike READ, the UPDATE operation alters the
existing data by making changes to it.
PUT and PATCH are the HTTP protocols with
which you can implement an UPDATE
operation, depending on what you need.
PUT should be used when you want the entire
entry updated, and PATCH if you don’t want the
entire entry to be modified.
In a SQL database, you use UPDATE to update
an entry. In a NoSQL database like MongoDB,
you can implement an update feature with the
findByIdAndUpdate() method.
To delete is to get rid of an entry
from the UI and the database.
Delete operation
DELETE is the HTTP protocol for
implementing a DELETE
operation.
In a SQL database, DELETE is used
to delete an entry. In a NoSQL
database like findByIdAndDelete()
MongoDB, you can implement
delete with the method.
What is a CrudRepository is a Spring Data
interface for generic CRUD operations
on a repository of a specific type. A
CrudRepository? repository provides a layer of
abstraction between the application
code and the database, making it easier
to manage data in a consistent and
efficient way.
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. If we want to use
CrudRepository in an application, we
have to create an interface and extend
the CrudRepository.
How to implement a
CrudRepository?
Create 4 packages and create some classes and interfaces
inside these packages as seen in the below image
• entity
• repository
• service
• controller
Define your entity class:
First, define your entity class that represents a database table
or collection. This class should have properties that map to
columns in the table or fields in the collection.
Create a simple POJO class inside the Department.java file.
In this example, we annotate the Department class with
@Entity to indicate that it is an entity class.
We also annotate the primary key property with @Id and
@GeneratedValue to indicate that it is an automatically
generated ID.
Define the repository interface:
Next, define an interface that extends the CrudRepository
interface and specifies the entity type and ID type. For
example, if you have an entity class called Department
with a Long ID, your repository interface would look like
this.
This interface will inherit all the basic CRUD methods from
CrudRepository, such as save(), findById(), findAll(), and
delete().
We've defined a DepartmenService interface define Crud
methods which will be overridden by
DepartmentServiceImpl class.
• Create (Save operation): The saveDepartment()
method is responsible for creating a new Department
entity and saving it to the database. This method takes
a Department object as an argument and returns the
saved Department object.
• Read (Fetch operation): The fetchDepartmentList()
method is responsible for retrieving a list of all
Department entities from the database. This method
returns a list of Department objects.
• Update (Update operation): The updateDepartment()
method is responsible for updating an existing
Department entity in the database. This method takes
a Department object and the ID of the department to
be updated as arguments and returns the updated
Department object.
• Delete (Delete operation): The
deleteDepartmentById() method is responsible for
deleting an existing Department entity from the
database. This method takes the ID of the department
to be deleted as an argument and does not return any
value.
This is an implementation of the DepartmentService
interface which uses the DepartmentRepository to perform
CRUD operations on the Department entity.
The DepartmentServiceImpl class is marked as a @Service,
which makes it a candidate for dependency injection.
This is a sample implementation of a REST
controller in Spring Boot that uses the
DepartmentService to perform CRUD
operations on Department entities.
The @RestController annotation tells Spring
that this class will handle incoming HTTP
requests and respond with JSON data.
The @Autowired annotation injects the
DepartmentService bean into the controller.
The four methods correspond to the four CRUD operations:
• saveDepartment(): This method is called when a POST
request is made to the "/departments" endpoint. It saves a
new Department entity to the database and returns the
saved entity.
• fetchDepartmentList(): This method is called when a GET
request is made to the "/departments" endpoint. It returns
a list of all Department entities in the database.
• updateDepartment(): This method is called when a
PUT request is made to the "/departments/{id}"
endpoint, where {id} is the ID of the Department
entity to be updated. It updates the specified
Department entity with the new data in the request
body and returns the updated entity.
• deleteDepartmentById(): This method is called when
a DELETE request is made to the "/departments/{id}"
endpoint, where {id} is the ID of the Department
entity to be deleted. It deletes the specified
Department entity from the database.
Thank you for your
attention
• Spring data crud repository save
References • CRUD operations
• Implement Crud Repository

You might also like