In this tutorials will teach you how to do the Restful API to add the records in to the mysql database.how to insert the records into mysql database step by step via spring boot layer architecture process using industrial standards.
i have explained how the layer architecture works on spring boot application. If you are not read that please read then come to this lesson you will understand easily.
Create the Package entity inside the package create the class Employee
Employee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | package com.springschool.firstapp.entity; import com.vladmihalcea.hibernate.type.json.JsonType; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; import javax.persistence.*; import java.util.ArrayList; @Entity @Table(name = "employee") @TypeDefs({ @TypeDef(name = "json",typeClass = JsonType.class) }) public class Employee { @Id @Column(name = "employee_id",length = 45) @GeneratedValue(strategy = GenerationType.AUTO) private int employeeId; @Column(name = "employee_name",length = 100,nullable = false) private String employeeName; @Column(name = "employee_address",length = 150) private String employeeAddress; @Column(name = "employee_salary",length = 25) private double employeeSalary; @Type(type = "json") @Column(name = "contact_numbers",columnDefinition = "json") private ArrayList contactNumbers; @Column(name = "nic",length = 12,unique = true) private String nic; @Column(name = "active_state",columnDefinition = "TINYINT default 1") private boolean activeState; public Employee() { } public Employee(int employeeId, String employeeName, String employeeAddress, double employeeSalary, ArrayList contactNumbers, String nic, boolean activeState) { this.employeeId = employeeId; this.employeeName = employeeName; this.employeeAddress = employeeAddress; this.employeeSalary = employeeSalary; this.contactNumbers = contactNumbers; this.nic = nic; this.activeState = activeState; } public int getEmployeeId() { return employeeId; } public void setEmployeeId(int 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; } public double getEmployeeSalary() { return employeeSalary; } public void setEmployeeSalary(double employeeSalary) { this.employeeSalary = employeeSalary; } public ArrayList getContactNumbers() { return contactNumbers; } public void setContactNumbers(ArrayList contactNumbers) { this.contactNumbers = contactNumbers; } public String getNic() { return nic; } public void setNic(String nic) { this.nic = nic; } public boolean isActiveState() { return activeState; } public void setActiveState(boolean activeState) { this.activeState = activeState; } @Override public String toString() { return "Employee{" + "employeeId=" + employeeId + ", employeeName='" + employeeName + '\'' + ", employeeAddress='" + employeeAddress + '\'' + ", employeeSalary=" + employeeSalary + ", contactNumbers=" + contactNumbers + ", nic='" + nic + '\'' + ", activeState=" + activeState + '}'; } } |
EmployeeDTO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | package com.springschool.firstapp.dto; import org.hibernate.annotations.Type; import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import java.util.ArrayList; public class EmployeeDTO { private int employeeId; private String employeeName; private String employeeAddress; private double employeeSalary; private ArrayList contactNumbers; private String nic; private boolean activeState; public EmployeeDTO() { } public EmployeeDTO(int employeeId, String employeeName, String employeeAddress, double employeeSalary, ArrayList contactNumbers, String nic, boolean activeState) { this.employeeId = employeeId; this.employeeName = employeeName; this.employeeAddress = employeeAddress; this.employeeSalary = employeeSalary; this.contactNumbers = contactNumbers; this.nic = nic; this.activeState = activeState; } public int getEmployeeId() { return employeeId; } public void setEmployeeId(int 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; } public double getEmployeeSalary() { return employeeSalary; } public void setEmployeeSalary(double employeeSalary) { this.employeeSalary = employeeSalary; } public ArrayList getContactNumbers() { return contactNumbers; } public void setContactNumbers(ArrayList contactNumbers) { this.contactNumbers = contactNumbers; } public String getNic() { return nic; } public void setNic(String nic) { this.nic = nic; } public boolean isActiveState() { return activeState; } public void setActiveState(boolean activeState) { this.activeState = activeState; } @Override public String toString() { return "EmployeeDTO{" + "employeeId=" + employeeId + ", employeeName='" + employeeName + '\'' + ", employeeAddress='" + employeeAddress + '\'' + ", employeeSalary=" + employeeSalary + ", contactNumbers=" + contactNumbers + ", nic='" + nic + '\'' + ", activeState=" + activeState + '}'; } } |
EmployeeController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import com.springschool.firstapp.dto.EmployeeDTO; import com.springschool.firstapp.service.EmployeeService; import com.springschool.firstapp.service.EmployeeServiceIMPL; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @CrossOrigin @RequestMapping("api/v1/employee") public class EmployeeController { @Autowired private EmployeeService employeeService; @PostMapping(path = "/save") public String saveEmployee(@RequestBody EmployeeDTO employeeDTO) { String id = employeeService.addEmployee(employeeDTO); return id; } } |
Employee Service
1 2 3 4 5 6 7 8 | import com.springschool.firstapp.dto.EmployeeDTO; import com.springschool.firstapp.repository.EmployeeRepo; import org.springframework.beans.factory.annotation.Autowired; public interface EmployeeService { String addEmployee(EmployeeDTO employeeDTO); } |
EmployeeServiceIMPL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import com.springschool.firstapp.dto.EmployeeDTO; import com.springschool.firstapp.entity.Employee; import com.springschool.firstapp.repository.EmployeeRepo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; @Service public class EmployeeServiceIMPL implements EmployeeService{ @Autowired private EmployeeRepo employeeRepo; @Override public String addEmployee(EmployeeDTO employeeDTO) { Employee employee = new Employee( employeeDTO.getEmployeeId(), employeeDTO.getEmployeeName(), employeeDTO.getEmployeeAddress(), employeeDTO.getEmployeeSalary(), employeeDTO.getContactNumbers(), employeeDTO.getNic(), employeeDTO.isActiveState() ); employeeRepo.save(employee); return employee.getEmployeeName(); } } |
EmployeeRepo
1 2 3 4 5 6 7 8 9 10 11 | import com.springschool.firstapp.entity.Employee; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.stereotype.Repository; @EnableJpaRepositories @Repository public interface EmployeeRepo extends JpaRepository<Employee,Integer> { } |
i have attached the video link below. which will do this tutorials step by step