Linux云服务器中搭建SpringBoot学生管理系统实战经验分享
2023-09-23 12:07:57
在 Linux 云服务器中搭建 SpringBoot 学生管理系统
安装 JDK 和配置环境变量
搭建学生管理系统的第一步是安装 Java 开发工具包 (JDK) 并配置环境变量。使用 SSH 连接到你的 Linux 云服务器,然后使用以下命令安装 JDK:
sudo apt-get update
sudo apt-get install default-jdk
配置环境变量以使系统能够识别已安装的 JDK,方法是打开 /etc/profile 文件并在文件末尾添加以下内容:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
使更改生效:
source /etc/profile
安装 Maven
Maven 是一个构建工具,可帮助你管理 Java 项目。使用以下命令安装 Maven:
sudo apt-get update
sudo apt-get install maven
创建项目
使用以下命令创建一个新的 Maven 项目:
mvn archetype:generate -DgroupId=com.example -DartifactId=student-management-system -Dpackage=com.example.student -Dversion=1.0.0
安装数据库
我们将使用 MySQL 作为数据库。使用以下命令安装 MySQL:
sudo apt-get update
sudo apt-get install mysql-server
配置数据库连接池
数据库连接池管理数据库连接并提高应用程序性能。我们将使用 HikariCP 作为连接池。在你的 pom.xml 文件中添加以下依赖:
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
</dependency>
在 application.properties 文件中添加以下配置:
spring.datasource.url=jdbc:mysql://localhost:3306/student_management_system
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.maximumPoolSize=10
spring.datasource.hikari.idleTimeout=30000
配置 Spring Security
Spring Security 是一个安全框架,可帮助保护你的应用程序。在你的 pom.xml 文件中添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.6.4</version>
</dependency>
在 application.properties 文件中添加以下配置:
spring.security.user.name=admin
spring.security.user.password=password
编写 Controller
Controller 类负责处理 HTTP 请求。在 src/main/java/com.example.student.controller 包下创建一个 StudentController 类并添加以下代码:
@RestController
@RequestMapping("/api/students")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping
public List<Student> getAllStudents() {
return studentService.getAllStudents();
}
@GetMapping("/{id}")
public Student getStudentById(@PathVariable Long id) {
return studentService.getStudentById(id);
}
@PostMapping
public Student createStudent(@RequestBody Student student) {
return studentService.createStudent(student);
}
@PutMapping("/{id}")
public Student updateStudent(@PathVariable Long id, @RequestBody Student student) {
return studentService.updateStudent(id, student);
}
@DeleteMapping("/{id}")
public void deleteStudent(@PathVariable Long id) {
studentService.deleteStudent(id);
}
}
编写 Service
Service 类包含业务逻辑。在 src/main/java/com.example.student.service 包下创建一个 StudentService 类并添加以下代码:
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
public List<Student> getAllStudents() {
return studentRepository.findAll();
}
public Student getStudentById(Long id) {
return studentRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Student not found with id :" + id));
}
public Student createStudent(Student student) {
return studentRepository.save(student);
}
public Student updateStudent(Long id, Student student) {
Student existingStudent = studentRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Student not found with id :" + id));
existingStudent.setName(student.getName());
existingStudent.setEmail(student.getEmail());
return studentRepository.save(existingStudent);
}
public void deleteStudent(Long id) {
studentRepository.deleteById(id);
}
}
编写 Repository
Repository 类用于数据访问。在 src/main/java/com.example.student.repository 包下创建一个 StudentRepository 接口并添加以下代码:
public interface StudentRepository extends JpaRepository<Student, Long> {
}
启动项目
在命令行中导航到项目目录并执行以下命令以启动项目:
mvn spring-boot:run
访问项目
在浏览器中输入以下地址:
http://localhost:8080/api/students
你将看到所有学生的信息。
常见问题解答
- 如何添加新学生?
使用 POST 请求并提供学生的 JSON 表示,如下所示:
{
"name": "John Doe",
"email": "john.doe@example.com"
}
- 如何获取特定学生?
使用 GET 请求并指定学生的 ID,如下所示:
http://localhost:8080/api/students/1
- 如何更新学生?
使用 PUT 请求并提供学生的更新后的 JSON 表示,如下所示:
{
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com"
}
- 如何删除学生?
使用 DELETE 请求并指定学生的 ID,如下所示:
http://localhost:8080/api/students/1
- 如何保护我的应用程序免受未经授权的访问?
Spring Security 已被配置为使用用户名“admin”和密码“password”进行身份验证。