This commit is contained in:
hejiafeng2003 2024-10-29 12:24:22 +08:00
parent feb3d8bace
commit bd09ffba19
4 changed files with 107 additions and 22 deletions

View File

@ -26,4 +26,6 @@ public interface ScoreMapper {
void insertScore(Score score); void insertScore(Score score);
void updateScore(Score score); void updateScore(Score score);
void deleteScoreById(Integer scoreId); void deleteScoreById(Integer scoreId);
// 根据课程ID获取所有相关的成绩ID
List<Score> getScoresWithNamesByStudentId(String number);
} }

View File

@ -4,6 +4,6 @@ import lombok.Data;
@Data @Data
public class Course extends BaseEntity{ public class Course extends BaseEntity{
private Integer id; private String id;
private String name; private String name;
} }

View File

@ -55,6 +55,23 @@
JOIN course c ON s.course_id = c.id JOIN course c ON s.course_id = c.id
WHERE s.student_id = #{studentId} WHERE s.student_id = #{studentId}
</select> </select>
<select id="getScoresWithNamesByStudentId" resultType="edu.hjf.pojo.Score">
SELECT
s.id AS score_id,
s.student_id,
s.course_id,
s.score,
st.name AS student_name,
c.name AS course_name
FROM
score s
JOIN
student st ON s.student_id = st.id
JOIN
course c ON s.course_id = c.id
WHERE
s.student_id = #{number}
</select>
<insert id="insertScore"> <insert id="insertScore">
INSERT INTO score (student_id, course_id, score, create_time) VALUES (#{studentId}, #{courseId}, #{score}, #{createTime}) INSERT INTO score (student_id, course_id, score, create_time) VALUES (#{studentId}, #{courseId}, #{score}, #{createTime})

View File

@ -16,7 +16,18 @@ import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; 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 @SpringBootTest
class ApplicationTests { class ApplicationTests {
@ -28,6 +39,9 @@ class ApplicationTests {
private StudentScoreMapper studentScoreMapper; private StudentScoreMapper studentScoreMapper;
@Autowired @Autowired
private AccountMapper accountMapper; private AccountMapper accountMapper;
@Autowired
private CourseMapper courseMapper;
@Test @Test
public void testGetAll(){ public void testGetAll(){
@ -146,30 +160,82 @@ class ApplicationTests {
System.out.println("课程:" + score.getCourse().getName() + ",成绩:" + score.getScore()); System.out.println("课程:" + score.getCourse().getName() + ",成绩:" + score.getScore());
} }
} }
/*@Test
public void testGradeUpdate(){ // 1. 查询所有课程名
@Test
public void testGetAllCourses() {
List<Course> 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<Score> 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 score = new Score();
score.setStudentID("1214 2"); score.setStudentId("12138");
score.setCourseID("101"); score.setCourseId("103");
score.setGrade(90); 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()); score.setUpdateTime(LocalDateTime.now());
gradeMapper.update(score); score.setUpdateBy(1);
System.out.println(score); scoreMapper.updateScore(score);
} }
// 9. 删除某个人某条成绩
@Test @Test
public void testGradeDelete(){ public void testDeleteScoreForStudent() {
gradeMapper.deleteByNo(24); scoreMapper.deleteScoreById(1);
} }
@Test
public void testGetByNo(){
Score score = gradeMapper.getByNo(1);
System.out.println(score);
}
*/
} }