Home Spring boot Spring Boot Crud For Beginners

Spring Boot Crud For Beginners

10 min read

This Spring boot tutorial will teach you how to do basic database functions that are CREATE RETIEVE, UPDATE and DELETE and SEARCH using mysql Database. The INSERT, SELECT, UPDATE and DELETE statements can be used in any database system, because this is support by all relational database systems.

We will learn how to INSERT, SELECT, UPDATE and DELETE and  SEARCH in database by writing code to manage the records table in the database named employeeinfo.  records  table consist of following columns fname,lname,city,phone,salary.

Crud function how to perform the RESTful Web Service let discuss with following annotations.

@PostMapping:  annotation which used to create new record.
@GetMapping:   annotation which used to reads a record.
@RequestMapping: annotation which used to search the record.
@PutMapping:   annotation which used to update the existing record.
@DeleteMapping:  annotation which used to delete the record.

The package structure, you must following the standard package structure of spring boot and spring framework.for the example i have created the project name demo1

// This is the main class of the programme where the programming runs.

Spring Boot MVC Structure
----------------------> Stuentrepository.java
-----------------------> Student.java

Spring Boot Mvc Working Flow


First you must Create the package com.example.demo1.Entity. inside the package you have to create the class.

i have created the table name  which is records in the database. this annotation  @Table(name=”Records”) indicate the table name of the database.

  1. Entity annotation @Entity indicate as Entity of class.
  2. Column annotation @Column indicate each column of the table.


package com.example.demo1.Entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

public class Student 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String fname;
    private String lname;
    private String 	city;
    private String phone;
    private String salary;

	public long getId() {
		return id;

	public void setId(long id) {
		this.id = id;

	public String getFname() {
		return fname;

	public void setFname(String fname) {
		this.fname = fname;

	public String getLname() {
		return lname;

	public void setLname(String lname) {
		this.lname = lname;

	public String getCity() {
		return city;

	public void setCity(String city) {
		this.city = city;

	public String getPhone() {
		return phone;

	public void setPhone(String phone) {
		this.phone = phone;

	public String getSalary() {
		return salary;

	public void setSalary(String salary) {
		this.salary = salary;

after that you have to Create the package com.example.demo1.controller . inside the package you have to create the class


package com.example.demo1.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo1.Entity.Student;
import com.example.demo1.repository.Studentrepository;
import com.example.demo1.service.StudentServices;

public class StudentContoller 
	 private StudentServices services;
	 public Iterable<Student>getStudents() 
		 return services.listAll();     

	 @PostMapping(value = "/save")
	 private long saveBook(@RequestBody Student students)   
		 return  students.getId();  
	 private Student getBooks(@PathVariable(name = "id") int studentid)   
	 return services.getStudentById(studentid);  

	    private Student update(@RequestBody Student students)   
	       return students;  

	 private void deleteStudent(@PathVariable("id") int id)   

First you must Create the package com.example.demo1.service . inside the package you have to create the class


package com.example.demo1.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo1.Entity.Student;
import com.example.demo1.repository.Studentrepository;

public class StudentServices {
    private Studentrepository repo;
	public Iterable<Student> listAll() {
        return this.repo.findAll();
	//saving a specific record by using the method save() of CrudRepository  
	public void saveOrUpdate(Student students)   
	public Student getStudentById(long id)   
		return repo.findById(id).get();  
	public void update(Student students, int id)   

	public void delete(long id)   


First you must Create the package com.example.demo1.repository inside the package you have to create the Interface here.

package com.example.demo1.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.example.demo1.Entity.Student;

public interface Studentrepository extends CrudRepository<Student,Long> {

After that you must set database path and project configuration in application.properties file.



i have attached the video link below. which will do this tutorials step by step.


Load More Related Articles
Load More By admin
Load More In Spring boot

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also

Laravel 11 CRUD Mastering RESTful API MVC with Repository Pattern

In this tutorial will teach Laravel 11 Api MVC with Repository Pattern Crud Application st…