0% found this document useful (0 votes)
75 views7 pages

06jan23-Springboot-Student CRUD

The document discusses Spring Boot CRUD operations using Spring Data JPA and Spring Data repositories. It defines the CRUD acronym and HTTP verbs used. It then explains how to perform CRUD operations using Spring Data's CrudRepository and JpaRepository interfaces. The interfaces provide generic CRUD methods and allow Spring to create proxy objects to perform common data operations. The document concludes with a step-by-step example of implementing CRUD for a Student entity using Spring Boot, Spring Data JPA and related interfaces.

Uploaded by

Feel It
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views7 pages

06jan23-Springboot-Student CRUD

The document discusses Spring Boot CRUD operations using Spring Data JPA and Spring Data repositories. It defines the CRUD acronym and HTTP verbs used. It then explains how to perform CRUD operations using Spring Data's CrudRepository and JpaRepository interfaces. The interfaces provide generic CRUD methods and allow Spring to create proxy objects to perform common data operations. The document concludes with a step-by-step example of implementing CRUD for a Student entity using Spring Boot, Spring Data JPA and related interfaces.

Uploaded by

Feel It
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Date: 06-Jan-2023

Objective: Spring boot CRUD operations


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 (Postman) 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.

• POST: Create or to existing save data


• GET: Reads or to fetching existing data
• PUT: Updates or to update existing data or if data is not available save it
• DELETE: Deletes the data
• PATCH: Partially update the data in database.

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.

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.

Why should we use these interfaces?


The interfaces allow Spring to find the repository interface and create proxy objects for that.
It provides methods that allow us to perform some common operations. We can also define
custom methods as well.
Spring Boot CRUD Operation Example
Step1: open https://start.spring.io/
Take spring-web-starter, spring-data-jpa, MySQL Driver and Lombok
Open any IDE tool (Eclipse), go to file menu → import → select the generated Student
project.
Step2: we need to give the proper structure of the package.
Next, we need create proper packages like controller, service, repository (interface only)
and entity.

Step3: create Student.java inside the entity package


//Student.java
package com.jojuskills.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Entity
@Table(name="student")
public class Student {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String address;
private String course;}
Step4: create StudentRepository.java inside the repository package
//StudentRepository
package com.jojuskills.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.jojuskills.entity.Student;

@Repository
public interface StudentRepository extends CrudRepository<Student, Integer>{ }
Step5: create StudentService.java inside the service package
//Student Service.java
package com.jojuskills.service;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jojuskills.entity.Student;
import com.jojuskills.repository.StudentRepository;

@Service
public class StudentService {

@Autowired
StudentRepository studentRepository;
//save the student
public void saveOrUpdate(Student s) {
studentRepository.save(s);}

public Student getStudentById(int id) {


return studentRepository.findById(id).orElseThrow(()->new
RuntimeException("Student_Id not found!!!"));}

public List<Student> getAllStudents(){


List<Student> students=new ArrayList<Student>();
studentRepository.findAll().forEach(stud->students.add(stud));
return students; }
public void update(Student s1, int id) {
studentRepository.save(s1); }
public void delete(int id) {
studentRepository.deleteById(id);//orElseThrow(()->new
RuntimeException("Order_Id not found!!!")); }}
Step6: create StudentController.java inside the controller package
// package com.jojuskills.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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.jojuskills.entity.Student;
import com.jojuskills.service.StudentService;

@RestController
public class StudentController {

@Autowired
StudentService studentService;

@PutMapping("/student")
private int saveStudent(@RequestBody Student student) {
studentService.saveOrUpdate(student);
return student.getId();
}

@GetMapping("/student/{id}")
private Student getStudent(@PathVariable("id")int id) {
return studentService.getStudentById(id);
}

@GetMapping("/student_list")
private List<Student> getAllStudents(){
return studentService.getAllStudents();
}

@PutMapping("/stud")
private Student update(@RequestBody Student s) {
studentService.saveOrUpdate(s);
return s;
}

@DeleteMapping("/student/{id}")
private void deleteStudent(@PathVariable("id") int id) {
studentService.delete(id);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy