From bd09ffba194f3c028425c239eeca4c88ebb97893 Mon Sep 17 00:00:00 2001 From: hejiafeng2003 <14455358+hejiafeng2003@user.noreply.gitee.com> Date: Tue, 29 Oct 2024 12:24:22 +0800 Subject: [PATCH] 22 --- src/main/java/edu/hjf/mapper/ScoreMapper.java | 2 + src/main/java/edu/hjf/pojo/Course.java | 2 +- src/main/resources/mapper/ScoreMapper.xml | 17 +++ src/test/java/edu/hjf/ApplicationTests.java | 108 ++++++++++++++---- 4 files changed, 107 insertions(+), 22 deletions(-) diff --git a/src/main/java/edu/hjf/mapper/ScoreMapper.java b/src/main/java/edu/hjf/mapper/ScoreMapper.java index b348f2d..b2af195 100644 --- a/src/main/java/edu/hjf/mapper/ScoreMapper.java +++ b/src/main/java/edu/hjf/mapper/ScoreMapper.java @@ -26,4 +26,6 @@ public interface ScoreMapper { void insertScore(Score score); void updateScore(Score score); void deleteScoreById(Integer scoreId); + // 根据课程ID获取所有相关的成绩ID + List getScoresWithNamesByStudentId(String number); } diff --git a/src/main/java/edu/hjf/pojo/Course.java b/src/main/java/edu/hjf/pojo/Course.java index aecf3ee..91983e0 100644 --- a/src/main/java/edu/hjf/pojo/Course.java +++ b/src/main/java/edu/hjf/pojo/Course.java @@ -4,6 +4,6 @@ import lombok.Data; @Data public class Course extends BaseEntity{ - private Integer id; + private String id; private String name; } diff --git a/src/main/resources/mapper/ScoreMapper.xml b/src/main/resources/mapper/ScoreMapper.xml index ed9edc3..3e7b00d 100644 --- a/src/main/resources/mapper/ScoreMapper.xml +++ b/src/main/resources/mapper/ScoreMapper.xml @@ -55,6 +55,23 @@ JOIN course c ON s.course_id = c.id WHERE s.student_id = #{studentId} + INSERT INTO score (student_id, course_id, score, create_time) VALUES (#{studentId}, #{courseId}, #{score}, #{createTime}) diff --git a/src/test/java/edu/hjf/ApplicationTests.java b/src/test/java/edu/hjf/ApplicationTests.java index 017f1b8..a348eb6 100644 --- a/src/test/java/edu/hjf/ApplicationTests.java +++ b/src/test/java/edu/hjf/ApplicationTests.java @@ -16,7 +16,18 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Arrays; import java.util.List; - +import edu.hjf.mapper.CourseMapper; +import edu.hjf.pojo.Course; +import edu.hjf.pojo.Score; +import edu.hjf.mapper.CourseMapper; +import edu.hjf.mapper.ScoreMapper; +import edu.hjf.pojo.Course; +import edu.hjf.pojo.Score; +import edu.hjf.pojo.Student; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import java.time.LocalDateTime; @SpringBootTest class ApplicationTests { @@ -28,7 +39,10 @@ class ApplicationTests { private StudentScoreMapper studentScoreMapper; @Autowired private AccountMapper accountMapper; - + @Autowired + private CourseMapper courseMapper; + + @Test public void testGetAll(){ // 查询所有学生信息 @@ -146,30 +160,82 @@ class ApplicationTests { System.out.println("课程:" + score.getCourse().getName() + ",成绩:" + score.getScore()); } } - /*@Test - public void testGradeUpdate(){ + + // 1. 查询所有课程名 + @Test + public void testGetAllCourses() { + List courses = courseMapper.getAllCourses(); + courses.forEach(course -> System.out.println(course.getName())); + } + + // 2. 增加某个课程 + @Test + public void testInsertCourse() { + Course course = new Course(); + course.setId("107"); + course.setName("医学信息工程"); + course.setCreateBy(1); + course.setCreateTime(LocalDateTime.now()); + courseMapper.insertCourse(course); + } + + // 3. 更新某个课程 + @Test + public void testUpdateCourse() { + Course course = new Course(); + course.setId("103"); + course.setName("线性代数"); + course.setUpdateBy(1); + course.setUpdateTime(LocalDateTime.now()); + courseMapper.updateCourse(course); + } + + // 4. 删除某个课程 + @Test + public void testDeleteCourse() { + courseMapper.deleteCourseById("107"); + } + + @Test + public void testGetScoresWithNamesByStudentId() { + List scores = scoreMapper.getScoresWithNamesByStudentId("12138"); + for (Score score : scores) { + if (score.getStudent() != null) { + System.out.println("学生姓名:" + score.getStudent().getName() + ",课程名:" + score.getCourse().getName() + ",成绩:" + score.getScore()); + } else { + System.out.println("学生记录为空"); + } + } + } + + + // 7. 增加某个人某条成绩 + @Test + public void testAddScoreForStudent() { Score score = new Score(); - score.setStudentID("1214 2"); - score.setCourseID("101"); - score.setGrade(90); + score.setStudentId("12138"); + score.setCourseId("103"); + score.setScore(95); + score.setCreateTime(LocalDateTime.now()); + score.setCreateBy(1); + scoreMapper.insertScore(score); + } + + // 8. 更新某个人某条成绩 + @Test + public void testUpdateScoreForStudent() { + Score score = new Score(); + score.setId(1); + score.setScore(98); score.setUpdateTime(LocalDateTime.now()); - gradeMapper.update(score); - System.out.println(score); + score.setUpdateBy(1); + scoreMapper.updateScore(score); } - + // 9. 删除某个人某条成绩 @Test - public void testGradeDelete(){ - gradeMapper.deleteByNo(24); + public void testDeleteScoreForStudent() { + scoreMapper.deleteScoreById(1); } - - @Test - public void testGetByNo(){ - Score score = gradeMapper.getByNo(1); - System.out.println(score); - } - - */ - }