选课功能
创建选课信息表
SQL CREATE TABLE `choice` ( `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键ID', `name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '课程名称', `teacher_id` int(10) DEFAULT NULL COMMENT '授课教师', `student_id` int(10) DEFAULT NULL COMMENT '选课学生', `course_id` int(10) DEFAULT NULL COMMENT '课程ID', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='选课信息表';
|
Entity
TypeScript package com.example.entity;
import lombok.Data;
@Data public class Choice {
private Integer id; //课程名称 private String name; //教师id private Integer teacherId; //学生id private Integer studentId; //课程id private Integer courseId; //教师名称 private String teacherName; //学生名称 private String studentName;
}
|
修改Course
学生在进行选课操作时,前端封装了学生的ID,如图所示:

所以,Course新增学生ID用以接收前端数据
Java //学生id private Integer studentId;
|
ChoiceController
SQL /** * 新增 */ @PostMapping("/add") public Result add(@RequestBody Course course) { choiceService.add(course); return Result.success(); }
|
ChoiceService
Java package com.example.service;
import com.example.entity.Course;
public interface ChoiceService { void add(Course course); }
|
ChoiceServiceImpl
Java package com.example.service.impl;
@Service public class ChoiceServiceImpl implements ChoiceService { @Resource private CourseMapper courseMapper; @Resource private ChoiceMapper choiceMapper; @Resource private StudentMapper studentMapper;
@Transactional @Override public void add(Course course) { //1.判断课程是否满员 if (ObjectUtil.isEmpty(course.getAlreadyNum())){ course.setAlreadyNum(0); } if (course.getNum() <= course.getAlreadyNum()){ throw new CustomException("课程已满"); } //2.判断该学生是否选过这门课程 Choice choice = choiceMapper.selectByCourseIdAndStudentId(course.getStudentId(), course.getId()); if (ObjectUtil.isNotEmpty(choice)){ throw new CustomException("该学生已选过该课程"); } //3.添加选课信息 Choice myChoice = new Choice(); myChoice.setCourseId(course.getId()); myChoice.setStudentId(course.getStudentId()); myChoice.setName(course.getName()); myChoice.setTeacherId(course.getTeacherId()); choiceMapper.insert(myChoice); //4.更新课程已选人数 course.setAlreadyNum(course.getAlreadyNum() + 1); courseMapper.updateById(course); //5.该学生学分增加 Student student = studentMapper.selectById(course.getStudentId()); student.setScore(student.getScore() + course.getScore()); studentMapper.updateById(student); }
}
|
ChoiceMapper
Java package com.example.mapper;
public interface ChoiceMapper { @Select("SELECT * FROM choice WHERE student_id=#{studentId} AND course_id=#{courseId}") Choice selectByCourseIdAndStudentId(Integer studentId, Integer courseId);
@Insert("INSERT INTO choice(name,course_id, student_id, teacher_id) VALUES(#{name},#{courseId}, #{studentId}, #{teacherId})") void insert(Choice myChoice); }
|
分页查询和条件查询
前端
Choice.vue
将Choice.vue添加到前端工程views/manager中
[Choice.vue]
在Manager.vue中添加选课信息

代码
XML <el-menu-item index="/choice"> <el-icon><Reading /></el-icon> <span>选课信息</span> </el-menu-item>
|
路由
在router/index.js中添加选课路由

代码
Java { path: 'choice', component: () => import('@/views/manager/Choice.vue')},
|
后端
ChoiceController
TypeScript /** * 分页查询 */ @GetMapping("/selectPage") public Result selectPage(Choice choice, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) { PageInfo<Choice> pageInfo = choiceService.selectPage(choice, pageNum, pageSize); return Result.success(pageInfo); }
|
ChoiceServiceImpl
TypeScript @Override public PageInfo<Choice> selectPage(Choice choice, Integer pageNum, Integer pageSize) { List<Choice> list; PageHelper.startPage(pageNum, pageSize); //学生登录 if (ObjectUtil.isNotEmpty(choice.getStudentId())){ if (ObjectUtil.isNotEmpty(choice.getName())){ list = choiceMapper.selectByNameAndStudentId(choice.getName(), choice.getStudentId()); }else{ list = choiceMapper.selectAllByStudentId(choice.getStudentId()); } }else if (ObjectUtil.isNotEmpty(choice.getTeacherId())){ //教师登录 if (ObjectUtil.isNotEmpty(choice.getName())){ list = choiceMapper.selectByNameAndTeacherId(choice.getName(), choice.getTeacherId()); }else { list = choiceMapper.selectAllByTeacherId(choice.getTeacherId()); } }else {//管理员登录 if (ObjectUtil.isNotEmpty(choice.getName())){ list = choiceMapper.selectByName(choice.getName()); }else{ list = choiceMapper.selectAll(); }
} return PageInfo.of(list); }
|
ChoiceMapper
Python @Select("SELECT c.*,t.`name` teacherName,s.`name` studentName FROM choice c " + "LEFT JOIN teacher t ON c.teacher_id=t.id\n" + "LEFT JOIN student s ON c.student_id=s.id \n" + "WHERE c.`name` LIKE CONCAT('%',#{name},'%') AND c.student_id=#{studentId}") List<Choice> selectByNameAndStudentId(String name, Integer studentId);
@Select("SELECT c.*,t.`name` teacherName,s.`name` studentName FROM choice c " + "LEFT JOIN teacher t ON c.teacher_id=t.id\n" + "LEFT JOIN student s ON c.student_id=s.id \n" + "WHERE c.student_id=#{studentId}") List<Choice> selectAllByStudentId(Integer studentId);
@Select("SELECT c.*,t.`name` teacherName,s.`name` studentName FROM choice c " + "LEFT JOIN teacher t ON c.teacher_id=t.id\n" + "LEFT JOIN student s ON c.student_id=s.id \n" + "WHERE c.`name` LIKE CONCAT('%',#{name},'%') AND c.teacher_id=#{teacherId}") List<Choice> selectByNameAndTeacherId(String name, Integer teacherId);
@Select("SELECT c.*,t.`name` teacherName,s.`name` studentName FROM choice c " + "LEFT JOIN teacher t ON c.teacher_id=t.id\n" + "LEFT JOIN student s ON c.student_id=s.id \n" + "WHERE c.teacher_id=#{teacherId}") List<Choice> selectAllByTeacherId(Integer teacherId);
@Select("SELECT c.*,t.`name` teacherName,s.`name` studentName FROM choice c " + "LEFT JOIN teacher t ON c.teacher_id=t.id\n" + "LEFT JOIN student s ON c.student_id=s.id \n" + "WHERE c.`name` LIKE CONCAT('%',#{name},'%')") List<Choice> selectByName(String name);
@Select("SELECT c.*,t.`name` teacherName,s.`name` studentName FROM choice c " + "LEFT JOIN teacher t ON c.teacher_id=t.id\n" + "LEFT JOIN student s ON c.student_id=s.id") List<Choice> selectAll();
|
取消选课
ChoiceController
SQL /** * 取消选课 */ @DeleteMapping("/deleteById/{id}") public Result deleteById(@PathVariable Integer id){ choiceService.deleteById(id); return Result.success(); }
|
ChoiceServiceImpl
Java @Transactional @Override public void deleteById(Integer id) { //1.删除选课信息 //获取选课信息 Choice choice = choiceMapper.selectById(id); choiceMapper.deleteById(id); //2.更新课程已选人数 //获取课程 Course course = courseMapper.selectById(choice.getCourseId()); course.setAlreadyNum(course.getAlreadyNum() - 1); courseMapper.updateById(course); //3.该学生学分减少 Student student = studentMapper.selectById(choice.getStudentId()); student.setScore(student.getScore() - course.getScore()); studentMapper.updateById(student); }
|
ChoiceMapper
Python @Delete("delete from choice where id=#{id}") void deleteById(Integer id);
@Select("select * from choice where id=#{id}") Choice selectById(Integer id);
|
CourseMapper
Java @Select("select * from course where id = #{id}") Course selectById(Integer courseId);
|
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
评论(0)