Spring Boot ORM - 更新專案



我們現在向 Spring 應用程式新增一個 REST API 來新增、編輯、刪除和顯示員工。

實體

建立一個實體 Employee,用於將 Employee 物件保留在 Employee 表中。

Employee.java

package com.tutorialspoint.springbootorm.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

// entity class to persist object to Employee Table
@Entity
public class Employee {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private int id;
   private String name;
   private int age;
   private String email;

   // setter, getter methods
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public int getAge() {
      return age;
   }

   public void setAge(int age) {
      this.age = age;
   }

   public String getEmail() {
      return email;
   }

   public void setEmail(String email) {
      this.email = email;
   }
}

儲存庫

建立一個儲存庫,用於對 Employee 物件執行資料庫操作。

EmployeeRepository.java

package com.tutorialspoint.springbootorm.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.tutorialspoint.springbootorm.entity.Employee;

@Repository
public interface EmployeeRepository extends CrudRepository<Employee, Integer>  {
}

服務

建立一個服務,呼叫儲存庫例項,用於對 Employee 物件執行資料庫操作。

EmployeeService.java

package com.tutorialspoint.springbootorm.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tutorialspoint.springbootorm.entity.Employee;
import com.tutorialspoint.springbootorm.repository.EmployeeRepository;

@Service
public class EmployeeService {

   @Autowired
   EmployeeRepository repository;

   // get an employee by id
   public Employee getEmployeeById(int id) {
      return repository.findById(id).get();
   }

   // get list of all employees
   public List<Employee> getAllEmployees(){
      List<Employee> employees = new ArrayList<Employee>();
      repository.findAll().forEach(employee -> employees.add(employee));
      return employees;
   }

   // create or update an employee
   public void saveOrUpdate(Employee employee) {
      repository.save(employee);
   }
   
   // delete the employee
   public void deleteEmployeeById(int id) {
      repository.deleteById(id);
   }
}

控制器

建立用於 REST API 的控制器。

EmployeeController.java

package com.tutorialspoint.springbootorm.controller;

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;

import com.tutorialspoint.springbootorm.entity.Employee;
import com.tutorialspoint.springbootorm.service.EmployeeService;

@RestController
@RequestMapping(path = "/emp")
public class EmployeeController {

   @Autowired
   EmployeeService employeeService;

   // GET mapping to get all the employees
   @GetMapping("/employees")
   public List<Employee> getAllEmployees(){
      return employeeService.getAllEmployees();
   }

   // GET mapping to get an employee by id
   @GetMapping("/employee/{id}")
   public Employee getEmployee(@PathVariable("id") int id) {
      return employeeService.getEmployeeById(id);
   }

   // DELETE mapping to delete an employee by id
   @DeleteMapping("/employee/{id}")
   public void deleteEmployee(@PathVariable("id") int id) {
      employeeService.deleteEmployeeById(id);
   }

   // POST mapping to create a new employee
   @PostMapping("/employee")
   public void addEmployee(@RequestBody Employee employee) {
      employeeService.saveOrUpdate(employee);
   }
   
   // PUT mapping to update an employee
   @PutMapping("/employee")
   public void updateEmployee(@RequestBody Employee employee) {
      employeeService.saveOrUpdate(employee);
   }	
}

主應用程式

主應用程式類,用於啟動應用程式。

SpringBootOrmApplication.java

package com.tutorialspoint.springbootorm;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// Main Application
@SpringBootApplication
public class SpringBootOrmApplication {
   public static void main(String[] args) {
      SpringApplication.run(SpringBootOrmApplication.class, args);
   }
}
廣告