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://127.0.0.1:8080/emp/employee

  • 主体 - 一个员工 JSON

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

点击发送按钮并检查响应状态是否为 OK。现在发出 GET 请求以获取所有记录。

在 Postman 中设置以下参数。

  • HTTP 方法 - GET

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

点击发送按钮并验证响应。

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