Prepared By : Prof. Uday Shah (HOD-IT)
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>4.0.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ruparel</groupId>
<artifactId>first_web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>first_web</name>
<description>First project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
---------------------------------------------------
application.properties
spring.application.name=first_web
spring.datasource.url=jdbc:mysql://localhost:3306/empdb
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
---------------------------------------------------------
Employee.java
//Data Class
//Model Class
//POJO Class
package com.ruparel.first_web;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name="employee")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Double salary;
public Employee() {}
public Employee(Integer id,String name,Double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
}
--------------------------------------------------
EmployeeRepository.java
package com.ruparel.first_web;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
}
-------------------------------------------------
EmployeeService.java
package com.ruparel.first_web;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EmployeeService {
@Autowired
EmployeeRepository employee;
public List<Employee> getAllEmployees(){
return employee.findAll();
}
public Employee getEmployeesById(int id){
return employee.findById(id).orElse(null);
/*for(Employee e : employee){
if(e.getId() == id) {
return e;
}
}
return null;*/
}
// New Record and Update Record
public void saveEmployee(Employee emp) {
employee.save(emp);
//employee.add(emp);
}
public void deleteEmployee(int id) {
employee.deleteById(id);
}
}
---------------------------------------------------
EmployeeController.java
package com.ruparel.first_web;
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;
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@Autowired
EmployeeService service;
@GetMapping
public List<Employee> getAllEmployees()
{
return service.getAllEmployees();
}
@GetMapping("/{id}")
public Employee getEmployee(@PathVariable int id)
{
return service.getEmployeesById(id);
}
@PostMapping
public String saveEmployee(@RequestBody Employee emp) {
service.saveEmployee(emp);
return "Employee Add Successfully...";
}
@PutMapping("/update/{id}")
public String updateEmployee(@PathVariable int id,@RequestBody Employee emp)
{
Employee e = service.getEmployeesById(id);
e.setName(emp.getName());
e.setSalary(emp.getSalary());
service.saveEmployee(emp);
return "Employee Update Successfully";
}
@DeleteMapping("/{id}")
public String deleteEmployee(@PathVariable int id) {
service.deleteEmployee(id);
return "Employee Delete Successfully...";
}
}