1. 技术概述
Spring Boot 是基于 Spring Framework 的一种快速开发框架,它通过大量自动配置,充分减少了应用开发过程中的配置成本。它通常被用于构建 RESTful API,小型服务器应用和应用原型。学习 Spring Boot 的原因是它简单易用,并且是现代应用开发的主流选择。技术难点在于类的分层策略,和处理复杂功能时的同步与性能优化。
2. 技术详述
以学生表的 CRUD (创建、读取、更新、删除) 为例,展示如何通过 Spring Boot 实现该功能。
- 项目结构和分层设计
项目使用栈络层模型,包括以下五个分层:
- Controller 层: 处理前端请求,调用 Service 层功能
- Service 层: 逻辑实现,调用 Repository 层
- Repository 层: 访问数据库,实现基础操作
- Entity: 定义学生数据的属性
- DTO: 用于数据传输的分层
- 代码实现
(1) Entity层
import jakarta.persistence.*;
import lombok.Data;
@Data
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String studentNumber;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String contact;
}
(2) DTO 层
import lombok.Data;
@Data
public class StudentDTO {
private String studentNumber;
private String name;
private String contact;
}
(3) Repository 层
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository<Student, Long> {
Student findByStudentNumber(String studentNumber);
}
(4) Service 层
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
public List<Student> getAllStudents() {
return studentRepository.findAll();
}
public Student getStudentByNumber(String studentNumber) {
return studentRepository.findByStudentNumber(studentNumber);
}
@Transactional
public Student createStudent(StudentDTO studentDTO) {
Student student = new Student();
student.setStudentNumber(studentDTO.getStudentNumber());
student.setName(studentDTO.getName());
student.setContact(studentDTO.getContact());
return studentRepository.save(student);
}
@Transactional
public Student updateStudent(String studentNumber, StudentDTO studentDTO) {
Student student = studentRepository.findByStudentNumber(studentNumber);
if (student == null) {
throw new RuntimeException("Student not found.");
}
student.setName(studentDTO.getName());
student.setContact(studentDTO.getContact());
return studentRepository.save(student);
}
@Transactional
public void deleteStudent(String studentNumber) {
Student student = studentRepository.findByStudentNumber(studentNumber);
if (student == null) {
throw new RuntimeException("Student not found.");
}
studentRepository.delete(student);
}
}
(5) Controller 层
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping
public ResponseEntity<List<Student>> getAllStudents() {
return ResponseEntity.ok(studentService.getAllStudents());
}
@GetMapping("/{studentNumber}")
public ResponseEntity<Student> getStudent(@PathVariable String studentNumber) {
return ResponseEntity.ok(studentService.getStudentByNumber(studentNumber));
}
@PostMapping
public ResponseEntity<Student> createStudent(@RequestBody StudentDTO studentDTO) {
return ResponseEntity.ok(studentService.createStudent(studentDTO));
}
@PutMapping("/{studentNumber}")
public ResponseEntity<Student> updateStudent(@PathVariable String studentNumber, @RequestBody StudentDTO studentDTO) {
return ResponseEntity.ok(studentService.updateStudent(studentNumber, studentDTO));
}
@DeleteMapping("/{studentNumber}")
public ResponseEntity<Void> deleteStudent(@PathVariable String studentNumber) {
studentService.deleteStudent(studentNumber);
return ResponseEntity.noContent().build();
}
}
3. 技术使用中的问题与解决
-
问题1:findByStudentId 返回 null
原因:学号字段未正确插入数据库或数据库查询条件不匹配。
解决:检查学号字段是否为唯一约束,并确保插入和查询时学号一致。 -
问题2:启动时报错 Failed to configure a DataSource
原因:未配置数据库连接。
解决:添加数据库依赖,配置 application.properties
4. 总结
通过本次实现,我们学习了 Spring Boot 的基本开发流程,包括分层架构的使用和增删改查的实现。在实际开发中,良好的代码分层和模块化设计能提升可维护性。

6729

被折叠的 条评论
为什么被折叠?



