课程删除、增加基本完成

Signed-off-by: Chenx221 <chenx221@yandex.com>
This commit is contained in:
Chenx221 2023-06-11 11:20:35 +08:00
parent d4e571936e
commit cc471c1478
11 changed files with 101 additions and 43 deletions

View File

@ -35,4 +35,19 @@
</trim>
WHERE CourseID = #{courseID}
</update>
<select id="getCourseById" parameterType="int" resultType="cyou.chenx221.pojo.Course">
SELECT *
FROM course
WHERE CourseID = #{id}
AND removed = 0
</select>
<update id="deleteCourse" parameterType="int">
UPDATE course
SET removed = 1
WHERE CourseID = #{id}
</update>
<insert id="insertCourse" parameterType="cyou.chenx221.pojo.Course">
INSERT INTO course (CourseName, Description)
VALUES (#{courseName}, #{description})
</insert>
</mapper>

View File

@ -10,7 +10,6 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.sql.Date;
import java.util.List;
@Controller
@ -42,43 +41,39 @@ public class CourseController {
return "courseopv2";
}
// @PostMapping(value = "/delinfo", produces = "application/x-www-form-urlencoded;charset=UTF-8")
// public String UpdateModInfo(@RequestParam(value = "id") int id,
// Model model) {
// Student temp = studentService.getStudentById(id);
// if (id <= 0 || temp == null) {
// model.addAttribute("errorMessage", "学生信息删除失败,可能是ID非法或者学生已删除");
// } else {
//// Student student = new Student(id);
// studentService.deleteStudent(id);
// model.addAttribute("successMessage", "学生信息删除成功");
// }
// return "redirect:/student/infomodv2";
// }
//
// @PostMapping(value = "/addinfo", produces = "application/x-www-form-urlencoded;charset=UTF-8")
// public String InsertStudentInfo(@RequestParam(value = "name") String name,
// @RequestParam(value = "birthday") Date birthday,
// @RequestParam(value = "sex") String sex,
// @RequestParam(value = "phone") String phone,
// @RequestParam(value = "classes") String classes,
// Model model) {
// Student student;
// String classesPattern = "[1-5]班";// 使用正则表达式验证班级格式
//
// //格式合法性检查
// if (!sex.equals("") && !sex.equals("")) {
// model.addAttribute("errorMessage", "性别格式不正确,仅允许'男'或'女'");
// } else if (!classes.matches(classesPattern)) {
// model.addAttribute("errorMessage", "班级格式不正确,格式应为?班(?为1~5之间的数字");
// } else {
// student = new Student(name, sex, birthday, phone, classes);
// studentService.insertStudent(student);
// model.addAttribute("successMessage", "学生信息添加成功");
// }
// return "redirect:/student/infomodv2";
// }
//
@PostMapping(value = "/delinfo", produces = "application/x-www-form-urlencoded;charset=UTF-8")
public String UpdateModInfo(@RequestParam(value = "id") int id,
Model model) {
Course temp = courseService.getCourseById(id);
if (id <= 0 || temp == null) {
model.addAttribute("errorMessage", "学生信息删除失败,可能是ID非法或者学生已删除");
} else {
int status_code = courseService.deleteCourse(id);
if (status_code == 1) {
model.addAttribute("successMessage", "学生信息删除成功");
} else if (status_code == 0) {
model.addAttribute("errorMessage", "学生信息删除失败,可能是ID非法或者学生已删除");
} else {
model.addAttribute("errorMessage", "学生信息删除异常");
System.out.println("DEBUG:异常删除,多条记录被删除");
}
}
return "redirect:/course/infomodv2";
}
//
@PostMapping(value = "/addinfo", produces = "application/x-www-form-urlencoded;charset=UTF-8")
public String InsertCourseInfo(@RequestParam(value = "name", defaultValue = "null", required = false) String name,
@RequestParam(value = "description", defaultValue = "null", required = false) String description,
Model model) {
Course course;
course = new Course(name, description);
courseService.insertCourse(course);
model.addAttribute("successMessage", "学生信息添加成功");
return "redirect:/course/infomodv2";
}
//
@PostMapping(value = "/modinfo", produces = "application/x-www-form-urlencoded;charset=UTF-8")
public String UpdateModInfo(@RequestParam(value = "name", defaultValue = "null", required = false) String name,
@RequestParam(value = "description", defaultValue = "null", required = false) String description,
@ -93,11 +88,11 @@ public class CourseController {
} else {
course = new Course(id, name, description);
int status_code = courseService.updateCourse(course);
if(status_code==1){
if (status_code == 1) {
model.addAttribute("successMessage", "课程信息修改成功");
}else if(status_code==0) {
} else if (status_code == 0) {
model.addAttribute("errorMessage", "课程信息修改失败可能是ID非法或者课程不存在");
}else{
} else {
model.addAttribute("errorMessage", "课程信息修改异常");
System.out.println("DEBUG:课程信息修改异常,存在多条记录被修改的情况");
}

View File

@ -12,4 +12,10 @@ public interface CourseDao {
List<Course> getQueryCourses(Course course);
int updateCourse(Course course);
Course getCourseById(int id);
int deleteCourse(int id);
void insertCourse(Course course);
}

View File

@ -30,4 +30,19 @@ public class CourseDaoImpl implements CourseDao {
public int updateCourse(Course course) {
return sqlSession.update("updateCourse", course);
}
@Override
public Course getCourseById(int id) {
return sqlSession.selectOne("getCourseById", id);
}
@Override
public int deleteCourse(int id) {
return sqlSession.delete("deleteCourse", id);
}
@Override
public void insertCourse(Course course) {
sqlSession.insert("insertCourse", course);
}
}

View File

@ -27,4 +27,16 @@ public class CourseService {
public int updateCourse(Course course) {
return courseDao.updateCourse(course);
}
public Course getCourseById(int id) {
return courseDao.getCourseById(id);
}
public int deleteCourse(int id) {
return courseDao.deleteCourse(id);
}
public void insertCourse(Course course) {
courseDao.insertCourse(course);
}
}

View File

@ -35,4 +35,19 @@
</trim>
WHERE CourseID = #{courseID}
</update>
<select id="getCourseById" parameterType="int" resultType="cyou.chenx221.pojo.Course">
SELECT *
FROM course
WHERE CourseID = #{id}
AND removed = 0
</select>
<update id="deleteCourse" parameterType="int">
UPDATE course
SET removed = 1
WHERE CourseID = #{id}
</update>
<insert id="insertCourse" parameterType="cyou.chenx221.pojo.Course">
INSERT INTO course (CourseName, Description)
VALUES (#{courseName}, #{description})
</insert>
</mapper>

View File

@ -298,7 +298,7 @@
<div class="row mb-2">
<div class="col-12">
<div class="form-outline">
<input type="text" id="typeText14" class="form-control"
<input type="text" id="typeText14" class="form-control" required
name="name"/>
<label class="form-label" for="typeText14">课程名 (必填)</label>
</div>
@ -307,7 +307,7 @@
<div class="row mb-2">
<div class="col-12">
<div class="form-outline">
<input type="text" id="typeText15" class="form-control"
<input type="text" id="typeText15" class="form-control" required
name="description"/>
<label class="form-label" for="typeText15">课程描述 (必填)</label>
</div>