一个完整的 Java 项目的运行方案
1. 项目概述
本项目是一个简单的学生管理系统,实现了学生信息的增删改查等基本功能。项目使用 Java 编写,采用 Maven 进行依赖管理,使用 Spring Boot 框架进行开发。
2. 环境准备
- JDK 1.8 及以上版本
- Maven 3.6.3 及以上版本
- IntelliJ IDEA 或其他 Java 开发工具
3. 项目结构
├── src/main/java
│ ├── com.example
│ │ ├── controller // 控制器层
│ │ ├── model // 数据模型层
│ │ ├── repository // 数据库访问层
│ │ ├── service // 服务层
│ │ └── Application.java // 项目入口
├── src/main/resources
│ ├── application.properties // 项目配置文件
│ ├── static // 静态资源文件
│ └── templates // 模板文件
└── pom.xml // Maven 依赖配置文件
4. 项目依赖
在 pom.xml 中添加以下依赖:
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 数据库 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
5. 数据库配置
在 application.properties 文件中配置数据库连接信息:
spring.datasource.url=jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
6. 创建数据模型
创建一个名为 Student
的数据模型类,代码如下:
package com.example.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private int age;
private String gender;
// 省略 getter 和 setter 方法
}
7. 创建数据库访问层
创建一个名为 StudentRepository
的接口,继承自 JpaRepository
,用于访问数据库:
package com.example.repository;
import com.example.model.Student;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository<Student, Long> {
}
8. 创建服务层
创建一个名为 StudentService
的服务类,处理学生信息的增删改查等操作:
package com.example.service;
import com.example.model.Student;
import com.example.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
public List<Student> getAllStudents() {
return studentRepository.findAll();
}
public Student getStudentById(Long id) {
Optional<Student> optionalStudent = studentRepository.findById(id);
return optionalStudent.orElse(null);
}
public void addStudent(Student student) {
studentRepository.save(student);
}
public void updateStudent(Long id, Student student) {
Optional<Student> optionalStudent = studentRepository.findById(id);
if (optionalStudent.isPresent()) {
Student existingStudent = optionalStudent.get();
existingStudent.setName(student.getName());
existingStudent.setAge(student.getAge());
existingStudent.setGender(student.getGender());
studentRepository.save(existingStudent);
}
}
public void deleteStudent(Long id) {
studentRepository.deleteById(id);
}
}
9. 创建控制器层
创建一个名为 StudentController
的控制器类,处理学生信息的 RESTful API 请求:
package com.example.controller;
import com.example.model.Student;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;