@ -0,0 +1,54 @@
|
||||
package com.example.controller;
|
||||
|
||||
import com.example.common.Result;
|
||||
import com.example.entity.Grade;
|
||||
import com.example.service.GradeService;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 成绩接口
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/grade")
|
||||
public class GradeController {
|
||||
|
||||
@Resource
|
||||
GradeService gradeService;
|
||||
|
||||
|
||||
@PostMapping("/add")
|
||||
public Result add(@RequestBody Grade grade) {
|
||||
gradeService.add(grade);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
public Result update(@RequestBody Grade grade) {
|
||||
gradeService.update(grade);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除成绩
|
||||
*/
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public Result delete(@PathVariable Integer id) {
|
||||
gradeService.deleteById(id);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页条件查询课程
|
||||
*/
|
||||
@GetMapping("/selectPage")
|
||||
public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
|
||||
@RequestParam(defaultValue = "5") Integer pageSize,
|
||||
Grade grade) { // ?name=xx&no=xx
|
||||
PageInfo<Grade> pageInfo = gradeService.selectPage(pageNum, pageSize, grade);
|
||||
return Result.success(pageInfo);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,77 @@
|
||||
package com.example.entity;
|
||||
|
||||
public class Grade {
|
||||
|
||||
private Integer id;
|
||||
private Integer courseId;
|
||||
private Integer studentId;
|
||||
private Double score;
|
||||
private String comment;
|
||||
private String feedback;
|
||||
private String studentName;
|
||||
private String courseName;
|
||||
|
||||
public String getStudentName() {
|
||||
return studentName;
|
||||
}
|
||||
|
||||
public void setStudentName(String studentName) {
|
||||
this.studentName = studentName;
|
||||
}
|
||||
|
||||
public String getCourseName() {
|
||||
return courseName;
|
||||
}
|
||||
|
||||
public void setCourseName(String courseName) {
|
||||
this.courseName = courseName;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getCourseId() {
|
||||
return courseId;
|
||||
}
|
||||
|
||||
public void setCourseId(Integer courseId) {
|
||||
this.courseId = courseId;
|
||||
}
|
||||
|
||||
public Integer getStudentId() {
|
||||
return studentId;
|
||||
}
|
||||
|
||||
public void setStudentId(Integer studentId) {
|
||||
this.studentId = studentId;
|
||||
}
|
||||
|
||||
public Double getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void setScore(Double score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public String getComment() {
|
||||
return comment;
|
||||
}
|
||||
|
||||
public void setComment(String comment) {
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
public String getFeedback() {
|
||||
return feedback;
|
||||
}
|
||||
|
||||
public void setFeedback(String feedback) {
|
||||
this.feedback = feedback;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package com.example.mapper;
|
||||
|
||||
import com.example.entity.Grade;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface GradeMapper {
|
||||
|
||||
@Insert("insert into grade (course_id, student_id, score, comment, feedback) " +
|
||||
"values (#{courseId}, #{studentId}, #{score}, #{comment}, #{feedback})")
|
||||
void insert(Grade grade);
|
||||
|
||||
List<Grade> selectAll(Grade grade); // 关联查询
|
||||
|
||||
@Update("update grade set score = #{score}, comment = #{comment}, feedback = #{ feedback } where id = #{id}")
|
||||
void update(Grade grade);
|
||||
|
||||
@Select("select * from grade where student_id = #{studentId} and course_id = #{courseId}")
|
||||
Grade selectByCondition(Grade grade);
|
||||
|
||||
@Delete("delete from grade where id = #{id}")
|
||||
void deleteById(Integer id);
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
package com.example.service;
|
||||
|
||||
import com.example.entity.Grade;
|
||||
import com.example.exception.CustomException;
|
||||
import com.example.mapper.GradeMapper;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class GradeService {
|
||||
|
||||
@Resource
|
||||
GradeMapper gradeMapper;
|
||||
|
||||
public void add(Grade grade) {
|
||||
Grade dbGrade = gradeMapper.selectByCondition(grade);
|
||||
if (dbGrade != null) { // 打过分了
|
||||
throw new CustomException("您已打过分了");
|
||||
}
|
||||
gradeMapper.insert(grade);
|
||||
}
|
||||
|
||||
public PageInfo<Grade> selectPage(Integer pageNum, Integer pageSize, Grade grade) {
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
List<Grade> list = gradeMapper.selectAll(grade);
|
||||
return PageInfo.of(list);
|
||||
}
|
||||
|
||||
public void update(Grade grade) {
|
||||
gradeMapper.update(grade);
|
||||
}
|
||||
|
||||
public void deleteById(Integer id) {
|
||||
gradeMapper.deleteById(id);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.example.mapper.GradeMapper">
|
||||
|
||||
<select id="selectAll" resultType="com.example.entity.Grade">
|
||||
select grade.*, student.name as studentName, course.name as courseName
|
||||
from grade
|
||||
left join student
|
||||
on grade.student_id = student.id
|
||||
left join course
|
||||
on grade.course_id = course.id
|
||||
<where>
|
||||
<if test="studentName != null">and student.name like concat('%', #{studentName}, '%')</if>
|
||||
<if test="courseName != null">and course.name like concat('%', #{courseName}, '%')</if>
|
||||
<if test="studentId != null">and grade.student_id = #{studentId}</if>
|
||||
</where>
|
||||
order by id desc
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
Loading…
Reference in new issue