教师信息添加功能完成

Signed-off-by: Chenx221 <chenx221@yandex.com>
This commit is contained in:
Chenx221 2023-06-17 11:12:35 +08:00
parent 278b08e1e9
commit ec49018b9b
14 changed files with 86 additions and 11 deletions

View File

@ -83,4 +83,9 @@
WHERE id = #{id};
</update>
<insert id="addTeacher" parameterType="cyou.chenx221.pojo.Teacher">
INSERT INTO teacher (name, sex, birthday, course_id, classes)
VALUES (#{name}, #{sex}, #{birthday}, #{course.CourseID}, #{classes})
</insert>
</mapper>

View File

@ -58,7 +58,6 @@
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown2">
<li><a class="dropdown-item" href="/teacher/manage">教师信息管理</a></li>
<li><a class="dropdown-item" href="#">教师课程管理</a></li>
</ul>
</li>
<li class="nav-item dropdown">

View File

@ -411,7 +411,7 @@
<div class="tab-pane fade" id="ex1-tabs-4" role="tabpanel" aria-labelledby="ex1-tab-4">
<!-- <h5 class="card-header">信息添加</h5> -->
<div class="card-body" style="min-width: 175px">
<form action="add" method="post">
<form action="add" method="post" id="addform">
<div class="row mb-2">
<div class="col-12">
<div class="form-outline">
@ -618,6 +618,21 @@
});
}
});
$('#addform').on('submit', function (event) {
event.preventDefault();
$.ajax({
url: 'add',
method: 'POST',
data: $('#addform').serialize(),
success: function (response) {
success(response)
},
error: function (xhr, status, error) {
error(xhr,status,error)
}
});
});
});
function success(response){

View File

@ -107,7 +107,7 @@ public class TeacherController {
}
@ResponseBody
@PostMapping(value = "/mod",produces = "text/plain;charset=UTF-8")
@PostMapping(value = "/mod", produces = "text/plain;charset=UTF-8")
public ResponseEntity<String> postEdit(@RequestParam(value = "id") int id,
@RequestParam(value = "name", defaultValue = "null", required = false) String name,
@RequestParam(value = "birthday", defaultValue = "null", required = false) String birthday_str,
@ -130,17 +130,17 @@ public class TeacherController {
return ResponseEntity.badRequest().body("班级格式非法");
}
}
if(name.equals("null")){
name=null;
if (name.equals("null")) {
name = null;
}
if(classes.equals("null")){
classes=null;
if (classes.equals("null")) {
classes = null;
}
//
//这里不考虑一个班一个科目不允许多名教师少了个班级表+数据表外键搞得不好所以这里偷懒了...
Teacher teacher = new Teacher(id,name,birthday,new Course(course_id), classes);
Teacher teacher = new Teacher(id, name, birthday, new Course(course_id), classes);
int status_code = teacherService.updateTeacher(teacher);
if (status_code == 0) {
return ResponseEntity.badRequest().body("修改失败");
@ -150,7 +150,7 @@ public class TeacherController {
}
@ResponseBody
@PostMapping(value = "/del",produces = "text/plain;charset=UTF-8")
@PostMapping(value = "/del", produces = "text/plain;charset=UTF-8")
public ResponseEntity<String> postDelete(@RequestParam(value = "id") int id) {
if (id <= 0) {
return ResponseEntity.badRequest().body("ID非法");
@ -162,4 +162,30 @@ public class TeacherController {
return ResponseEntity.ok("删除成功");
}
}
@ResponseBody
@PostMapping(value = "/add", produces = "text/plain;charset=UTF-8")
public ResponseEntity<String> postAdd(@RequestParam(value = "name") String name,
@RequestParam(value = "sex") String sex,
@RequestParam(value = "birthday") String birthday_str,
@RequestParam(value = "course_id") int course_id,
@RequestParam(value = "classes") String classes) throws ParseException {
// 处理表单字段数据
String classesPattern = "[1-5]班";// 使用正则表达式验证班级格式
if (!classes.matches(classesPattern)) {
return ResponseEntity.badRequest().body("班级格式非法");
}
if (course_id <= 0) {
return ResponseEntity.badRequest().body("科目ID非法");
}
Date birthday = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(birthday_str).getTime());
Teacher teacher = new Teacher(name, sex, birthday, new Course(course_id), classes);
int status_code = teacherService.addTeacher(teacher);
if (status_code == 0) {
return ResponseEntity.badRequest().body("添加失败");
} else {
return ResponseEntity.ok("添加成功");
}
}
}

View File

@ -15,4 +15,6 @@ public interface TeacherDao {
int updateTeacher(Teacher teacher);
int deleteTeacher(int id);
int addTeacher(Teacher teacher);
}

View File

@ -37,4 +37,9 @@ public class TeacherDaoImpl implements TeacherDao {
return sqlSession.update("deleteTeacher", id);
}
@Override
public int addTeacher(Teacher teacher) {
return sqlSession.insert("addTeacher", teacher);
}
}

View File

@ -30,4 +30,8 @@ public class TeacherService {
public int deleteTeacher(int id) {
return teacherDao.deleteTeacher(id);
}
public int addTeacher(Teacher teacher) {
return teacherDao.addTeacher(teacher);
}
}

View File

@ -83,4 +83,9 @@
WHERE id = #{id};
</update>
<insert id="addTeacher" parameterType="cyou.chenx221.pojo.Teacher">
INSERT INTO teacher (name, sex, birthday, course_id, classes)
VALUES (#{name}, #{sex}, #{birthday}, #{course.CourseID}, #{classes})
</insert>
</mapper>

View File

@ -58,7 +58,6 @@
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown2">
<li><a class="dropdown-item" href="/teacher/manage">教师信息管理</a></li>
<li><a class="dropdown-item" href="#">教师课程管理</a></li>
</ul>
</li>
<li class="nav-item dropdown">

View File

@ -411,7 +411,7 @@
<div class="tab-pane fade" id="ex1-tabs-4" role="tabpanel" aria-labelledby="ex1-tab-4">
<!-- <h5 class="card-header">信息添加</h5> -->
<div class="card-body" style="min-width: 175px">
<form action="add" method="post">
<form action="add" method="post" id="addform">
<div class="row mb-2">
<div class="col-12">
<div class="form-outline">
@ -618,6 +618,21 @@
});
}
});
$('#addform').on('submit', function (event) {
event.preventDefault();
$.ajax({
url: 'add',
method: 'POST',
data: $('#addform').serialize(),
success: function (response) {
success(response)
},
error: function (xhr, status, error) {
error(xhr,status,error)
}
});
});
});
function success(response){