Spring Boot JPA - 應用設定



正如在上一章環境設定中,我們已將生成的 Spring Boot 專案匯入到 Eclipse 中。現在讓我們在src/main/java資料夾中建立以下結構。

Project Structure
  • com.tutorialspoint.controller.EmployeeController - 一個基於 REST 的控制器,用於實現基於 REST 的 API。

  • com.tutorialspoint.entity.Employee - 一個實體類,表示資料庫中相應的表。

  • com.tutorialspoint.repository.EmployeeRepository - 一個倉庫介面,用於在資料庫上實現 CRUD 操作。

  • com.tutorialspoint.service.EmployeeService - 一個服務類,用於在倉庫函式上實現業務操作。

  • com.tutorialspoint.springbooth2.SprintBootH2Application - 一個 Spring Boot 應用類。

SprintBootH2Application 類已存在。我們需要建立上述包和相關的類以及介面,如下所示:

實體 - Entity.java

以下是 Employee 的預設程式碼。它表示一個 Employee 表,包含 id、name、age 和 email 列。

package com.tutorialspoint.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table
public class Employee {
   @Id
   @Column
   private int id;

   @Column
   private String name;

   @Column
   private int age;

   @Column
   private String email;

   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;
   }
}

倉庫 - EmployeeRepository.java

以下是倉庫的預設程式碼,用於在上述實體 Employee 上實現 CRUD 操作。

package com.tutorialspoint.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.tutorialspoint.entity.Employee;

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

服務 - EmployeeService.java

以下是服務的預設程式碼,用於在倉庫函式上實現操作。

package com.tutorialspoint.service;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tutorialspoint.entity.Employee;
import com.tutorialspoint.repository.EmployeeRepository;

@Service
public class EmployeeService {
   @Autowired
   EmployeeRepository repository;

   public Employee getEmployeeById(int id) {
      return repository.findById(id).get();
   }
   public List<Employee> getAllEmployees(){
      List<Employee> employees = new ArrayList<Employee>();
      repository.findAll().forEach(employee -> employees.add(employee));
      return employees;
   }
   public void saveOrUpdate(Employee employee) {
      repository.save(employee);
   }
   public void deleteEmployeeById(int id) {
      repository.deleteById(id);
   }
}

控制器 - EmployeeController.java

以下是控制器的預設程式碼,用於實現 REST API。

package com.tutorialspoint.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.entity.Employee;
import com.tutorialspoint.service.EmployeeService;

@RestController
@RequestMapping(path = "/emp")
public class EmployeeController {
   @Autowired
   EmployeeService employeeService;

   @GetMapping("/employees")
   public List<Employee> getAllEmployees(){
      return employeeService.getAllEmployees();
   }
   @GetMapping("/employee/{id}")
   public Employee getEmployee(@PathVariable("id") int id) {
      return employeeService.getEmployeeById(id);
   }
   @DeleteMapping("/employee/{id}")
   public void deleteEmployee(@PathVariable("id") int id) {
      employeeService.deleteEmployeeById(id);
   }
   @PostMapping("/employee")
   public void addEmployee(@RequestBody Employee employee) {
      employeeService.saveOrUpdate(employee);   
   }
   @PutMapping("/employee")
   public void updateEmployee(@RequestBody Employee employee) {
      employeeService.saveOrUpdate(employee);
   }	
}

應用 - SprintBootH2Application.java

以下是應用的更新程式碼,用於使用上述類。

package com.tutorialspoint.sprintbooth2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@ComponentScan({"com.tutorialspoint.controller","com.tutorialspoint.service"})
@EntityScan("com.tutorialspoint.entity")
@EnableJpaRepositories("com.tutorialspoint.repository")
@SpringBootApplication
public class SprintBootH2Application {
   public static void main(String[] args) {
      SpringApplication.run(SprintBootH2Application.class, args);
   }
}

執行/除錯配置

在 Eclipse 中建立以下Maven 配置,以使用目標spring-boot:run執行 Spring Boot 應用。此配置將幫助執行 REST API,我們可以使用 Postman 對其進行測試。

Maven Configuration

執行應用

在 Eclipse 中,執行Employee 應用配置。Eclipse 控制檯將顯示類似的輸出。

[INFO] Scanning for projects...
...
2021-07-24 20:51:14.823  INFO 9760 --- [restartedMain] c.t.s.SprintBootH2Application: 
Started SprintBootH2Application in 7.353 seconds (JVM running for 8.397)

伺服器啟動並執行後,使用 Postman 發出 POST 請求以首先新增記錄。

在 Postman 中設定以下引數。

  • HTTP 方法 - POST

  • URL - https://:8080/emp/employee

  • 主體 - 一個員工 JSON

{  
   "id": "1",  
   "age": "35",  
   "name": "Julie",  
   "email": "julie@gmail.com"  
}   

點擊發送按鈕並檢查響應狀態是否為 OK。現在發出 GET 請求以獲取所有記錄。

在 Postman 中設定以下引數。

  • HTTP 方法 - GET

  • URL - https://:8080/emp/employees

點擊發送按鈕並驗證響應。

[{  
   "id": "1",  
   "age": "35",  
   "name": "Julie",  
   "email": "julie@gmail.com"  
}]   
廣告

© . All rights reserved.