Teacher Info Mod

Signed-off-by: Chenx221 <chenx221@yandex.com>
This commit is contained in:
Chenx221 2023-06-16 21:12:59 +08:00
parent eb98f1cb85
commit 96f0fcb2cb
16 changed files with 372 additions and 211 deletions

View File

@ -1,10 +1,10 @@
log4j.rootLogger=INFO, stdout
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.logger.your.package.name=INFO, sql
log4j.logger.your.package.name=WARN, sql
log4j.appender.sql=org.apache.log4j.ConsoleAppender
log4j.appender.sql.layout=org.apache.log4j.PatternLayout

View File

@ -56,4 +56,27 @@
</if>
</trim>
</select>
<update id="updateTeacher" parameterType="cyou.chenx221.pojo.Teacher">
UPDATE teacher
<set>
<trim suffixOverrides=",">
<if test="name != null">
name = #{name},
</if>
<if test="birthday != null">
birthday = #{birthday},
</if>
<if test="course.CourseID != -1">
course_id = #{course.CourseID},
</if>
<if test="classes != null">
classes = #{classes}
</if>
</trim>
</set>
WHERE id = #{id};
</update>
</mapper>

View File

@ -173,10 +173,12 @@
<td>${teacher.classes}</td>
<td class="text-center">
<div class="btn-group" role="group" aria-label="Basic example">
<button type="button" class="btn btn-danger">
<button type="button" class="btn btn-info" data-mdb-toggle="tooltip"
data-mdb-placement="top" title="编辑">
<i class="fas fa-pencil"></i>
</button>
<button type="button" class="btn btn-primary">
<button type="button" class="btn btn-danger" data-mdb-toggle="tooltip"
data-mdb-placement="top" title="删除">
<i class="fas fa-trash"></i>
</button>
</div>
@ -303,7 +305,7 @@
</div>
<div class="tab-pane fade" id="ex1-tabs-2" role="tabpanel" aria-labelledby="ex1-tab-2">
<div class="card-body" style="min-width: 175px">
<form action="mod" method="post">
<form id="modform" action="mod" method="post">
<div class="row mb-2">
<div class="col-12">
<div class="form-outline">
@ -351,28 +353,28 @@
</div>
</div>
</div>
<div class="row mb-2">
<div class="col-12">
<div class="form-outline">
<input type="text" id="typeText14" class="form-control"
name="course_name"/>
<label class="form-label" for="typeText14">课程名(支持部分)</label>
</div>
</div>
</div>
<%-- <div class="row mb-2">--%>
<%-- <div class="col-12">--%>
<%-- <div class="form-outline">--%>
<%-- <input type="text" id="typeText14" class="form-control"--%>
<%-- name="course_name"/>--%>
<%-- <label class="form-label" for="typeText14">课程名(支持部分)</label>--%>
<%-- </div>--%>
<%-- </div>--%>
<%-- </div>--%>
<div class="row mb-2">
<div class="col-12">
<div class="form-outline">
<input type="text" id="typeText15" class="form-control"
name="classes"/>
<label class="form-label"
for="typeText15">授课班级(一班~五班)</label>
for="typeText15">授课班级(1~5班)</label>
</div>
</div>
</div>
<div class="row mb-2 mt-4">
<div class="col-lg-6 text-center">
<button type="submit" class="btn btn-primary">修改
<button type="submit" class="btn btn-primary" id="modbtn">修改
</button>
</div>
<div class="col-lg-6 text-center">
@ -452,7 +454,7 @@
<div class="form-outline">
<input type="text" id="typeText23" class="form-control" required
name="classes"/>
<label class="form-label" for="typeText23">授课班级</label>
<label class="form-label" for="typeText23">授课班级(1~5班)</label>
</div>
</div>
</div>
@ -557,96 +559,118 @@
</c:if>
});
function resetPWD(id) {
$('#typeText5').val(id);
$('#ex1-tab-2').tab('show');
}
//实现按钮的点击事件(禁用/启用用户)
$(document).ready(function () {
$('.btn-danger').on('click', function () {
$('.btn-danger').on('click', function () { //移除用户
var id = $(this).closest('tr').find('td:first').text();
userdisabled(id);
// userdisabled(id);
console.log(id) // debug
});
$('.btn-info').on('click', function () { //编辑用户
var id = $(this).closest('tr').find('td:first').text();
$('#ex1-tab-2').tab('show');
$('#typeText9').val(id).focus();
// console.log(id) // debug
});
});
$(document).ready(function () {
$('.btn-success').on('click', function () {
var id = $(this).closest('tr').find('td:first').text();
userenabled(id);
// console.log(id) // debug
$('#modform').on('submit', function (event) {
event.preventDefault();
$.ajax({
url: 'mod', // 替换为您的请求 URL
method: 'POST', // 根据您的需求选择请求方法 (POST, GET, etc.)
data: $('#modform').serialize(),
success: function (response) {
$('#de_message').text(response);
$('#de_button').on('click', function () {
reloadka("reload");
});
$('#exampleModal3').modal('show').on('hidden.bs.modal', function () { //针对点击modal外的情况的处理
reloadka("reload");
});
},
error: function (xhr, status, error) {
if (xhr.status === 400) {
$('#de_message').text(xhr.responseText);
$('#de_button').on('click', function () {
reloadka("close");
});
$('#exampleModal3').modal('show');
} else {
console.log('请求失败:', error);
alert('请求请求失败,请重试');
}
}
});
});
});
//使用ajax在后台发送post禁用/启用用户请求
function userdisabled(id) {
//btn-danger
$.ajax({
url: 'userdisabled',
type: 'post',
data: {
id: id
},
success: function (data) {
if (data.status === 'success') {
$('#de_message').text(data.message);
$('#de_button').on('click', function () {
reloadka("reload");
});
$('#exampleModal3').modal('show');
$('#exampleModal3').on('hidden.bs.modal', function () { //针对点击modal外的情况的处理
reloadka("reload");
});
} else {
$('#de_message').text(data.message);
$('#de_button').on('click', function () {
reloadka("close");
});
$('#exampleModal3').modal('show');
}
},
error: function (xhr, status, error) {
// 处理请求失败的逻辑
console.log('请求失败:', error);
alert('请求请求失败,请重试');
}
});
}
function userenabled(id) {
$.ajax({
url: 'userenabled',
type: 'post',
data: {
id: id
},
success: function (data) {
if (data.status === 'success') {
$('#de_message').text(data.message);
$('#de_button').on('click', function () {
reloadka("reload");
});
$('#exampleModal3').modal('show');
$('#exampleModal3').on('hidden.bs.modal', function () {
reloadka("reload");
});
} else {
$('#de_message').text(data.message);
$('#de_button').on('click', function () {
reloadka("close");
});
$('#exampleModal3').modal('show');
}
},
error: function (xhr, status, error) {
// 处理请求失败的逻辑
console.log('请求失败:', error);
alert('请求请求失败,请重试');
}
});
}
// //使用ajax在后台发送post禁用/启用用户请求
// function userdisabled(id) {
// //btn-danger
// $.ajax({
// url: 'userdisabled',
// type: 'post',
// data: {
// id: id
// },
// success: function (data) {
// if (data.status === 'success') {
// $('#de_message').text(data.message);
// $('#de_button').on('click', function () {
// reloadka("reload");
// });
// $('#exampleModal3').modal('show');
// $('#exampleModal3').on('hidden.bs.modal', function () { //针对点击modal外的情况的处理
// reloadka("reload");
// });
// } else {
// $('#de_message').text(data.message);
// $('#de_button').on('click', function () {
// reloadka("close");
// });
// $('#exampleModal3').modal('show');
// }
// },
// error: function (xhr, status, error) {
// // 处理请求失败的逻辑
// console.log('请求失败:', error);
// alert('请求请求失败,请重试');
// }
// });
// }
//
// function userenabled(id) {
// $.ajax({
// url: 'userenabled',
// type: 'post',
// data: {
// id: id
// },
// success: function (data) {
// if (data.status === 'success') {
// $('#de_message').text(data.message);
// $('#de_button').on('click', function () {
// reloadka("reload");
// });
// $('#exampleModal3').modal('show');
// $('#exampleModal3').on('hidden.bs.modal', function () {
// reloadka("reload");
// });
// } else {
// $('#de_message').text(data.message);
// $('#de_button').on('click', function () {
// reloadka("close");
// });
// $('#exampleModal3').modal('show');
// }
// },
// error: function (xhr, status, error) {
// // 处理请求失败的逻辑
// console.log('请求失败:', error);
// alert('请求请求失败,请重试');
// }
// });
// }
//
//处理成功/失败后的刷新页面/关闭模态框操作
function reloadka(action) {
if (action === 'reload') {

View File

@ -11,7 +11,7 @@
Target Server Version : 80033 (8.0.33)
File Encoding : 65001
Date: 16/06/2023 13:52:21
Date: 16/06/2023 21:12:31
*/
SET NAMES utf8mb4;
@ -1232,8 +1232,8 @@ CREATE TABLE `teacher` (
-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES (1, '韦秀英', '', '1989-01-01', 8, '1班', 0);
INSERT INTO `teacher` VALUES (2, '蔡岚', '', '1997-08-14', 1, '5班', 0);
INSERT INTO `teacher` VALUES (1, '无名氏', '', '1989-01-01', 8, '1班', 0);
INSERT INTO `teacher` VALUES (2, '无名氏2', '', '1997-08-14', 1, '5班', 0);
INSERT INTO `teacher` VALUES (3, '蒋岚', '', '1998-09-28', 9, '3班', 0);
INSERT INTO `teacher` VALUES (4, '侯嘉伦', '', '1980-05-07', 13, '3班', 0);
INSERT INTO `teacher` VALUES (5, '毛子异', '', '1995-05-03', 12, '1班', 0);

View File

@ -3,17 +3,14 @@ package cyou.chenx221.controller;
import cyou.chenx221.helper.CsvHelper;
import cyou.chenx221.helper.UsernameHelper;
import cyou.chenx221.pojo.Course;
import cyou.chenx221.pojo.Score;
import cyou.chenx221.pojo.Student;
import cyou.chenx221.pojo.Teacher;
import cyou.chenx221.service.CourseService;
import cyou.chenx221.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.sql.Date;
@ -26,10 +23,12 @@ import java.util.List;
@RequestMapping("/teacher")
public class TeacherController {
private final TeacherService teacherService;
private final CourseService courseService;
@Autowired
public TeacherController(TeacherService teacherService) {
public TeacherController(TeacherService teacherService, CourseService courseService) {
this.teacherService = teacherService;
this.courseService = courseService;
}
@GetMapping("/manage")
@ -93,8 +92,6 @@ public class TeacherController {
classes = null;
}
String classesPattern = "[1-5]班";// 使用正则表达式验证班级格式
if (id <= 0 && name == null && sex == null && birthdayBegin == null && birthdayEnd == null && course_id <= 0 && course_name == null && classes == null) {
model.addAttribute("errorMessage", "ID非法或查询条件全为空");
return "redirect:/teacher/manage";
@ -110,4 +107,47 @@ public class TeacherController {
model.addAttribute("download", new CsvHelper().generateTeacherCSVFile(teachers));
return "teacherQueryResult";
}
@ResponseBody
@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,
@RequestParam(value = "course_id", defaultValue = "-1", required = false) int course_id,
@RequestParam(value = "classes", defaultValue = "null", required = false) String classes) throws ParseException {
// 处理表单字段数据
String classesPattern = "[1-5]班";// 使用正则表达式验证班级格式
if (id <= 0) {
return ResponseEntity.badRequest().body("ID非法");
} else if (name.equals("null") && birthday_str.equals("null") && course_id == -1 && classes.equals("null")) {
return ResponseEntity.badRequest().body("修改内容为空");
}
Date birthday = null;
if (!birthday_str.equals("null")) {
birthday = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(birthday_str).getTime());
}
if (!classes.equals("null")) {
if (!classes.matches(classesPattern)) {
return ResponseEntity.badRequest().body("班级格式非法");
}
}
if(name.equals("null")){
name=null;
}
if(classes.equals("null")){
classes=null;
}
//
//这里不考虑一个班一个科目不允许多名教师少了个班级表+数据表外键搞得不好所以这里偷懒了...
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("修改失败");
} else {
return ResponseEntity.ok("修改成功");
}
}
}

View File

@ -10,4 +10,7 @@ public interface TeacherDao {
List<Teacher> getAllTeachers();
List<Teacher> getQueryTeachers(Teacher teacher);
int updateTeacher(Teacher teacher);
}

View File

@ -24,6 +24,12 @@ public class TeacherDaoImpl implements TeacherDao {
@Override
public List<Teacher> getQueryTeachers(Teacher teacher) {
return sqlSession.selectList("getQueryTeachers",teacher);
return sqlSession.selectList("getQueryTeachers", teacher);
}
@Override
public int updateTeacher(Teacher teacher) {
return sqlSession.update("updateTeacher", teacher);
}
}

View File

@ -31,6 +31,11 @@ public class Teacher {
this.birthdayEnd = birthdayEnd;
}
public Teacher(Course course, String classes) {
this.course = course;
this.classes = classes;
}
public Teacher(Integer id, String name, String sex, Course course, String classes, Date birthdayBegin, Date birthdayEnd) {
this.id = id;
this.name = name;
@ -41,6 +46,14 @@ public class Teacher {
this.birthdayEnd = birthdayEnd;
}
public Teacher(Integer id, String name, Date birthday, Course course, String classes) {
this.id = id;
this.name = name;
this.birthday = birthday;
this.course = course;
this.classes = classes;
}
@Override
public String toString() {
return "Teacher{" +

View File

@ -21,4 +21,9 @@ public class TeacherService {
public List<Teacher> getQueryTeachers(Teacher teacher) {
return teacherDao.getQueryTeachers(teacher);
}
public int updateTeacher(Teacher teacher) {
return teacherDao.updateTeacher(teacher);
}
}

View File

@ -56,4 +56,27 @@
</if>
</trim>
</select>
<update id="updateTeacher" parameterType="cyou.chenx221.pojo.Teacher">
UPDATE teacher
<set>
<trim suffixOverrides=",">
<if test="name != null">
name = #{name},
</if>
<if test="birthday != null">
birthday = #{birthday},
</if>
<if test="course.CourseID != -1">
course_id = #{course.CourseID},
</if>
<if test="classes != null">
classes = #{classes}
</if>
</trim>
</set>
WHERE id = #{id};
</update>
</mapper>

View File

@ -173,10 +173,12 @@
<td>${teacher.classes}</td>
<td class="text-center">
<div class="btn-group" role="group" aria-label="Basic example">
<button type="button" class="btn btn-danger">
<button type="button" class="btn btn-info" data-mdb-toggle="tooltip"
data-mdb-placement="top" title="编辑">
<i class="fas fa-pencil"></i>
</button>
<button type="button" class="btn btn-primary">
<button type="button" class="btn btn-danger" data-mdb-toggle="tooltip"
data-mdb-placement="top" title="删除">
<i class="fas fa-trash"></i>
</button>
</div>
@ -303,7 +305,7 @@
</div>
<div class="tab-pane fade" id="ex1-tabs-2" role="tabpanel" aria-labelledby="ex1-tab-2">
<div class="card-body" style="min-width: 175px">
<form action="mod" method="post">
<form id="modform" action="mod" method="post">
<div class="row mb-2">
<div class="col-12">
<div class="form-outline">
@ -351,28 +353,28 @@
</div>
</div>
</div>
<div class="row mb-2">
<div class="col-12">
<div class="form-outline">
<input type="text" id="typeText14" class="form-control"
name="course_name"/>
<label class="form-label" for="typeText14">课程名(支持部分)</label>
</div>
</div>
</div>
<%-- <div class="row mb-2">--%>
<%-- <div class="col-12">--%>
<%-- <div class="form-outline">--%>
<%-- <input type="text" id="typeText14" class="form-control"--%>
<%-- name="course_name"/>--%>
<%-- <label class="form-label" for="typeText14">课程名(支持部分)</label>--%>
<%-- </div>--%>
<%-- </div>--%>
<%-- </div>--%>
<div class="row mb-2">
<div class="col-12">
<div class="form-outline">
<input type="text" id="typeText15" class="form-control"
name="classes"/>
<label class="form-label"
for="typeText15">授课班级(一班~五班)</label>
for="typeText15">授课班级(1~5班)</label>
</div>
</div>
</div>
<div class="row mb-2 mt-4">
<div class="col-lg-6 text-center">
<button type="submit" class="btn btn-primary">修改
<button type="submit" class="btn btn-primary" id="modbtn">修改
</button>
</div>
<div class="col-lg-6 text-center">
@ -452,7 +454,7 @@
<div class="form-outline">
<input type="text" id="typeText23" class="form-control" required
name="classes"/>
<label class="form-label" for="typeText23">授课班级</label>
<label class="form-label" for="typeText23">授课班级(1~5班)</label>
</div>
</div>
</div>
@ -557,96 +559,118 @@
</c:if>
});
function resetPWD(id) {
$('#typeText5').val(id);
$('#ex1-tab-2').tab('show');
}
//实现按钮的点击事件(禁用/启用用户)
$(document).ready(function () {
$('.btn-danger').on('click', function () {
$('.btn-danger').on('click', function () { //移除用户
var id = $(this).closest('tr').find('td:first').text();
userdisabled(id);
// userdisabled(id);
console.log(id) // debug
});
$('.btn-info').on('click', function () { //编辑用户
var id = $(this).closest('tr').find('td:first').text();
$('#ex1-tab-2').tab('show');
$('#typeText9').val(id).focus();
// console.log(id) // debug
});
});
$(document).ready(function () {
$('.btn-success').on('click', function () {
var id = $(this).closest('tr').find('td:first').text();
userenabled(id);
// console.log(id) // debug
$('#modform').on('submit', function (event) {
event.preventDefault();
$.ajax({
url: 'mod', // 替换为您的请求 URL
method: 'POST', // 根据您的需求选择请求方法 (POST, GET, etc.)
data: $('#modform').serialize(),
success: function (response) {
$('#de_message').text(response);
$('#de_button').on('click', function () {
reloadka("reload");
});
$('#exampleModal3').modal('show').on('hidden.bs.modal', function () { //针对点击modal外的情况的处理
reloadka("reload");
});
},
error: function (xhr, status, error) {
if (xhr.status === 400) {
$('#de_message').text(xhr.responseText);
$('#de_button').on('click', function () {
reloadka("close");
});
$('#exampleModal3').modal('show');
} else {
console.log('请求失败:', error);
alert('请求请求失败,请重试');
}
}
});
});
});
//使用ajax在后台发送post禁用/启用用户请求
function userdisabled(id) {
//btn-danger
$.ajax({
url: 'userdisabled',
type: 'post',
data: {
id: id
},
success: function (data) {
if (data.status === 'success') {
$('#de_message').text(data.message);
$('#de_button').on('click', function () {
reloadka("reload");
});
$('#exampleModal3').modal('show');
$('#exampleModal3').on('hidden.bs.modal', function () { //针对点击modal外的情况的处理
reloadka("reload");
});
} else {
$('#de_message').text(data.message);
$('#de_button').on('click', function () {
reloadka("close");
});
$('#exampleModal3').modal('show');
}
},
error: function (xhr, status, error) {
// 处理请求失败的逻辑
console.log('请求失败:', error);
alert('请求请求失败,请重试');
}
});
}
function userenabled(id) {
$.ajax({
url: 'userenabled',
type: 'post',
data: {
id: id
},
success: function (data) {
if (data.status === 'success') {
$('#de_message').text(data.message);
$('#de_button').on('click', function () {
reloadka("reload");
});
$('#exampleModal3').modal('show');
$('#exampleModal3').on('hidden.bs.modal', function () {
reloadka("reload");
});
} else {
$('#de_message').text(data.message);
$('#de_button').on('click', function () {
reloadka("close");
});
$('#exampleModal3').modal('show');
}
},
error: function (xhr, status, error) {
// 处理请求失败的逻辑
console.log('请求失败:', error);
alert('请求请求失败,请重试');
}
});
}
// //使用ajax在后台发送post禁用/启用用户请求
// function userdisabled(id) {
// //btn-danger
// $.ajax({
// url: 'userdisabled',
// type: 'post',
// data: {
// id: id
// },
// success: function (data) {
// if (data.status === 'success') {
// $('#de_message').text(data.message);
// $('#de_button').on('click', function () {
// reloadka("reload");
// });
// $('#exampleModal3').modal('show');
// $('#exampleModal3').on('hidden.bs.modal', function () { //针对点击modal外的情况的处理
// reloadka("reload");
// });
// } else {
// $('#de_message').text(data.message);
// $('#de_button').on('click', function () {
// reloadka("close");
// });
// $('#exampleModal3').modal('show');
// }
// },
// error: function (xhr, status, error) {
// // 处理请求失败的逻辑
// console.log('请求失败:', error);
// alert('请求请求失败,请重试');
// }
// });
// }
//
// function userenabled(id) {
// $.ajax({
// url: 'userenabled',
// type: 'post',
// data: {
// id: id
// },
// success: function (data) {
// if (data.status === 'success') {
// $('#de_message').text(data.message);
// $('#de_button').on('click', function () {
// reloadka("reload");
// });
// $('#exampleModal3').modal('show');
// $('#exampleModal3').on('hidden.bs.modal', function () {
// reloadka("reload");
// });
// } else {
// $('#de_message').text(data.message);
// $('#de_button').on('click', function () {
// reloadka("close");
// });
// $('#exampleModal3').modal('show');
// }
// },
// error: function (xhr, status, error) {
// // 处理请求失败的逻辑
// console.log('请求失败:', error);
// alert('请求请求失败,请重试');
// }
// });
// }
//
//处理成功/失败后的刷新页面/关闭模态框操作
function reloadka(action) {
if (action === 'reload') {