SpringBoot Assignment - Pravardhan Sajjan
SpringBoot Assignment - Pravardhan Sajjan
Batch: 16-AUG-FullStack-Bangalore-Offline
Topic: Spring Boot CRUD Application
Q:
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.
CODE:
package com.springboot.crud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
Exployees.java
package com.springboot.crud;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "employees")
public class Employee {
@Id
private Long id;
// Constructors
public Employee() {
}
// toString() method
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", department='" + department + '\'' +
", salary=" + salary +
'}';
}
}
EmployeeController.java
package com.springboot.crud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping
public List<Employee> getAllEmployees() {
return employeeService.getAllEmployees();
}
@GetMapping("/{id}")
public Employee getEmployeeById(@PathVariable Long id) {
return employeeService.getEmployeeById(id);
}
@PostMapping
public Employee addEmployee(@RequestBody Employee employee) {
return employeeService.addEmployee(employee);
}
@PutMapping("/{id}")
public Employee updateEmployee(@PathVariable Long id, @RequestBody
Employee employee) {
return employeeService.updateEmployee(id, employee);
}
@DeleteMapping("/{id}")
public void deleteEmployee(@PathVariable Long id) {
employeeService.deleteEmployee(id);
}
}
EmployeeRepository.java
package com.springboot.crud;
import org.springframework.data.jpa.repository.JpaRepository;
EmployeeService.java
package com.springboot.crud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
existingEmployee.setDepartment(updatedEmployee.getDepartment());
existingEmployee.setSalary(updatedEmployee.getSalary());
return employeeRepository.save(existingEmployee);
}
return null;
}
application.properties
spring.application.name=com.springboot.crud
# Database Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/employee_db
spring.datasource.username=root
spring.datasource.password=SQLserver@123
# Hibernate Configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>com.springboot.crud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>com.springboot.crud</name>
<description>Assignment</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MySQL Driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version> <!-- or the latest version -->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
MySQL Database
POST Method:
URL: http://localhost:8080/employees
GET Method:
URL: http://localhost:8080/employees
Delete Method:
URL: http://localhost:8080/employees/1
PUT Method:
URL: http://localhost:8080/employees/2