- Spring Boot ORM - Home
- Spring Boot ORM - Overview
- Spring Boot ORM - Environment Setup
- Spring Boot ORM - JPA
Spring Boot ORM & Spring Data JPA
- Spring Boot ORM - Create Project
- Spring Boot ORM - application.properties
- Spring Boot ORM - Update Project
- Spring Boot ORM - Test Hibernate
Spring Boot ORM & EclipseLink
- Spring Boot ORM - Maven EclipseLink
- Spring Boot ORM - Update Project EclipseLink
- Spring Boot ORM - Test EclipseLink
Spring Boot ORM Resources
Spring Boot ORM - Update Project
Let's now add a REST API to our the spring application which can add, edit, delete and display employee(s).
Entity
Create an entity Employee to persist Employee Object to Employee Table.
Employee.java
package com.tutorialspoint.springbootorm.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
// entity class to persist object to Employee Table
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private int age;
private String email;
// setter, getter methods
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Repository
Create a Repository to perform database operations on Employee Object.
EmployeeRepository.java
package com.tutorialspoint.springbootorm.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.tutorialspoint.springbootorm.entity.Employee;
@Repository
public interface EmployeeRepository extends CrudRepository<Employee, Integer> {
}
Service
Create a Service to call repository instance to perform database operations on Employee Object.
EmployeeService.java
package com.tutorialspoint.springbootorm.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tutorialspoint.springbootorm.entity.Employee;
import com.tutorialspoint.springbootorm.repository.EmployeeRepository;
@Service
public class EmployeeService {
@Autowired
EmployeeRepository repository;
// get an employee by id
public Employee getEmployeeById(int id) {
return repository.findById(id).get();
}
// get list of all employees
public List<Employee> getAllEmployees(){
List<Employee> employees = new ArrayList<Employee>();
repository.findAll().forEach(employee -> employees.add(employee));
return employees;
}
// create or update an employee
public void saveOrUpdate(Employee employee) {
repository.save(employee);
}
// delete the employee
public void deleteEmployeeById(int id) {
repository.deleteById(id);
}
}
Controller
Create a controller for REST APIs.
EmployeeController.java
package com.tutorialspoint.springbootorm.controller;
import java.util.List;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tutorialspoint.springbootorm.entity.Employee;
import com.tutorialspoint.springbootorm.service.EmployeeService;
@RestController
@RequestMapping(path = "/emp")
public class EmployeeController {
@Autowired
EmployeeService employeeService;
// GET mapping to get all the employees
@GetMapping("/employees")
public List<Employee> getAllEmployees(){
return employeeService.getAllEmployees();
}
// GET mapping to get an employee by id
@GetMapping("/employee/{id}")
public Employee getEmployee(@PathVariable("id") int id) {
return employeeService.getEmployeeById(id);
}
// DELETE mapping to delete an employee by id
@DeleteMapping("/employee/{id}")
public void deleteEmployee(@PathVariable("id") int id) {
employeeService.deleteEmployeeById(id);
}
// POST mapping to create a new employee
@PostMapping("/employee")
public void addEmployee(@RequestBody Employee employee) {
employeeService.saveOrUpdate(employee);
}
// PUT mapping to update an employee
@PutMapping("/employee")
public void updateEmployee(@RequestBody Employee employee) {
employeeService.saveOrUpdate(employee);
}
}
Main Application
Main Application class to boot the application.
SpringBootOrmApplication.java
package com.tutorialspoint.springbootorm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// Main Application
@SpringBootApplication
public class SpringBootOrmApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootOrmApplication.class, args);
}
}
Advertisements