Home Spring boot Spring boot JPA Save records to Mysql database

Spring boot JPA Save records to Mysql database

9 min read
0
0
659

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

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

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

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

Employee Service

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

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

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> {


}

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

    React Inventory Management System Project

    Inventory Management App in React.this app explain the complete module of the Inventory sa…