springboot实现学生管理系统

实现学生管理系统是一个常见的Web开发入门项目。下面我将基于Spring Boot框架,讲解如何实现一个简单的学生管理系统。

1. 环境准备

在开始之前,需要准备好以下软件或工具:

  • JDK 1.8 或以上版本
  • IntelliJ IDEA 或其他Java开发工具
  • MySQL 数据库
  • Maven 依赖管理工具

2. Spring Boot 项目初始化

  1. 在 IntelliJ IDEA 中选择 File->New->Project,选择Spring Initializr创建一个新的Spring Boot项目。
  2. 输入项目信息后,选择生成项目的依赖,可以选择 Web、JPA、MySQL等依赖。
  3. 等待 IDEA 生成项目骨架,初始化完成。

3. 数据库设计

在MySQL中创建一个名为student_management的数据库,包含以下两个表:

学生表

字段名 类型 描述
id bigint 学生ID
name varchar(64) 学生姓名
gender varchar(2) 性别
age int 年龄

成绩表

字段名 类型 描述
id bigint 学生ID
course varchar(64) 课程
score int 成绩

4. 实现学生管理接口

  1. 创建一个Student实体类,包含学生基本信息的字段和Getter/Setter方法
@Entity
@Data
public class Student {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;
    private String name;
    private String gender;
    private Integer age;
}
  1. 创建一个Student的JpaRepository,提供基本的CRUD方法
public interface StudentRepository extends JpaRepository<Student, Long> {
}
  1. 创建一个StudentController,实现学生信息的增删查改功能,代码如下:
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentRepository studentRepository;

    @PostMapping("/")
    public Student createStudent(@RequestBody Student student){
        return studentRepository.save(student);
    }

    @GetMapping("/{id}")
    public Student getStudentById(@PathVariable Long id){
        return studentRepository.findById(id).get();
    }

    @PutMapping("/{id}")
    public Student updateStudent(@PathVariable Long id, @RequestBody Student student){
        student.setId(id);
        return studentRepository.save(student);
    }

    @DeleteMapping("/{id}")
    public void deleteStudent(@PathVariable Long id){
        studentRepository.deleteById(id);
    }

    @GetMapping("/")
    public List<Student> getAllStudents(){
        return studentRepository.findAll();
    }
}
  1. 在resources/application.properties中配置与MySQL的连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/student_management?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.format_sql=true
  1. 启动应用,访问http://localhost:8080/student即可测试以上学生信息的增删查改接口。

5. 实现课程成绩接口(示例二)

  1. 创建一个Score实体类,包含成绩信息的字段和Getter/Setter方法
@Entity
@Data
public class Score {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;
    private String course;
    private Integer score;
    @ManyToOne
    @JoinColumn(name = "student_id")
    private Student student;
}
  1. 修改Student实体类,使Score与Student之间存在一对多的关系
@Entity
@Data
public class Student {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;
    private String name;
    private String gender;
    private Integer age;
    @OneToMany(mappedBy = "student", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    private List<Score> scores;
}
  1. 创建一个Score的JpaRepository
public interface ScoreRepository extends JpaRepository<Score, Long> {
    List<Score> findByStudentId(Long studentId);
}
  1. 创建一个ScoreController,实现课程成绩的增删查改和查询某个学生的所有课程成绩等功能,代码如下:
@RestController
@RequestMapping("/score")
public class ScoreController {
    @Autowired
    private ScoreRepository scoreRepository;

    @PostMapping("/")
    public Score createScore(@RequestBody Score score){
        return scoreRepository.save(score);
    }

    @GetMapping("/{id}")
    public Score getScoreById(@PathVariable Long id){
        return scoreRepository.findById(id).get();
    }

    @GetMapping("/")
    public List<Score> getAllScores(){
        return scoreRepository.findAll();
    }

    @GetMapping("/student/{id}")
    public List<Score> getScoreByStudentId(@PathVariable Long id){
        return scoreRepository.findByStudentId(id);
    }

    @PutMapping("/{id}")
    public Score updateScore(@RequestBody Score score){
        return scoreRepository.save(score);
    }

