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);
   }
}
广告