TeacherQuery
Signed-off-by: Chenx221 <chenx221@yandex.com>
This commit is contained in:
parent
4a4871eccf
commit
ebb95bbfa4
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -14,10 +14,46 @@
|
|||||||
<result property="CourseName" column="CourseName"/>
|
<result property="CourseName" column="CourseName"/>
|
||||||
</association>
|
</association>
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
<select id="getAllTeachers" resultMap="TeacherResultMap">
|
<select id="getAllTeachers" resultMap="TeacherResultMap">
|
||||||
SELECT id, name, sex, birthday, classes, c.CourseID, c.CourseName
|
SELECT id, name, sex, birthday, classes, c.CourseID, c.CourseName
|
||||||
FROM teacher t
|
FROM teacher t
|
||||||
JOIN course c ON c.CourseID = t.course_id
|
JOIN course c ON c.CourseID = t.course_id
|
||||||
WHERE t.removed = 0;
|
WHERE t.removed = 0;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="getQueryTeachers" parameterType="cyou.chenx221.pojo.Teacher" resultMap="TeacherResultMap">
|
||||||
|
SELECT id, name, sex, birthday, classes, c.CourseID, c.CourseName
|
||||||
|
FROM teacher t
|
||||||
|
JOIN course c ON c.CourseID = t.course_id
|
||||||
|
WHERE t.removed = 0
|
||||||
|
<trim prefix="AND" prefixOverrides="AND | OR">
|
||||||
|
<if test="id != -1">
|
||||||
|
AND id = #{id}
|
||||||
|
</if>
|
||||||
|
<if test="name != null">
|
||||||
|
AND name = #{name}
|
||||||
|
</if>
|
||||||
|
<if test="sex != null">
|
||||||
|
AND sex = #{sex}
|
||||||
|
</if>
|
||||||
|
<if test="course !=null">
|
||||||
|
<if test="course.CourseID != null">
|
||||||
|
AND c.CourseID = #{course.courseID}
|
||||||
|
</if>
|
||||||
|
<if test="course.CourseName != null">
|
||||||
|
AND c.CourseName LIKE CONCAT('%', #{course.courseName}, '%')
|
||||||
|
</if>
|
||||||
|
</if>
|
||||||
|
<if test="classes != null">
|
||||||
|
AND classes = #{classes}
|
||||||
|
</if>
|
||||||
|
<if test="birthdayBegin != null">
|
||||||
|
AND birthday >= #{birthdayBegin}
|
||||||
|
</if>
|
||||||
|
<if test="birthdayEnd != null">
|
||||||
|
AND birthday <= #{birthdayEnd}
|
||||||
|
</if>
|
||||||
|
</trim>
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
@ -25,7 +25,7 @@
|
|||||||
|
|
||||||
.table-container table {
|
.table-container table {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
/*height: 100%;*/
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
@ -158,7 +158,7 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<c:forEach var="teacher" items="${teacherList}">
|
<c:forEach var="teacher" items="${teachers}">
|
||||||
<tr>
|
<tr>
|
||||||
<td>${teacher.id}</td>
|
<td>${teacher.id}</td>
|
||||||
<td>${teacher.name}</td>
|
<td>${teacher.name}</td>
|
||||||
|
@ -133,7 +133,19 @@
|
|||||||
style="background-image: url('${pageContext.request.contextPath}/resources/img/jason-blackeye-nyL-rzwP-Mk-unsplash.jpg'); margin-top: -58.59px;">
|
style="background-image: url('${pageContext.request.contextPath}/resources/img/jason-blackeye-nyL-rzwP-Mk-unsplash.jpg'); margin-top: -58.59px;">
|
||||||
<div class="mask d-flex align-items-center h-100" style="background-color: hsla(0, 0%, 100%, 0.5);">
|
<div class="mask d-flex align-items-center h-100" style="background-color: hsla(0, 0%, 100%, 0.5);">
|
||||||
<div class="container d-flex justify-content-center">
|
<div class="container d-flex justify-content-center">
|
||||||
<button type="button" class="btn btn-primary" onclick="location.href='../dashboard'">返回</button>
|
<div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-12">
|
||||||
|
<button type="button" class="btn btn-primary" onclick="location.href='../dashboard'">返回
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-12">
|
||||||
|
<button type="button" class="btn btn-primary" onclick="exportCSV()">导出</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="table-container rounded-4 shadow-3-strong"
|
<div class="table-container rounded-4 shadow-3-strong"
|
||||||
style="background-color: rgba(255,255,255,0.9); overflow-y: auto; max-height: 400px">
|
style="background-color: rgba(255,255,255,0.9); overflow-y: auto; max-height: 400px">
|
||||||
<table class="table table-striped table-hover border-primary align-middle">
|
<table class="table table-striped table-hover border-primary align-middle">
|
||||||
@ -642,6 +654,17 @@
|
|||||||
$('#exampleModal3').modal('hide');
|
$('#exampleModal3').modal('hide');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function exportCSV() {
|
||||||
|
// 构造请求 URL,根据需要导出的类型传递参数
|
||||||
|
var exportUrl = '/output/export-csv?type=teacher'; // 示例:导出学生数据
|
||||||
|
var link = document.createElement('a');
|
||||||
|
link.href = exportUrl;
|
||||||
|
link.style.display = 'none';
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
@ -3,9 +3,11 @@ package cyou.chenx221.controller;
|
|||||||
import cyou.chenx221.pojo.Course;
|
import cyou.chenx221.pojo.Course;
|
||||||
import cyou.chenx221.pojo.Score;
|
import cyou.chenx221.pojo.Score;
|
||||||
import cyou.chenx221.pojo.Student;
|
import cyou.chenx221.pojo.Student;
|
||||||
|
import cyou.chenx221.pojo.Teacher;
|
||||||
import cyou.chenx221.service.CourseService;
|
import cyou.chenx221.service.CourseService;
|
||||||
import cyou.chenx221.service.ScoreService;
|
import cyou.chenx221.service.ScoreService;
|
||||||
import cyou.chenx221.service.StudentService;
|
import cyou.chenx221.service.StudentService;
|
||||||
|
import cyou.chenx221.service.TeacherService;
|
||||||
import org.apache.commons.csv.CSVFormat;
|
import org.apache.commons.csv.CSVFormat;
|
||||||
import org.apache.commons.csv.CSVPrinter;
|
import org.apache.commons.csv.CSVPrinter;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -34,6 +36,9 @@ public class OutputController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ScoreService scoreService;
|
private ScoreService scoreService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TeacherService teacherService;
|
||||||
|
|
||||||
@GetMapping("/export-csv")
|
@GetMapping("/export-csv")
|
||||||
public void exportCSV(HttpServletResponse response, @RequestParam("type") String type) throws IOException {
|
public void exportCSV(HttpServletResponse response, @RequestParam("type") String type) throws IOException {
|
||||||
response.setHeader("Content-Disposition", "attachment; filename=export.csv");
|
response.setHeader("Content-Disposition", "attachment; filename=export.csv");
|
||||||
@ -59,6 +64,11 @@ public class OutputController {
|
|||||||
List<Score> scores = scoreService.getAllScores(); // 根据实际情况获取成绩数据
|
List<Score> scores = scoreService.getAllScores(); // 根据实际情况获取成绩数据
|
||||||
data = transformScoresToData(scores);
|
data = transformScoresToData(scores);
|
||||||
}
|
}
|
||||||
|
case "teacher" -> {
|
||||||
|
headers = Arrays.asList("教师ID", "姓名", "性别","出生日期","授课课程ID","授课课程名","授课班级");
|
||||||
|
List<Teacher> teachers = teacherService.getAllTeachers(); // 根据实际情况获取教师数据
|
||||||
|
data = transformTeachersToData(teachers);
|
||||||
|
}
|
||||||
default -> throw new IllegalArgumentException("Invalid export type: " + type);
|
default -> throw new IllegalArgumentException("Invalid export type: " + type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,6 +132,23 @@ public class OutputController {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<List<String>> transformTeachersToData(List<Teacher> teachers) {
|
||||||
|
List<List<String>> data = new ArrayList<>();
|
||||||
|
for (Teacher teacher : teachers) {
|
||||||
|
List<String> rowData = Arrays.asList(
|
||||||
|
String.valueOf(teacher.getId()),
|
||||||
|
teacher.getName(),
|
||||||
|
teacher.getSex(),
|
||||||
|
teacher.getBirthday().toString(),
|
||||||
|
String.valueOf(teacher.getCourse().getCourseID()),
|
||||||
|
teacher.getCourse().getCourseName(),
|
||||||
|
teacher.getClasses()
|
||||||
|
);
|
||||||
|
data.add(rowData);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/get-csv")
|
@GetMapping("/get-csv")
|
||||||
public void getCSV(HttpServletResponse response, @RequestParam("filename") String filename) throws IOException {
|
public void getCSV(HttpServletResponse response, @RequestParam("filename") String filename) throws IOException {
|
||||||
String filePath = "file" + File.separator + filename;
|
String filePath = "file" + File.separator + filename;
|
||||||
|
@ -33,11 +33,15 @@ public class TeacherController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/manage")
|
@GetMapping("/manage")
|
||||||
public String manageTeacherInfo(Model model) {
|
public String manageTeacherInfo(@RequestParam(value = "successMessage", required = false, defaultValue = "null") String successMessage,
|
||||||
|
@RequestParam(value = "errorMessage", required = false, defaultValue = "null") String errorMessage,
|
||||||
|
Model model) {
|
||||||
String username = new UsernameHelper().getCurrentUsername();
|
String username = new UsernameHelper().getCurrentUsername();
|
||||||
if (username != null) {
|
if (username != null) {
|
||||||
model.addAttribute("username", username);
|
model.addAttribute("username", username);
|
||||||
}
|
}
|
||||||
|
model.addAttribute("successMessage", successMessage);
|
||||||
|
model.addAttribute("errorMessage", errorMessage);
|
||||||
List<Teacher> teacherList = teacherService.getAllTeachers();
|
List<Teacher> teacherList = teacherService.getAllTeachers();
|
||||||
model.addAttribute("teacherList", teacherList);
|
model.addAttribute("teacherList", teacherList);
|
||||||
return "teachermanage";
|
return "teachermanage";
|
||||||
@ -52,7 +56,7 @@ public class TeacherController {
|
|||||||
@RequestParam(value = "course_id", defaultValue = "-1", required = false) int course_id,//课程ID
|
@RequestParam(value = "course_id", defaultValue = "-1", required = false) int course_id,//课程ID
|
||||||
@RequestParam(value = "course_name", defaultValue = "null", required = false) String course_name,//课程名
|
@RequestParam(value = "course_name", defaultValue = "null", required = false) String course_name,//课程名
|
||||||
@RequestParam(value = "classes", defaultValue = "null", required = false) String classes,//授课班级
|
@RequestParam(value = "classes", defaultValue = "null", required = false) String classes,//授课班级
|
||||||
Model model) {
|
Model model) throws IOException {
|
||||||
|
|
||||||
//username helper start
|
//username helper start
|
||||||
String username = new UsernameHelper().getCurrentUsername();
|
String username = new UsernameHelper().getCurrentUsername();
|
||||||
@ -102,11 +106,8 @@ public class TeacherController {
|
|||||||
}
|
}
|
||||||
Teacher teacher = new Teacher(id, name, sex, (course_id == -1 && course_name == null ? null : (new Course(course_id, course_name))), classes, birthdayBegin, birthdayEnd);
|
Teacher teacher = new Teacher(id, name, sex, (course_id == -1 && course_name == null ? null : (new Course(course_id, course_name))), classes, birthdayBegin, birthdayEnd);
|
||||||
List<Teacher> teachers = teacherService.getQueryTeachers(teacher);
|
List<Teacher> teachers = teacherService.getQueryTeachers(teacher);
|
||||||
|
model.addAttribute("teachers", teachers);
|
||||||
// scores = scoreService.getQueryScores(score);
|
model.addAttribute("download", new CsvHelper().generateTeacherCSVFile(teachers));
|
||||||
// String download = new CsvHelper().generateScoreCSVFile(scores);
|
return "teacherQueryResult";
|
||||||
// model.addAttribute("download", download);
|
|
||||||
// model.addAttribute("scores", scores);
|
|
||||||
return "scoreQueryResult";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package cyou.chenx221.helper;
|
|||||||
import cyou.chenx221.pojo.Course;
|
import cyou.chenx221.pojo.Course;
|
||||||
import cyou.chenx221.pojo.Score;
|
import cyou.chenx221.pojo.Score;
|
||||||
import cyou.chenx221.pojo.Student;
|
import cyou.chenx221.pojo.Student;
|
||||||
|
import cyou.chenx221.pojo.Teacher;
|
||||||
import org.apache.commons.csv.CSVFormat;
|
import org.apache.commons.csv.CSVFormat;
|
||||||
import org.apache.commons.csv.CSVPrinter;
|
import org.apache.commons.csv.CSVPrinter;
|
||||||
|
|
||||||
@ -31,6 +32,10 @@ public class CsvHelper {
|
|||||||
);
|
);
|
||||||
data.add(rowData);
|
data.add(rowData);
|
||||||
}
|
}
|
||||||
|
return getString(headers, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getString(List<String> headers, List<List<String>> data) throws IOException {
|
||||||
String directoryPath = "file";
|
String directoryPath = "file";
|
||||||
File directory = new File(directoryPath);
|
File directory = new File(directoryPath);
|
||||||
|
|
||||||
@ -70,28 +75,7 @@ public class CsvHelper {
|
|||||||
);
|
);
|
||||||
data.add(rowData);
|
data.add(rowData);
|
||||||
}
|
}
|
||||||
String directoryPath = "file";
|
return getString(headers, data);
|
||||||
File directory = new File(directoryPath);
|
|
||||||
|
|
||||||
if (!directory.exists()) {
|
|
||||||
boolean created = directory.mkdir(); //Be careful, path: tomcat's installation path (D:\softs\res\apache-tomcat-9.0.75-windows-x64\apache-tomcat-9.0.75\bin)
|
|
||||||
if (created) {
|
|
||||||
System.out.println("目录已创建");
|
|
||||||
} else {
|
|
||||||
System.out.println("无法创建目录");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
File csvFile = new File("file/export-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + ".csv");
|
|
||||||
try (PrintWriter writer = new PrintWriter(csvFile);
|
|
||||||
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) {
|
|
||||||
csvPrinter.printRecord(headers);
|
|
||||||
for (List<String> row : data) {
|
|
||||||
csvPrinter.printRecord(row);
|
|
||||||
}
|
|
||||||
csvPrinter.flush();
|
|
||||||
}
|
|
||||||
return csvFile.getName();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String generateCourseCSVFile(List<Course> courses) throws IOException {
|
public String generateCourseCSVFile(List<Course> courses) throws IOException {
|
||||||
@ -106,28 +90,25 @@ public class CsvHelper {
|
|||||||
);
|
);
|
||||||
data.add(rowData);
|
data.add(rowData);
|
||||||
}
|
}
|
||||||
String directoryPath = "file";
|
return getString(headers, data);
|
||||||
File directory = new File(directoryPath);
|
|
||||||
|
|
||||||
if (!directory.exists()) {
|
|
||||||
boolean created = directory.mkdir(); //Be careful, path: tomcat's installation path (D:\softs\res\apache-tomcat-9.0.75-windows-x64\apache-tomcat-9.0.75\bin)
|
|
||||||
if (created) {
|
|
||||||
System.out.println("目录已创建");
|
|
||||||
} else {
|
|
||||||
System.out.println("无法创建目录");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
File csvFile = new File("file/export-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + ".csv");
|
|
||||||
try (PrintWriter writer = new PrintWriter(csvFile);
|
|
||||||
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) {
|
|
||||||
csvPrinter.printRecord(headers);
|
|
||||||
for (List<String> row : data) {
|
|
||||||
csvPrinter.printRecord(row);
|
|
||||||
}
|
|
||||||
csvPrinter.flush();
|
|
||||||
}
|
|
||||||
return csvFile.getName();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String generateTeacherCSVFile(List<Teacher> teachers) throws IOException {
|
||||||
|
List<String> headers;
|
||||||
|
headers = Arrays.asList("教师ID", "姓名", "性别","出生日期","授课课程ID","授课课程名","授课班级");
|
||||||
|
List<List<String>> data = new ArrayList<>();
|
||||||
|
for (Teacher teacher : teachers) {
|
||||||
|
List<String> rowData = Arrays.asList(
|
||||||
|
String.valueOf(teacher.getId()),
|
||||||
|
teacher.getName(),
|
||||||
|
teacher.getSex(),
|
||||||
|
teacher.getBirthday().toString(),
|
||||||
|
String.valueOf(teacher.getCourse().getCourseID()),
|
||||||
|
teacher.getCourse().getCourseName(),
|
||||||
|
teacher.getClasses()
|
||||||
|
);
|
||||||
|
data.add(rowData);
|
||||||
|
}
|
||||||
|
return getString(headers, data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,12 @@
|
|||||||
<if test="classes != null">
|
<if test="classes != null">
|
||||||
AND classes = #{classes}
|
AND classes = #{classes}
|
||||||
</if>
|
</if>
|
||||||
# 待添加
|
<if test="birthdayBegin != null">
|
||||||
|
AND birthday >= #{birthdayBegin}
|
||||||
|
</if>
|
||||||
|
<if test="birthdayEnd != null">
|
||||||
|
AND birthday <= #{birthdayEnd}
|
||||||
|
</if>
|
||||||
</trim>
|
</trim>
|
||||||
</select>
|
</select>
|
||||||
</mapper>
|
</mapper>
|
@ -25,7 +25,7 @@
|
|||||||
|
|
||||||
.table-container table {
|
.table-container table {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
/*height: 100%;*/
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
@ -158,7 +158,7 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<c:forEach var="teacher" items="${teacherList}">
|
<c:forEach var="teacher" items="${teachers}">
|
||||||
<tr>
|
<tr>
|
||||||
<td>${teacher.id}</td>
|
<td>${teacher.id}</td>
|
||||||
<td>${teacher.name}</td>
|
<td>${teacher.name}</td>
|
||||||
|
@ -133,7 +133,19 @@
|
|||||||
style="background-image: url('${pageContext.request.contextPath}/resources/img/jason-blackeye-nyL-rzwP-Mk-unsplash.jpg'); margin-top: -58.59px;">
|
style="background-image: url('${pageContext.request.contextPath}/resources/img/jason-blackeye-nyL-rzwP-Mk-unsplash.jpg'); margin-top: -58.59px;">
|
||||||
<div class="mask d-flex align-items-center h-100" style="background-color: hsla(0, 0%, 100%, 0.5);">
|
<div class="mask d-flex align-items-center h-100" style="background-color: hsla(0, 0%, 100%, 0.5);">
|
||||||
<div class="container d-flex justify-content-center">
|
<div class="container d-flex justify-content-center">
|
||||||
<button type="button" class="btn btn-primary" onclick="location.href='../dashboard'">返回</button>
|
<div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-12">
|
||||||
|
<button type="button" class="btn btn-primary" onclick="location.href='../dashboard'">返回
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-12">
|
||||||
|
<button type="button" class="btn btn-primary" onclick="exportCSV()">导出</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="table-container rounded-4 shadow-3-strong"
|
<div class="table-container rounded-4 shadow-3-strong"
|
||||||
style="background-color: rgba(255,255,255,0.9); overflow-y: auto; max-height: 400px">
|
style="background-color: rgba(255,255,255,0.9); overflow-y: auto; max-height: 400px">
|
||||||
<table class="table table-striped table-hover border-primary align-middle">
|
<table class="table table-striped table-hover border-primary align-middle">
|
||||||
@ -642,6 +654,17 @@
|
|||||||
$('#exampleModal3').modal('hide');
|
$('#exampleModal3').modal('hide');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function exportCSV() {
|
||||||
|
// 构造请求 URL,根据需要导出的类型传递参数
|
||||||
|
var exportUrl = '/output/export-csv?type=teacher'; // 示例:导出学生数据
|
||||||
|
var link = document.createElement('a');
|
||||||
|
link.href = exportUrl;
|
||||||
|
link.style.display = 'none';
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user