项目结构
flowchart TB client(客户端浏览器) API(API层: controller) service(service层: service) dataAccess(data access层: dao) database[(数据库)] client-->API-->service-->dataAccess-->database database-->dataAccess-->service-->API-->client
RESTful API 规范
路径
路径又称”终点”(endpoint),表示API的具体网址。
在 RESTful 架构中,每个网址代表一种资源(resource),所以网址中不能有动词,只能有名词,而且所用的名词往往与数据库的表格名对应。
RESTful API 的请求方式与资源的关系
- GET: 获取资源
- POST: 创建资源
- PATCH: 部分更新
- PUT: 更新资源
- DELETE: 删除资源
Controller 测试
package com.example.springbootproject02;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class TestController {
@RequestMapping("/hello")
public String hello() {
return "Hello World";
}
@RequestMapping("/userList")
public List<String> userList() {
return List.of("Erik", "Tom");
}
}实例:根据 id 获取学生信息
创建数据库
create database springbootdemo;
use springbootdemo;
create table student (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
age INT
);连接数据库
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdemo?characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=995983
实现 data access 层
Student.java
package com.example.springbootproject02.dao;
import jakarta.persistence.*;
import static jakarta.persistence.GenerationType.IDENTITY;
@Entity
@Table(name="student")
public class Student {
@Id
@Column(name="id")
@GeneratedValue(strategy=IDENTITY)
private long id;
@Column(name="name")
private String name;
@Column(name="email")
private String email;
@Column(name="age")
private int age;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}StudentRepository.java
package com.example.springbootproject02.dao;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository<Student, Long> {
}实现 service 层
StudentService.java
package com.example.springbootproject02.service;
import com.example.springbootproject02.dao.Student;
public interface StudentService {
Student getStudentById(long id);
}StudentServiceImpl.java
package com.example.springbootproject02.service;
import com.example.springbootproject02.dao.Student;
import com.example.springbootproject02.dao.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentRepository studentRepository;
@Override
public Student getStudentById(long id) {
return studentRepository.findById(id).orElseThrow(RuntimeException::new);
}
}实现 API 层
StudentController.java
package com.example.springbootproject02.controller;
import com.example.springbootproject02.dao.Student;
import com.example.springbootproject02.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/student/{id}")
public Student getStudentById(@PathVariable long id) {
return studentService.getStudentById(id);
}
}依赖关系
flowchart TB Client("Client") subgraph Controller StudentController(StudentController) end subgraph Service StudentService(StudentService) StudentServiceImpl(StudentServiceImpl) StudentService-->|实现|StudentServiceImpl end subgraph Dao Student(Student) StudentRepository(StudentRepository) StudentRepository-->|依赖|Student end Client-->|请求|Controller-->|依赖|StudentService StudentServiceImpl-->|依赖|StudentRepository