    @DeleteMapping("/{id}")
    public void deleteScore(@PathVariable Long id){
        scoreRepository.deleteById(id);
    }
}
  1. 启动应用,访问http://localhost:8080/score可测试以上课程成绩的增删查改和查询某个学生的所有课程成绩接口。

至此,一个简单的学生管理系统的基本功能已经实现。通过此项目,我们不仅学会了Spring Boot项目的初始化、数据库设计,还学会了如何构建简单的REST API。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:springboot实现学生管理系统 - Python技术站

(0)
上一篇 2023年5月15日
下一篇 2023年5月15日

相关文章

  • java实现操作系统中的最佳置换Optimal算法

    Java实现操作系统中的最佳置换Optimal算法攻略 算法介绍 最佳置换算法(Optimal)也称为理论最优算法。其思想是根据还未完成的进程未来的使用情况,计算出每一个进程在什么时候会访问页面,然后选择最长时间以后才用到的页面进行置换。 实现步骤 首先根据需要分配的内存大小,将所有的物理块置为空闲状态,并初始化所有页面的最近使用时间为正无穷大。 当一个新页…

    Java 2023年5月19日
    00
  • 关于SpringMVC对Restful风格的支持详解

    关于SpringMVC对Restful风格的支持详解 在Web开发中,RESTful风格的API设计已经成为了一种趋势。SpringMVC作为一个流行的Web框架,也提供了对RESTful风格的支持。本文将详细讲解SpringMVC对RESTful风格的支持,包括如何使用@RequestMapping注解、如何使用@PathVariable注解、如何使用@R…

    Java 2023年5月18日
    00
  • HTML实现title 属性换行小技巧

    当我们在HTML标记中使用title属性时,有时候需要在倒数第二个单词之后添加一个换行符。这个时候我们可以用一些小技巧来完成。 方法一:使用实体字符 HTML中有几个实体字符可以用于在title属性中添加换行: &#13; 或 &#x0D; 表示回车 &#10; 或 &#x0A; 表示换行 代码示例: <a href=&…

    Java 2023年6月15日
    00
  • java 两个数组合并的几种方法

    Java两个数组合并的几种方法 介绍 在Java中,有时候需要将两个数组合并成一个数组。本文将介绍Java中合并两个数组的几种方法。 方法一:使用for循环 首先,我们可以使用for循环来合并两个数组。具体的操作是,将第一个数组的元素复制到新的数组中,然后将第二个数组的元素复制到新的数组中。 示例代码: public static int[] mergeAr…

    Java 2023年5月27日
    00
  • 七个Spring核心模块详解

    下面是关于“七个Spring核心模块详解”的完整攻略,包含两个示例说明。 七个Spring核心模块详解 Spring框架是一个开源的JavaEE应用程序框架,它提供了一系列的核心模块,用于简化企业级应用程序的开发。下面我们将详细介绍Spring框架的七个核心模块。 1. Spring Core Spring Core是Spring框架的核心模块,它提供了Io…

    Java 2023年5月17日
    00
  • Java之String.format()方法案例讲解

    下面将详细讲解“Java之String.format()方法案例讲解”的完整攻略。 1. String.format()方法介绍 String.format()方法是Java中的一个常用方法,用于格式化字符串。该方法的语法如下: public static String format(String format, Object… args) 其中,第一个…

    Java 2023年5月26日
    00
  • Sprint Boot @Validated使用方法详解

    Spring Boot的@Validated的作用与使用方法 在Spring Boot中,@Validated注解用于对方法参数进行校验。通过使用@Validated注解,可以确保方法参数满足特定的条件,从而提高应用程序的健壮性和可靠性。 @Validated注解的作用 @Validated注解用于对方法参数进行校验。当使用@Validated注解标记一个方…

    Java 2023年5月5日
    00
  • JS文本框不能输入空格验证方法

    确保JS文本框输入内容不包含空格可以通过验证输入内容的方法来实现。以下是实现JS文本框不能输入空格的完整步骤: 第一步:获取文本框中用户输入的内容 使用 JavaScript 获取该文本框中用户输入的内容,可以使用 document.getElementById() 方法或其他选择器。 let userInput = document.getElementB…

    Java 2023年6月15日
    00
合作推广
合作推广
分享本页
返回顶部