commit cd1dddb798b6b1917cb6c7a2e33d133267eb5986 Author: 赵亚鹏 <13666640+zhao-yapengaaa@user.noreply.gitee.com> Date: Fri Nov 7 11:30:56 2025 +0800 目前完成了所有的功能测试,准备进行优化 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..f80780a --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..63e9001 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..82dbec8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000..c0bcafe --- /dev/null +++ b/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,3 @@ +wrapperVersion=3.3.4 +distributionType=only-script +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.11/apache-maven-3.9.11-bin.zip diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..67c555d --- /dev/null +++ b/pom.xml @@ -0,0 +1,94 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.5.7 + + + cn.zyp.stuSystem + StuSystem + 0.0.1-SNAPSHOT + StuSystem + StuSystem + + + + + + + + + + + + + + + 17 + + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 3.0.5 + + + + com.mysql + mysql-connector-j + runtime + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + com.baomidou + mybatis-plus-boot-starter + 3.5.5 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + org.projectlombok + lombok + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/src/main/java/cn/zyp/stusystem/StuSystemApplication.java b/src/main/java/cn/zyp/stusystem/StuSystemApplication.java new file mode 100644 index 0000000..b4f52ba --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/StuSystemApplication.java @@ -0,0 +1,13 @@ +package cn.zyp.stusystem; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class StuSystemApplication { + + public static void main(String[] args) { + SpringApplication.run(StuSystemApplication.class, args); + } + +} diff --git a/src/main/java/cn/zyp/stusystem/config/MyBatisPlusConfig.java b/src/main/java/cn/zyp/stusystem/config/MyBatisPlusConfig.java new file mode 100644 index 0000000..2fff08c --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/config/MyBatisPlusConfig.java @@ -0,0 +1,18 @@ +package cn.zyp.stusystem.config; + +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MyBatisPlusConfig { + + // 配置分页插件 + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor() { + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); + return interceptor; + } +} \ No newline at end of file diff --git a/src/main/java/cn/zyp/stusystem/controller/ClassController.java b/src/main/java/cn/zyp/stusystem/controller/ClassController.java new file mode 100644 index 0000000..5c5b3da --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/controller/ClassController.java @@ -0,0 +1,66 @@ +package cn.zyp.stusystem.controller; + + +import cn.zyp.stusystem.entity.GradeClass; +import cn.zyp.stusystem.service.ClassService; +import org.apache.ibatis.annotations.Param; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/api/class") +public class ClassController { + + @Autowired + private ClassService classService; + + //根据年级查询班级列表 + @GetMapping("/list") + public Map getClassList(@RequestParam Integer grade,@RequestParam(required = false) String name){ + List gradeClasses = classService.findClassByGradeId(grade); + System.out.println(gradeClasses); + + //过滤班级名称 + if(name != null){ + gradeClasses= gradeClasses.stream().filter(gradeClass -> gradeClass.getName().contains(name)).toList(); + } + Map result = new HashMap<>(); + result.put("data",gradeClasses); + return result; + } + + //新增班级 + @PostMapping("/add") + public boolean addClass(@RequestBody GradeClass gradeClass){ + return classService.save(gradeClass); + } + + //编辑班级 + @PutMapping("/edit") + public boolean editClass(@RequestBody GradeClass gradeClass){ + return classService.updateById(gradeClass); + } + + //删除班级 + @DeleteMapping("/delete/{id}") + public boolean deleteClass(@PathVariable Integer id){ + if(classService.isClassHasStudent(id)){ + return false; + }else { + return classService.removeById(id); + } + } + + @GetMapping("/check") + //检查班级是否有学生 + public Map checkClassHasStudent(@RequestParam Integer classId){ + Map result = new HashMap<>(); + result.put("data",classService.isClassHasStudent(classId)); + return result; + } +} diff --git a/src/main/java/cn/zyp/stusystem/controller/StudentController.java b/src/main/java/cn/zyp/stusystem/controller/StudentController.java new file mode 100644 index 0000000..2d831d6 --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/controller/StudentController.java @@ -0,0 +1,63 @@ +package cn.zyp.stusystem.controller; + + +import cn.zyp.stusystem.entity.Student; +import cn.zyp.stusystem.service.StudentService; +import com.baomidou.mybatisplus.core.metadata.IPage; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +@RestController +@RequestMapping("/api/student") +public class StudentController { + + @Autowired + private StudentService studentService; + + //获取学生列表 + @GetMapping("/list") + public Map getStudentList( + @RequestParam Integer page, // 页码(从1开始) + @RequestParam Integer size, // 每页条数 + @RequestParam Integer grade, // 年级(1-3) + @RequestParam(required = false) Long classId, // 班级ID + @RequestParam(required = false) String name + ){ + IPage studentPage = studentService.selectPageStudent(page,size,grade,classId,name); + Map result = new HashMap<>(); + result.put("list",studentPage.getRecords()); + result.put("total",studentPage.getTotal()); + + //测试 + System.out.println("后端返回的学生数据:" + result); + + return result; + } + + //新增学生 + @PostMapping("/add") + public boolean addStudent(@RequestBody Student student){ + return studentService.save(student); + } + + //编辑学生 + @PutMapping("/edit") + public boolean editStudent(@RequestBody Student student){ + return studentService.updateById(student); + } + + //删除学生 + @DeleteMapping("/delete/{id}") + public boolean deleteStudent(@PathVariable String id){ + if(id.contains(",")){ + String[] ids = id.split(","); + return studentService.removeByIds(Arrays.asList(ids)); + }else { + return studentService.removeById(id); + } + } +} diff --git a/src/main/java/cn/zyp/stusystem/entity/GradeClass.java b/src/main/java/cn/zyp/stusystem/entity/GradeClass.java new file mode 100644 index 0000000..f7dbe7f --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/entity/GradeClass.java @@ -0,0 +1,27 @@ +package cn.zyp.stusystem.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@TableName("class") +@AllArgsConstructor +@NoArgsConstructor +public class GradeClass { + @TableId(type = IdType.AUTO) + private Long id; + + @TableField("name") + private String name; + + @TableField("grade") + private Integer grade; + + @TableField("head_teacher") + private String headTeacher; +} diff --git a/src/main/java/cn/zyp/stusystem/entity/Student.java b/src/main/java/cn/zyp/stusystem/entity/Student.java new file mode 100644 index 0000000..e5493a8 --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/entity/Student.java @@ -0,0 +1,37 @@ +package cn.zyp.stusystem.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.Data; + +@Data +@TableName("student") +public class Student { + + @TableId(type = IdType.AUTO) + private Long id; + + @TableField("name") + private String name; + + @TableField("age") + private Integer age; + + @TableField("gender") + private Integer gender; + + @TableField("address") + private String address; + + @TableField("grade") + private Integer grade; + + @TableField("class_id") + private Long classId; + + @TableField(exist = false) + private String className; +} diff --git a/src/main/java/cn/zyp/stusystem/mapper/ClassMapper.java b/src/main/java/cn/zyp/stusystem/mapper/ClassMapper.java new file mode 100644 index 0000000..c6876ce --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/mapper/ClassMapper.java @@ -0,0 +1,10 @@ +package cn.zyp.stusystem.mapper; + +import cn.zyp.stusystem.entity.GradeClass; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; + + +@Mapper +public interface ClassMapper extends BaseMapper { +} diff --git a/src/main/java/cn/zyp/stusystem/mapper/StudentMapper.java b/src/main/java/cn/zyp/stusystem/mapper/StudentMapper.java new file mode 100644 index 0000000..c8fb3a5 --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/mapper/StudentMapper.java @@ -0,0 +1,9 @@ +package cn.zyp.stusystem.mapper; + +import cn.zyp.stusystem.entity.Student; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface StudentMapper extends BaseMapper { +} diff --git a/src/main/java/cn/zyp/stusystem/service/ClassService.java b/src/main/java/cn/zyp/stusystem/service/ClassService.java new file mode 100644 index 0000000..85dfb23 --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/service/ClassService.java @@ -0,0 +1,14 @@ +package cn.zyp.stusystem.service; + +import cn.zyp.stusystem.entity.GradeClass; +import com.baomidou.mybatisplus.extension.service.IService; + +import java.util.List; + +public interface ClassService extends IService { + //根据年级查询班级 + public List findClassByGradeId(Integer gradeId); + + //查询班级是否还有学生 + public boolean isClassHasStudent(Integer classId); +} diff --git a/src/main/java/cn/zyp/stusystem/service/StudentService.java b/src/main/java/cn/zyp/stusystem/service/StudentService.java new file mode 100644 index 0000000..60109e5 --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/service/StudentService.java @@ -0,0 +1,12 @@ +package cn.zyp.stusystem.service; + +import cn.zyp.stusystem.entity.Student; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.service.IService; + +public interface StudentService extends IService { + + //分页查询 + IPage selectPageStudent(Integer page,Integer size,Integer grade,Long classId,String name); + +} diff --git a/src/main/java/cn/zyp/stusystem/service/impl/ClassServiceImpl.java b/src/main/java/cn/zyp/stusystem/service/impl/ClassServiceImpl.java new file mode 100644 index 0000000..a86d065 --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/service/impl/ClassServiceImpl.java @@ -0,0 +1,43 @@ +package cn.zyp.stusystem.service.impl; + + +import cn.zyp.stusystem.entity.GradeClass; +import cn.zyp.stusystem.entity.Student; +import cn.zyp.stusystem.mapper.ClassMapper; +import cn.zyp.stusystem.mapper.StudentMapper; +import cn.zyp.stusystem.service.ClassService; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import lombok.ConfigurationKeys; +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + + +@Service +public class ClassServiceImpl extends ServiceImpl implements ClassService { + + @Autowired + private StudentMapper studentMapper; + + @Override + public List findClassByGradeId(Integer gradeId) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("grade",gradeId); + return baseMapper.selectList(queryWrapper); + } + + @Override + public boolean isClassHasStudent(Integer classId) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("class_id",classId); + + return studentMapper.selectCount(queryWrapper)>0; + } +} diff --git a/src/main/java/cn/zyp/stusystem/service/impl/StudentServiceImpl.java b/src/main/java/cn/zyp/stusystem/service/impl/StudentServiceImpl.java new file mode 100644 index 0000000..bccbfc2 --- /dev/null +++ b/src/main/java/cn/zyp/stusystem/service/impl/StudentServiceImpl.java @@ -0,0 +1,57 @@ +package cn.zyp.stusystem.service.impl; + +import cn.zyp.stusystem.entity.GradeClass; +import cn.zyp.stusystem.entity.Student; +import cn.zyp.stusystem.mapper.ClassMapper; +import cn.zyp.stusystem.mapper.StudentMapper; +import cn.zyp.stusystem.service.StudentService; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + + +@Service +public class StudentServiceImpl extends ServiceImpl implements StudentService { + + @Autowired + private ClassMapper classMapper; + + @Override + public IPage selectPageStudent(Integer page, Integer size, Integer grade, Long classId, String name) { + + //创建分页对象 + IPage studentPage = new Page<>(page,size); + + //构建查询条件 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq(grade != null, "grade", grade); + queryWrapper.eq(classId != null && classId > 0, "class_id", classId); + queryWrapper.like(name != null && !name.isEmpty(), "name", name); + studentPage = baseMapper.selectPage(studentPage, queryWrapper); + + //关联查询班级名称 + List students = studentPage.getRecords(); + if (students != null && students.size() > 0) { + List classIds = students.stream().map(Student::getClassId).collect(Collectors.toList()); + QueryWrapper classQueryWrapper = new QueryWrapper<>(); + classQueryWrapper.in("id", classIds); + List classes = classMapper.selectList(classQueryWrapper); + + //转换为Map + Map classMap = classes.stream().collect(Collectors.toMap(GradeClass::getId, GradeClass::getName)); + + students.forEach(student -> student.setClassName(classMap.get(student.getClassId()))); + + + } + return studentPage; + } +} + diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml new file mode 100644 index 0000000..eb8bbda --- /dev/null +++ b/src/main/resources/application.yaml @@ -0,0 +1,13 @@ +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/student_management?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai + username: root + password: 123456 + +# MyBatis-Plus ?? +mybatis-plus: + mapper-locations: classpath:mapper/*.xml # Mapper.xml ???? + type-aliases-package: com.example.student.entity # ?????? + configuration: + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # ??SQL????????? \ No newline at end of file diff --git a/src/test/java/cn/zyp/stusystem/stusystem/StuSystemApplicationTests.java b/src/test/java/cn/zyp/stusystem/stusystem/StuSystemApplicationTests.java new file mode 100644 index 0000000..c5ad284 --- /dev/null +++ b/src/test/java/cn/zyp/stusystem/stusystem/StuSystemApplicationTests.java @@ -0,0 +1,13 @@ +package cn.zyp.stusystem.stusystem; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class StuSystemApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/target/classes/application.yaml b/target/classes/application.yaml new file mode 100644 index 0000000..eb8bbda --- /dev/null +++ b/target/classes/application.yaml @@ -0,0 +1,13 @@ +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/student_management?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai + username: root + password: 123456 + +# MyBatis-Plus ?? +mybatis-plus: + mapper-locations: classpath:mapper/*.xml # Mapper.xml ???? + type-aliases-package: com.example.student.entity # ?????? + configuration: + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # ??SQL????????? \ No newline at end of file diff --git a/target/classes/cn/zyp/stusystem/StuSystemApplication.class b/target/classes/cn/zyp/stusystem/StuSystemApplication.class new file mode 100644 index 0000000..2ca59af Binary files /dev/null and b/target/classes/cn/zyp/stusystem/StuSystemApplication.class differ diff --git a/target/classes/cn/zyp/stusystem/config/MyBatisPlusConfig.class b/target/classes/cn/zyp/stusystem/config/MyBatisPlusConfig.class new file mode 100644 index 0000000..ba93903 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/config/MyBatisPlusConfig.class differ diff --git a/target/classes/cn/zyp/stusystem/controller/ClassController.class b/target/classes/cn/zyp/stusystem/controller/ClassController.class new file mode 100644 index 0000000..8758c5e Binary files /dev/null and b/target/classes/cn/zyp/stusystem/controller/ClassController.class differ diff --git a/target/classes/cn/zyp/stusystem/controller/StudentController.class b/target/classes/cn/zyp/stusystem/controller/StudentController.class new file mode 100644 index 0000000..483135e Binary files /dev/null and b/target/classes/cn/zyp/stusystem/controller/StudentController.class differ diff --git a/target/classes/cn/zyp/stusystem/entity/GradeClass.class b/target/classes/cn/zyp/stusystem/entity/GradeClass.class new file mode 100644 index 0000000..9136d3d Binary files /dev/null and b/target/classes/cn/zyp/stusystem/entity/GradeClass.class differ diff --git a/target/classes/cn/zyp/stusystem/entity/Student.class b/target/classes/cn/zyp/stusystem/entity/Student.class new file mode 100644 index 0000000..af12449 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/entity/Student.class differ diff --git a/target/classes/cn/zyp/stusystem/mapper/ClassMapper.class b/target/classes/cn/zyp/stusystem/mapper/ClassMapper.class new file mode 100644 index 0000000..31a5e6d Binary files /dev/null and b/target/classes/cn/zyp/stusystem/mapper/ClassMapper.class differ diff --git a/target/classes/cn/zyp/stusystem/mapper/StudentMapper.class b/target/classes/cn/zyp/stusystem/mapper/StudentMapper.class new file mode 100644 index 0000000..8db8da8 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/mapper/StudentMapper.class differ diff --git a/target/classes/cn/zyp/stusystem/service/ClassService.class b/target/classes/cn/zyp/stusystem/service/ClassService.class new file mode 100644 index 0000000..62d2745 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/service/ClassService.class differ diff --git a/target/classes/cn/zyp/stusystem/service/StudentService.class b/target/classes/cn/zyp/stusystem/service/StudentService.class new file mode 100644 index 0000000..47f7a71 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/service/StudentService.class differ diff --git a/target/classes/cn/zyp/stusystem/service/impl/ClassServiceImpl.class b/target/classes/cn/zyp/stusystem/service/impl/ClassServiceImpl.class new file mode 100644 index 0000000..9f1f0a8 Binary files /dev/null and b/target/classes/cn/zyp/stusystem/service/impl/ClassServiceImpl.class differ diff --git a/target/classes/cn/zyp/stusystem/service/impl/StudentServiceImpl.class b/target/classes/cn/zyp/stusystem/service/impl/StudentServiceImpl.class new file mode 100644 index 0000000..ed6dd7b Binary files /dev/null and b/target/classes/cn/zyp/stusystem/service/impl/StudentServiceImpl.class differ