Spring Assign
Spring Assign
java
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
<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