Springboot
Springboot
a) Add a new employee: The user can enter details like employee name,
department, and salary, and the program should add the employee to the
database.
b) Update employee details: The user can update the name, department, or
salary of an existing employee based on their employee ID.
c) Delete an employee: The user can delete an employee from the database
based on their employee ID.
d) Display all employees: The program should retrieve and display a list of
all employees and their details from the database.
e) Requirements:
i) Use Spring Boot to create the application and Hibernate to manage
the database.
ii) Implement JPA (Java Persistence API) for data access.
iii) Provide a RESTful API for performing CRUD operations on
employees.
iv) Implement exception handling to handle possible errors during
database interactions.
v) Cover Spring Boot and Hibernate topics, such as entity classes,
repositories, services, and controllers.
f) Note: Before running the program, make sure you have set up the
database and configured the connection in the application.properties file.
EmployeeManagementApplication.java
package com.example.employee_management;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class EmployeeManagementApplication {
}
EmployeeController.java
package com.example.employee_management.employee.controller;
import com.example.employee_management.employee.entity.Employee;
import com.example.employee_management.employee.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping
public List<Employee> getAllEmployees() {
return employeeService.getAllEmployees();
}
@GetMapping("/{id}")
public ResponseEntity<Employee> getEmployeeById(@PathVariable Long id)
{
Employee employee = employeeService.getEmployeeById(id)
.orElseThrow(() -> new RuntimeException("Employee not found
with id: " + id));
return ResponseEntity.ok(employee);
}
@PostMapping
public Employee createEmployee(@RequestBody Employee employee) {
return employeeService.saveEmployee(employee);
}
@PostMapping("/batch")
public ResponseEntity<List<Employee>> createEmployees(@RequestBody
List<Employee> employees) {
List<Employee> savedEmployees =
employeeService.saveEmployees(employees);
return ResponseEntity.ok(savedEmployees);
}
@PutMapping("/{id}")
public ResponseEntity<Employee> updateEmployee(@PathVariable Long id,
@RequestBody Employee employeeDetails) {
Employee employee = employeeService.updateEmployee(id,
employeeDetails);
return ResponseEntity.ok(employee);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteEmployee(@PathVariable Long id) {
employeeService.deleteEmployee(id);
return ResponseEntity.noContent().build();
}
}
Employee.java
package com.example.employee_management.employee.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String department;
private Double salary;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<?> handleRuntimeException(RuntimeException ex,
WebRequest request) {
return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<?> handleGlobalException(Exception ex, WebRequest
request) {
return new ResponseEntity<>(ex.getMessage(),
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
EmployeeRepository.java
package com.example.employee_management.employee.repository;
import com.example.employee_management.employee.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
EmployeeService.java
package com.example.employee_management.employee.service;
import com.example.employee_management.employee.entity.Employee;
import
com.example.employee_management.employee.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
employee.setName(employeeDetails.getName());
employee.setDepartment(employeeDetails.getDepartment());
employee.setSalary(employeeDetails.getSalary());
return employeeRepository.save(employee);
}
employeeRepository.delete(employee);
}
}
Output: