1.创建春季启动应用程序
Spring Boot提供了一个名为Spring Initializer的Web工具,以快速创建应用程序。只需转到https://start.spring.io/并生成一个新的Spring Boot项目。
在春季启动创建中使用以下详细信息:
项目名称:Springboot-Backend
项目类型:Maven
选择依赖项:春季网络,Lombok,Spring Data JPA和MySQL驱动程序
软件包名称:springboot-backend.demo
包装:jar
java:8
将Spring Boot Project作为ZIP文件下载,解压缩并将其导入Intellij Idea。
2.将项目演示打开到Intellij Idea中
3.配置MySQL数据库
不要根据您的MySQL安装更改Spring.datasource.username和Spring.datasource.password。另外,在继续前往下一节之前,请创建一个名为MySQL中所需的数据库。
spring.datasource.url=jdbc:mysql://localhost:3306/gestionemployees?useSSL=false
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto = update
您不需要创建任何表。表将通过Employee
实体的Hibernate自动创建,我们将在下一步中定义。属性spring.jpa.hibernate.ddl-auto = update
4.创建JPA实体
转到模型软件包,创建一个名为“员工”的类,然后将以下内容添加到其中:
package net.javaguides.springboot.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import jakarta.persistence.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "firstname")
private String firstName;
@Column(name = "lastname")
private String lastName;
@Column(name = "email")
private String emailId;
}
- 创建春季数据JPA存储库
package repository;
import model.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
// all crud database methods
}
- 创建ResourcenotFoundException自定义异常
package exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException{
public ResourceNotFoundException(String message){
super(message);
}
}
7.创建春季靴子CRUD REST API
转到控制器软件包,创建一个名为lospereeecontroller的类,然后添加以下内容:
package controller;
import model.Employee;
import repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import exception.ResourceNotFoundException;
import java.util.List;
@CrossOrigin("*")
@RestController
@RequestMapping("/api/v1/employees")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
@GetMapping
public List<Employee> getAllEmployees(){
return employeeRepository.findAll();
}
// build create employee REST API
@PostMapping
public Employee createEmployee(@RequestBody Employee employee) {
return employeeRepository.save(employee);
}
// build get employee by id REST API
@GetMapping("{id}")
public ResponseEntity<Employee> getEmployeeById(@PathVariable long id){
Employee employee = employeeRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Employee not exist with id:" + id));
return ResponseEntity.ok(employee);
}
// build update employee REST API
@PutMapping("{id}")
public ResponseEntity<Employee> updateEmployee(@PathVariable long id,@RequestBody Employee employeeDetails) {
Employee updateEmployee = employeeRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Employee not exist with id: " + id));
updateEmployee.setFirstName(employeeDetails.getFirstName());
updateEmployee.setLastName(employeeDetails.getLastName());
updateEmployee.setEmailId(employeeDetails.getEmailId());
employeeRepository.save(updateEmployee);
return ResponseEntity.ok(updateEmployee);
}
// build delete employee REST API
@DeleteMapping("{id}")
public ResponseEntity<HttpStatus> deleteEmployee(@PathVariable long id){
Employee employee = employeeRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Employee not exist with id: " + id));
employeeRepository.delete(employee);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
8. Running the Application
我们成功地为员工模型开发了所有CRUD REST API。现在是时候将我们的应用程序部署在servlet容器(嵌入式tomcat)中了。
我们可以启动独立的Spring Boot应用程序的两种方式。
- 从应用程序的根目录中键入以下命令以运行它 -
mvn spring-boot:run