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
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 + '}'; } }
Create the Package Dt0 inside the package create the class EmployeeDTO
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 + '}'; } }
Create the Package Controller inside the package create the class EmployeeController
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; } }
Create the Package Service inside the package create the one class EmployeeServiceIMPL and create one interface EmployeeService
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); }
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(); } }
Create the Package EmployeeRepo inside the package create one interface EmployeeRepo
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> { }
This article explain how to make a Fish Inventory Management App in Angular.this app explain…
Introduction to Fish Inventory Management In the aquaculture industry, managing fish inventory is crucial for…
Introduction to Java GUI CRUD Java is a powerful programming language widely used for building…
Introduction to Login Form Design Designing an effective and beautiful login form is crucial for…
Introduction In today creating a responsive login form is essential for providing a seamless user…
Introduction to Inventory Management Systems In today's fast-paced digital environment, businesses require efficient inventory management…