Step 1: Create Maven project
Following pom.xml defines the dependencies for this example.
<?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.3.2.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.smoothexample</groupId>
<artifactId>spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test </scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step 2: Create a Model for the Project
Following file defines the MODEL for this example.
package com.smoothexample.spring.model;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "first_name")
private String first_name;
@Column(name = "last_name")
private String last_name;
@Column(name = "address")
private String address;
@Column(name = "language")
private String language;
public Employee() {
}
public Employee(long id, String first_name, String last_name, String address, String language) {
this.id = id;
this.first_name = first_name;
this.last_name = last_name;
this.address = address;
this.language = language;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language= language;
}
}
Step 3: Create Repository
Following file defines the Repository for this example.
package com.smoothexample.spring.repository;
import com.smoothexample.spring.model.Employee;
import org.springframework.data.repository.CrudRepository;
public interface EmployeeRepository extends CrudRepository<Employee, Long> {
}
Step 4: Create Service
In the following file can define the Service for this example
package com.smoothexample.spring.service;
import com.smoothexample.spring.model.Employee;
import java.util.List;
public interface EmployeeService {
public List<Employee> getAllEmployee();
public Employee getByid(Long id);
public void saveorupdate(Employee employee);
public void deleteEmployee(Long id);
void updateEmployee(Employee employee);
}
Step 5: Create ServiceImpl
Following file defines the ServiceImpl for this example.
package com.smoothexample.service;
import com.smoothexample.model.Employee;
import com.smoothexample.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
EmployeeRepository employeeRepository;
@Override
public List< Employee> getAllEmployee() {
return (List< Employee>)employeeRepository.findAll();
}
@Override
public Employee getByid(Long id) {
return employeeRepository.findById(id).get();
}
@Override
public void saveorupdate(Employee employee) {
employeeRepository.save(employee);
}
@Override
public void updateEmployee(Employee employee) {
Optional< Employee> empDb = this.employeeRepository.findById(employee.getId());
if (empDb.isPresent()) {
Employee courseUpdate = empDb.get();
employee.setId(employee.getId());
employee.setFirst_name(employee.getFirst_name());
employee.setLast_name(employee.getLast_name());
employee.setAddress(employee.getAddress());
employee.setLanguage(employee.getLanguage());
employeeRepository.save(employee);
}
}
@Override
public void deleteEmployee(Long id) {
employeeRepository.deleteById(id);
}
}
Step 6: Create Controller
Implements the DELETE METHOD in controller.
In here it display all data’s from database.
package com.smoothexample.spring.controller;
import com.smoothexample.spring.model.Employee;
import com.smoothexample.spring.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@EnableAutoConfiguration
@RequestMapping(value = "/api")
public class EmployeeController {
@Autowired
EmployeeService employeeService;
@DeleteMapping("/delete/{id}")
public String deleteEmployee(@PathVariable (value = "id") Long id){
employeeService.deleteEmployee(id);
return "delete successfully"+id;
}
}
Result
http://localhost:8080/api/delete/3
delete successfully
OUTPUT