目前完成了所有的功能测试,准备进行优化
commit
cd1dddb798
@ -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
|
||||
@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<annotationProcessing>
|
||||
<profile name="Annotation profile for StuSystem" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<processorPath useClasspath="false">
|
||||
<entry name="$MAVEN_REPOSITORY$/org/projectlombok/lombok/1.18.42/lombok-1.18.42.jar" />
|
||||
</processorPath>
|
||||
<module name="StuSystem" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
<component name="JavacSettings">
|
||||
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
|
||||
<module name="StuSystem" options="-parameters" />
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
||||
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@ -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
|
||||
@ -0,0 +1,94 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.5.7</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>cn.zyp.stuSystem</groupId>
|
||||
<artifactId>StuSystem</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>StuSystem</name>
|
||||
<description>StuSystem</description>
|
||||
<url/>
|
||||
<licenses>
|
||||
<license/>
|
||||
</licenses>
|
||||
<developers>
|
||||
<developer/>
|
||||
</developers>
|
||||
<scm>
|
||||
<connection/>
|
||||
<developerConnection/>
|
||||
<tag/>
|
||||
<url/>
|
||||
</scm>
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>3.0.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>3.5.5</version> <!-- 版本可根据需求选择,建议使用最新稳定版 -->
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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<String, Object> getClassList(@RequestParam Integer grade,@RequestParam(required = false) String name){
|
||||
List<GradeClass> gradeClasses = classService.findClassByGradeId(grade);
|
||||
System.out.println(gradeClasses);
|
||||
|
||||
//过滤班级名称
|
||||
if(name != null){
|
||||
gradeClasses= gradeClasses.stream().filter(gradeClass -> gradeClass.getName().contains(name)).toList();
|
||||
}
|
||||
Map<String,Object> 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<String,Object> checkClassHasStudent(@RequestParam Integer classId){
|
||||
Map<String,Object> result = new HashMap<>();
|
||||
result.put("data",classService.isClassHasStudent(classId));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
@ -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<GradeClass> {
|
||||
}
|
||||
@ -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<Student> {
|
||||
}
|
||||
@ -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<GradeClass> {
|
||||
//根据年级查询班级
|
||||
public List<GradeClass> findClassByGradeId(Integer gradeId);
|
||||
|
||||
//查询班级是否还有学生
|
||||
public boolean isClassHasStudent(Integer classId);
|
||||
}
|
||||
@ -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<Student> {
|
||||
|
||||
//分页查询
|
||||
IPage<Student> selectPageStudent(Integer page,Integer size,Integer grade,Long classId,String name);
|
||||
|
||||
}
|
||||
@ -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<ClassMapper, GradeClass> implements ClassService {
|
||||
|
||||
@Autowired
|
||||
private StudentMapper studentMapper;
|
||||
|
||||
@Override
|
||||
public List<GradeClass> findClassByGradeId(Integer gradeId) {
|
||||
QueryWrapper<GradeClass> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("grade",gradeId);
|
||||
return baseMapper.selectList(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClassHasStudent(Integer classId) {
|
||||
QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("class_id",classId);
|
||||
|
||||
return studentMapper.selectCount(queryWrapper)>0;
|
||||
}
|
||||
}
|
||||
@ -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<StudentMapper, Student> implements StudentService {
|
||||
|
||||
@Autowired
|
||||
private ClassMapper classMapper;
|
||||
|
||||
@Override
|
||||
public IPage<Student> selectPageStudent(Integer page, Integer size, Integer grade, Long classId, String name) {
|
||||
|
||||
//创建分页对象
|
||||
IPage<Student> studentPage = new Page<>(page,size);
|
||||
|
||||
//构建查询条件
|
||||
QueryWrapper<Student> 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<Student> students = studentPage.getRecords();
|
||||
if (students != null && students.size() > 0) {
|
||||
List<Long> classIds = students.stream().map(Student::getClassId).collect(Collectors.toList());
|
||||
QueryWrapper<GradeClass> classQueryWrapper = new QueryWrapper<>();
|
||||
classQueryWrapper.in("id", classIds);
|
||||
List<GradeClass> classes = classMapper.selectList(classQueryWrapper);
|
||||
|
||||
//转换为Map
|
||||
Map<Long, String> classMap = classes.stream().collect(Collectors.toMap(GradeClass::getId, GradeClass::getName));
|
||||
|
||||
students.forEach(student -> student.setClassName(classMap.get(student.getClassId())));
|
||||
|
||||
|
||||
}
|
||||
return studentPage;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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?????????
|
||||
@ -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() {
|
||||
}
|
||||
|
||||
}
|
||||
@ -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?????????
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue