Spring boot Series-1 project setup and CRUD operations

Mohamed Razan
Mar 20 · 3 min read

In this spring boot series, I am going to show you all how to develop a spring boot project from basic. In this article we’ll focus on how to start a spring boot project and do all the four CRUD (Create, Read, Update and Delete) operations using a single table. Here I’ll be using the spring tool suite with Maven and MySQL database for implementation.

  1. Project Setup
Go to File → New → Spring Starter Project
Change Name, Group and Package
Select the dependencies Spring web, spring Data JPA, MySQL Driver
Got to application.properties

Paste the above code into the application.properties change username and password according to your MySQL settings.

2. Create packages

create packages for Model, Repository, Service and Controller

3. Create Model Class

Create Model class in com.series.springboot.model

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String email;
private String contactNo;

public Student(int id, String name, String email, String contactNo) {
super();
this.id = id;
this.name = name;
this.email = email;
this.contactNo = contactNo;
}
public Student() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
}

4. Create Repository Interface

Create Repository Interface in com.series.springboot.repository and extend it from JPARepository.

import org.springframework.data.jpa.repository.JpaRepository;import com.series.springboot.model.Student;public interface StudentRepository extends JpaRepository<Student, Integer> {}

5. Create Service Class

Create Service Class in com.series.springboot.service

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.series.springboot.model.Student;
import com.series.springboot.repository.StudentRepository;
@Service
public class StudentService {
@Autowired
private StudentRepository repository;

public Student createStudent(Student student) {
return repository.save(student);
}

public Student getStudentById(int sid) {
return repository.findById(sid).orElse(null);
}

public String deleteStudentrById(int sid) {
repository.deleteById(sid);
return “deleted”;
}

public Student updateStudent(Student student) {
Student existingStudent = repository.findById(student.getId()).orElse(null);

existingStudent.setContactNo(student.getContactNo());
existingStudent.setEmail(student.getEmail());
existingStudent.setName(student.getName());

repository.save(existingStudent);

return existingStudent;
}
}

6. Create Controller Class

Create Controller Class in com.series.springboot.controller

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.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.series.springboot.model.Student;
import com.series.springboot.service.StudentService;
@RestController
public class StudentController {
@Autowired
private StudentService service;

@PostMapping(“/create_student”)
public Student createStudent(@RequestBody Student student) {
return service.createStudent(student);
}

@GetMapping(“get_student/{sid}”)
public Student getStudent(@PathVariable int sid) {
return service.getStudentById(sid);
}

@DeleteMapping(“/delete_student/{uid}”)
public String deleteStudent(@PathVariable int sid) {
return service.deleteStudentrById(sid);
}

@PutMapping(“/update_student”)
public Student updateStudent(@RequestBody Student student) {
return service.updateStudent(student);
}
}

I hope this article is useful for you. Let’s continue this spring boot series in the next article

Javarevisited

Medium’s largest Java publication, followed by 12700+ programmers. Follow to join our community.