0% found this document useful (0 votes)
0 views

Springboot

The document outlines the design of a Spring Boot CRUD application for managing employee records using Hibernate. It includes specifications for adding, updating, deleting, and displaying employee details, along with requirements for implementing RESTful APIs and exception handling. The document also provides code snippets for key components such as the application main class, controller, entity, service, repository, and global exception handler.

Uploaded by

52Viraj Gavas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Springboot

The document outlines the design of a Spring Boot CRUD application for managing employee records using Hibernate. It includes specifications for adding, updating, deleting, and displaying employee details, along with requirements for implementing RESTful APIs and exception handling. The document also provides code snippets for key components such as the application main class, controller, entity, service, repository, and global exception handler.

Uploaded by

52Viraj Gavas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1) Design a Spring Boot program to create a CRUD (Create, Read, Update, Delete)

application using Hibernate for managing employee records. The program


should allow users to perform the following operations on the employee
database:

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 {

public static void main(String[] args) {


SpringApplication.run(EmployeeManagementApplication.class, args);
}

}
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;

// Getters and Setters

public Long getId() {


return id;
}

public void setId(Long id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getDepartment() {


return department;
}

public void setDepartment(String department) {


this.department = department;
}

public Double getSalary() {


return salary;
}

public void setSalary(Double salary) {


this.salary = salary;
}
}
GlobalExceptionHandler.java
package com.example.employee_management.employee.exception;

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;

public interface EmployeeRepository extends JpaRepository<Employee, Long> {


}

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();
}

public Optional<Employee> getEmployeeById(Long id) {


return employeeRepository.findById(id);
}

public Employee saveEmployee(Employee employee) {


return employeeRepository.save(employee);
}

public List<Employee> saveEmployees(List<Employee> employees) {


return employeeRepository.saveAll(employees);
}

public Employee updateEmployee(Long id, Employee employeeDetails) {


Employee employee = employeeRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Employee not found
with id: " + id));

employee.setName(employeeDetails.getName());
employee.setDepartment(employeeDetails.getDepartment());
employee.setSalary(employeeDetails.getSalary());
return employeeRepository.save(employee);
}

public void deleteEmployee(Long id) {


Employee employee = employeeRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Employee not found
with id: " + id));

employeeRepository.delete(employee);
}
}
Output:

You might also like