Hi there, if you have followed my last tutorial, you have successfully set up the environment for creating CRUD project with Spring boot and embedded DB Apache Derby. You should get the below main class by default.
Now we will create Entity class, controller class and service class one after another to make a complete CRUD project. Let me explain here - sprine boot provides a nice feature which allows us not to set up any db connectivity class - instead - we will be using help of CrudRepository<Employee, String>. Lets follow the below steps -
Step-1: Use the below code and create the Entity class Employee.java
package com.ncteam.business;
import org.springframework.data.repository.CrudRepository;
public interface EmployeeCURDRepository extends CrudRepository<Employee, String>{
}
Step-3: Now create the controller class with below peace of code. We did not make any change to the last project - used the same functionality - Class name EmployeeController.java
package com.ncteam.business;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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;
@RestController
public class EmployeeController {
@Autowired
private EmployeeService employeeservice;
@RequestMapping(method=RequestMethod.GET, value="/listofemp")
public List<Employee> getEmployee() {
return employeeservice.getEmployee();
}
@RequestMapping(method=RequestMethod.GET, value="/listofemp/{empid}")
public Optional<Employee> getEmployeeBYId(@PathVariable String empid) {
return employeeservice.getEmployeeBYId(empid);
}
@RequestMapping(method=RequestMethod.POST, value="/listofemp")
public void saveEmployee(@RequestBody Employee emp) {
employeeservice.saveEmployee(emp);
}
@RequestMapping(method=RequestMethod.PUT, value="/listofemp/{empid}")
public void updateEmployee(@RequestBody Employee emp, @PathVariable String empid) {
employeeservice.updateEmployee(emp,empid);
}
@RequestMapping(method=RequestMethod.DELETE, value="/listofemp/{empid}")
public void deleteEmployee(@PathVariable String empid) {
employeeservice.deleteEmployee(empid);
}
}
Step-4: Create the service class with below code - Class name EmployeeService.java
Now we will create Entity class, controller class and service class one after another to make a complete CRUD project. Let me explain here - sprine boot provides a nice feature which allows us not to set up any db connectivity class - instead - we will be using help of CrudRepository<Employee, String>. Lets follow the below steps -
Step-1: Use the below code and create the Entity class Employee.java
package com.ncteam.business;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
private String employeeID;
@Column
private String employeeName;
@Column
private String employeeAddress;
public String getEmployeeID() {
return employeeID;
}
public void setEmployeeID(String employeeID) {
this.employeeID = employeeID;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeAddress() {
return employeeAddress;
}
public void setEmployeeAddress(String employeeAddress) {
this.employeeAddress = employeeAddress;
}
}
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
private String employeeID;
@Column
private String employeeName;
@Column
private String employeeAddress;
public String getEmployeeID() {
return employeeID;
}
public void setEmployeeID(String employeeID) {
this.employeeID = employeeID;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeAddress() {
return employeeAddress;
}
public void setEmployeeAddress(String employeeAddress) {
this.employeeAddress = employeeAddress;
}
}
Step-2: Now create an interface to include the functionality of CrudRepository. Use the below code and create an interface called EmployeeCRUDRepository.java This is basically provides all JPA functionality.
package com.ncteam.business;
import org.springframework.data.repository.CrudRepository;
public interface EmployeeCURDRepository extends CrudRepository<Employee, String>{
}
Step-3: Now create the controller class with below peace of code. We did not make any change to the last project - used the same functionality - Class name EmployeeController.java
package com.ncteam.business;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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;
@RestController
public class EmployeeController {
@Autowired
private EmployeeService employeeservice;
@RequestMapping(method=RequestMethod.GET, value="/listofemp")
public List<Employee> getEmployee() {
return employeeservice.getEmployee();
}
@RequestMapping(method=RequestMethod.GET, value="/listofemp/{empid}")
public Optional<Employee> getEmployeeBYId(@PathVariable String empid) {
return employeeservice.getEmployeeBYId(empid);
}
@RequestMapping(method=RequestMethod.POST, value="/listofemp")
public void saveEmployee(@RequestBody Employee emp) {
employeeservice.saveEmployee(emp);
}
@RequestMapping(method=RequestMethod.PUT, value="/listofemp/{empid}")
public void updateEmployee(@RequestBody Employee emp, @PathVariable String empid) {
employeeservice.updateEmployee(emp,empid);
}
@RequestMapping(method=RequestMethod.DELETE, value="/listofemp/{empid}")
public void deleteEmployee(@PathVariable String empid) {
employeeservice.deleteEmployee(empid);
}
}
Step-4: Create the service class with below code - Class name EmployeeService.java
package com.ncteam.business;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EmployeeService {
@Autowired
private EmployeeCURDRepository crudrep;
public void saveEmployee(Employee emp) {
// TODO Auto-generated method stub
crudrep.save(emp);
}
public void updateEmployee(Employee emp, String empid) {
// TODO Auto-generated method stub
crudrep.save(emp);
}
public void deleteEmployee(String empid) {
// TODO Auto-generated method stub
crudrep.deleteById(empid);
}
public List<Employee> getEmployee() {
// TODO Auto-generated method stub
List<Employee> employeeList = new ArrayList<>();
crudrep.findAll().forEach(employeeList::add);
return employeeList;
}
public Optional<Employee> getEmployeeBYId(String empid) {
// TODO Auto-generated method stubs
return crudrep.findById(empid);
}
}
We are all set to build the CRUD project using spring boot and Apache Derby DB. Entire project structure should look like below image -
Let's run and test the below test cases -
(A) Using Put method create the DB inserts as shown in below image -
(B) Using GET method select the all employee list like below image -
(C) Using PUT method update a EE data Say Emp2 like below images -
select all ee data to test the change like below image -
(D) Use Delete request to delete the EE data like below image for Emp1
Hope you are able to create and run the complete CRUD project using Spring boot. Please note here we have used embedded DB - so whenever you rebuild the project db gets deleted and you need to create all the inserts from the beginning. In the next tutorial we will create the same project using some other RDBMS - MYSQL. Keep exploring our blog. Share this if you liked it. Also comment below if you have any query. Happy Reading!!
No comments:
Post a Comment