student management
In recent years, Java, as a programming language widely used in back-end development, has a wide range of application fields and rich development resources. In the blog of the past few days, we discussed how to build the front-end and back-end environments, laying a solid foundation for the subsequent development work. Today, we will further expand our project and implement an exciting feature - querying information about all students.
This feature is crucial for student management systems. We can use this function to view the information of all students in the system at once, including their names, ages, and other important personal data. Whether it is student advisors, faculty or students themselves, they can easily obtain the information they need. Let’s continue to learn more and implement this feature!
2.1 Query all: backend
2.1.2 Analysis
- Search all students
- Condition query: class, name (fuzzy), age (range)
- Paging query
- Basic information: number, name, age, birthday, gender
- Related information: classes, number of courses selected, course selection details
2.1.2 domain
1) students
package com.czxy.domain;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;
import java.util.List;
/**
* @author 薛慕昭
* @description
*/
@Table(name="tb_student")
public class Student {
@Id
@Column(name="s_id")
private Integer sid; //学生ID
private String sname; //姓名
private Integer age; //年龄
private Date birthday; //生日
private String gender; //性别
@Column(name="c_id")
private Integer cid; //所属班级id
//多对一:多个学生属于一个班级
@Transient //临时,表示数据库没有对应列
private Classes classes; //所属班级
private String cityIds; //城市:320000,321300,321322
//多对多:不同学生选修不同的课程
@Transient //临时,表示数据库没有对应列
private List<Course> courseList;
@Transient //临时,表示数据库没有对应列
private Integer courseCount; //选课数
2) Class
package com.czxy.domain;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @author 薛慕昭
* @description
*/
@Table(name = "tb_class")
public class Classes {
@Id
private Integer cid;
private String cname; //班级名称
@Column(name = "teacher1_id")
private int teacher1Id; //授课老师
@Column(name = "teacher2_id")
private int teacher2Id; //助理老师
@Column(name = "teacher3_id")
private int teacher3Id; //辅导员老师
3) Course selection
package com.czxy.domain;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @author 薛慕昭
* @description
*/
@Table(name = "tb_course")
public class Course {
@Id
@Column(name = "c_id")
private Integer cid;
private String cname;
@Column(name = "`desc`")
private String desc;
2.1.3 Vo
package com.czxy.vo;
/**
* @author 薛慕昭
* @description
*/
public class StudentVo {
private Integer classId; //班级id
private String studentName; //学生姓名
private Integer startAge; //开始年龄
private Integer endAge; //结束年龄
2.1.4 knives
1) students
package com.czxy.mapper;
import com.czxy.domain.Student;
import tk.mybatis.mapper.common.Mapper;
/**
* @author 薛慕昭
* @description
*/
public interface StudentMapper extends Mapper<Student> {
}
2) Class
package com.czxy.mapper;
import com.czxy.domain.Classes;
import tk.mybatis.mapper.common.Mapper;
/**
* @author 薛慕昭
* @description
*/
public interface ClassesMapper extends Mapper<Classes> {
}
3) Course selection
package com.czxy.mapper;
import com.czxy.domain.Course;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
/**
* @author 薛慕昭
* @description
*/
public interface CourseMapper extends Mapper<Course> {
/**
* 查询指定学生的所有课程
* @param sid
* @return
*/
@Select("select c.* from tb_course c, tb_student_course sc where c.c_id = sc.c_id and sc.s_id = #{sid}")
public List<Course> selectAllBySid(@Param("sid") Integer sid);
}
2.1.5 service
-
interface
package com.czxy.service; import com.czxy.domain.Student; import com.czxy.vo.StudentVo; import com.github.pagehelper.PageInfo; /** * @author 薛慕昭 * @description */ public interface StudentService { /** * 查询(基本、分页、条件) * @param studentVo * @param pageNum * @param pageSize * @return */ PageInfo<Student> condition(StudentVo studentVo, Integer pageNum, Integer pageSize); }
-
Implementation class
package com.czxy.service.impl; import com.czxy.domain.Classes; import com.czxy.domain.Course; import com.czxy.domain.Student; import com.czxy.mapper.ClassesMapper; import com.czxy.mapper.CourseMapper; import com.czxy.mapper.StudentMapper; import com.czxy.service.StudentService; import com.czxy.vo.StudentVo; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import tk.mybatis.mapper.entity.Example; import javax.annotation.Resource; import java.util.List; /** * @author 薛慕昭 * @description */ @Service @Transactional public class StudentServiceImpl implements StudentService { @Resource private StudentMapper studentMapper; @Resource private ClassesMapper classesMapper; @Resource private CourseMapper courseMapper; @Override public PageInfo<Student> condition(StudentVo studentVo, Integer pageNum, Integer pageSize) { //1 拼凑条件 // 1.0 获得条件对象 Example studentExample = new Example(Student.class); Example.Criteria studentCriteria = studentExample.createCriteria(); // 1.1 班级 if(studentVo.getClassId() != null) { studentCriteria.andEqualTo("cid", studentVo.getClassId()); } // 1.2 姓名 不能为null,不能为“” , %张% if(studentVo.getStudentName() != null && !"".equals(studentVo.getStudentName())) { studentCriteria.andLike("sname", "%"+studentVo.getStudentName()+"%"); } // 1.3 年龄 30~50 if(studentVo.getStartAge() != null) { studentCriteria.andGreaterThanOrEqualTo("age", studentVo.getStartAge()); } if(studentVo.getEndAge() != null) { studentCriteria.andLessThanOrEqualTo("age", studentVo.getEndAge()); } //2 分页 PageHelper.startPage(pageNum, pageSize); //3.查询 List<Student> studentList = studentMapper.selectByExample(studentExample); //4 关联 for(Student student : studentList) { // 4.1 班级信息 Classes classes = classesMapper.selectByPrimaryKey(student.getCid()); student.setClasses(classes); // 查询学生选修的可成 List<Course> courseList = courseMapper.selectAllBySid(student.getSid()); // 4.2 选课数 student.setCourseCount(courseList.size()); // 4.3 选课详情 student.setCourseList(courseList); } //5 封装 PageInfo<Student> pageInfo = new PageInfo<>(studentList); return pageInfo; } }
2.1.6 controller
package com.czxy.controller;
import com.czxy.domain.Student;
import com.czxy.service.StudentService;
import com.czxy.vo.StudentVo;
import com.github.pagehelper.PageInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* @author 薛慕昭
* @description
*/
@RestController
@RequestMapping("/student")
public class StudentController {
@Resource
private StudentService studentService;
@PostMapping("/condition/{pageSize}/{pageNum}")
public ResponseEntity<PageInfo<Student>> condition(
@RequestBody StudentVo studentVo,
@PathVariable("pageSize") Integer pageSize,
@PathVariable("pageNum") Integer pageNum) {
//查询
PageInfo<Student> pageInfo =
studentService.condition(studentVo, pageNum, pageSize );
//处理结果
return ResponseEntity.ok(pageInfo);
}
}
2.1.7 Testing
2.2 Query all: front end
2.2.1 Requirements
2.2.2 List display
<template>
<div>
<!-- 列表start -->
<el-table
:data="pageInfo.list"
stripe
style="width: 100%">
<el-table-column
prop="sid"
fixed
label="学生ID"
width="180">
</el-table-column>
<el-table-column
prop="classes.cname"
fixed
label="班级名称"
width="180">
</el-table-column>
<el-table-column
prop="sname"
label="学生姓名"
width="180">
</el-table-column>
<el-table-column
prop="age"
label="年龄"
width="180">
</el-table-column>
<el-table-column
prop="birthday"
label="生日"
width="180">
</el-table-column>
<el-table-column
prop="gender"
label="性别"
width="180">
<template slot-scope="scope">
{
{scope.row.gender == 1 ? '男': '女'}}
</template>
</el-table-column>
<el-table-column
prop="courseCount"
label="选课数"
width="180">
</el-table-column>
<el-table-column
label="选课详情"
width="300">
<template slot-scope="scope">
<el-tag v-for="(course,index) in scope.row.courseList" :key="index">{
{course.cname}}</el-tag>
</template>
</el-table-column>
<el-table-column
width="180"
fixed="right"
label="操作">
<template slot-scope="scope">
<el-button size="mini">编辑</el-button>
<el-button size="mini" type="danger">删除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 列表end -->
{
{pageInfo}}
</div>
</template>
<script>
export default {
data() {
return {
studentVo: { //查询条件
classId: '',
studentName: '',
startAge: '',
endAge: ''
},
pageInfo: { //分页条件
pageNum: 1,
pageSize: 2
}
}
},
methods: {
async selectAllStudent() {
//处理请求路径
let url = `/student/condition/${this.pageInfo.pageSize}/${this.pageInfo.pageNum}`
//发送ajax
let {data} = await this.$http.post(url, this.studentVo)
//保存结果
this.pageInfo = data
}
},
mounted() { //页面加载成功
this.selectAllStudent()
},
}
</script>
<style>
</style>
2.2.3 Conditional form
- After the page loads successfully, query all classes
- Write a form: display class list, bind conditional parameters, bind query button
<template>
<div>
<!-- 查询表单start -->
<el-form :inline="true" :model="studentVo" size="mini" class="demo-form-inline">
<el-form-item label="班级">
<el-select v-model="studentVo.classId" clearable @change="selectAllStudent" placeholder="请选择班级">
<el-option v-for="(classes,index) in classesList" :key="index" :label="classes.cname" :value="classes.cid">
</el-option>
</el-select>
</el-form-item>
<el-form-item label="姓名">
<el-input v-model="studentVo.studentName" @keyup.enter.native="selectAllStudent" clearable placeholder="请输入姓名"></el-input>
</el-form-item>
<el-form-item label="年龄">
<el-col :span="11">
<el-input v-model="studentVo.startAge" clearable placeholder="请输入开始年龄"></el-input>
</el-col>
<el-col class="line" :span="2">-</el-col>
<el-col :span="11">
<el-input v-model="studentVo.endAge" clearable placeholder="请输入结束年龄"></el-input>
</el-col>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="selectAllStudent">查询</el-button>
</el-form-item>
</el-form>
<!-- 查询表单end -->
<!-- 列表start -->
<el-table
:data="pageInfo.list"
stripe
style="width: 100%">
<el-table-column
prop="sid"
fixed
label="学生ID"
width="180">
</el-table-column>
<el-table-column
prop="classes.cname"
fixed
label="班级名称"
width="180">
</el-table-column>
<el-table-column
prop="sname"
label="学生姓名"
width="180">
</el-table-column>
<el-table-column
prop="age"
label="年龄"
width="180">
</el-table-column>
<el-table-column
prop="birthday"
label="生日"
width="180">
</el-table-column>
<el-table-column
prop="gender"
label="性别"
width="180">
<template slot-scope="scope">
{
{scope.row.gender == 1 ? '男': '女'}}
</template>
</el-table-column>
<el-table-column
prop="courseCount"
label="选课数"
width="180">
</el-table-column>
<el-table-column
label="选课详情"
width="300">
<template slot-scope="scope">
<el-tag v-for="(course,index) in scope.row.courseList" :key="index">
{
{course.cname}}
</el-tag>
</template>
</el-table-column>
<el-table-column
width="180"
fixed="right"
label="操作">
<template slot-scope="scope">
<el-button size="mini">编辑</el-button>
<el-button size="mini" type="danger">删除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 列表end -->
{
{pageInfo}}
</div>
</template>
<script>
export default {
data() {
return {
studentVo: { //查询条件
classId: '',
studentName: '',
startAge: '',
endAge: ''
},
pageInfo: { //分页条件
pageNum: 1,
pageSize: 5
},
classesList: [], //所有班级
}
},
methods: {
async selectAllStudent() {
//处理请求路径
let url = `/student/condition/${this.pageInfo.pageSize}/${this.pageInfo.pageNum}`
//发送ajax
let {data} = await this.$http.post(url, this.studentVo)
//保存结果
this.pageInfo = data
},
async selectAllClasses() {
let { data } = await this.$http.get('/classes')
this.classesList = data
}
},
mounted() { //页面加载成功
//查询所有学生
this.selectAllStudent()
//查询所有班级
this.selectAllClasses()
},
}
</script>
<style>
.line {
text-align: center;
}
</style>
2.2.4 Pagination strip
-
Show pagination bar
-
Writing paging requires a trigger function
<template>
<div>
<!-- 查询表单start -->
<el-form :inline="true" :model="studentVo" size="mini" class="demo-form-inline">
<el-form-item label="班级">
<el-select v-model="studentVo.classId" clearable @change="selectAllStudent" placeholder="请选择班级">
<el-option v-for="(classes,index) in classesList" :key="index" :label="classes.cname" :value="classes.cid">
</el-option>
</el-select>
</el-form-item>
<el-form-item label="姓名">
<el-input v-model="studentVo.studentName" @keyup.enter.native="selectAllStudent" clearable placeholder="请输入姓名"></el-input>
</el-form-item>
<el-form-item label="年龄">
<el-col :span="11">
<el-input v-model="studentVo.startAge" clearable placeholder="请输入开始年龄"></el-input>
</el-col>
<el-col class="line" :span="2">-</el-col>
<el-col :span="11">
<el-input v-model="studentVo.endAge" clearable placeholder="请输入结束年龄"></el-input>
</el-col>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="selectAllStudent">查询</el-button>
</el-form-item>
</el-form>
<!-- 查询表单end -->
<!-- 列表start -->
<el-table
:data="pageInfo.list"
stripe
style="width: 100%">
<el-table-column
prop="sid"
fixed
label="学生ID"
width="180">
</el-table-column>
<el-table-column
prop="classes.cname"
fixed
label="班级名称"
width="180">
</el-table-column>
<el-table-column
prop="sname"
label="学生姓名"
width="180">
</el-table-column>
<el-table-column
prop="age"
label="年龄"
width="180">
</el-table-column>
<el-table-column
prop="birthday"
label="生日"
width="180">
</el-table-column>
<el-table-column
prop="gender"
label="性别"
width="180">
<template slot-scope="scope">
{
{scope.row.gender == 1 ? '男': '女'}}
</template>
</el-table-column>
<el-table-column
prop="courseCount"
label="选课数"
width="180">
</el-table-column>
<el-table-column
label="选课详情"
width="300">
<template slot-scope="scope">
<el-tag v-for="(course,index) in scope.row.courseList" :key="index">
{
{course.cname}}
</el-tag>
</template>
</el-table-column>
<el-table-column
width="180"
fixed="right"
label="操作">
<template slot-scope="scope">
<el-button size="mini">编辑</el-button>
<el-button size="mini" type="danger">删除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 列表end -->
<!-- 分页条start -->
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="pageInfo.pageNum"
:page-sizes="[1,2,3,5,10]"
:page-size="pageInfo.pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="pageInfo.total">
</el-pagination>
<!-- 分页条end -->
</div>
</template>
<script>
export default {
data() {
return {
studentVo: { //查询条件
classId: '',
studentName: '',
startAge: '',
endAge: ''
},
pageInfo: { //分页条件
pageNum: 1, //当前页(第几页)
pageSize: 2 //每页个数
},
classesList: [], //所有班级
}
},
methods: {
async selectAllStudent() {
//处理请求路径
let url = `/student/condition/${this.pageInfo.pageSize}/${this.pageInfo.pageNum}`
//发送ajax
let {data} = await this.$http.post(url, this.studentVo)
//保存结果
this.pageInfo = data
},
async selectAllClasses() {
let { data } = await this.$http.get('/classes')
this.classesList = data
},
handleSizeChange(val) {
//修改 每页个数
//console.log(`每页 ${val} 条`);
this.pageInfo.pageSize = val
this.pageInfo.pageNum = 1
//再查询
this.selectAllStudent()
},
handleCurrentChange(val) {
//修改当前页
//console.log(`当前页: ${val}`);
this.pageInfo.pageNum = val
//再查询
this.selectAllStudent()
}
},
mounted() { //页面加载成功
//查询所有学生
this.selectAllStudent()
//查询所有班级
this.selectAllClasses()
},
}
</script>
<style>
.line {
text-align: center;
}
</style>
Through the study of this article, we successfully implemented all query functions for student management projects. We built a student management system and added test data to simulate real student information. By calling the showAllStudents() method provided by the system, we can easily view and print the information of all students. This function adds powerful query capabilities to the student management system, making management work more efficient and convenient.
In today's study, we not only consolidated the basic knowledge of Java, but also gained an in-depth understanding of how to process and display large amounts of data. As the project develops, we will continue to explore more interesting and practical features and gradually build a complete student management system. I hope this article has inspired and helped you, and if you have any questions or suggestions, please feel free to share them with us. Looking forward to the next blog and continue to explore the wonderful world of Java development!
SizeChange(val) {
//修改 每页个数
//console.log(`每页 ${val} 条`);
this.pageInfo.pageSize = val
this.pageInfo.pageNum = 1
//再查询
this.selectAllStudent()
},
handleCurrentChange(val) {
//修改当前页
//console.log(`当前页: ${val}`);
this.pageInfo.pageNum = val
//再查询
this.selectAllStudent()
}
},
mounted() {
//页面加载成功
//查询所有学生
this.selectAllStudent()
//查询所有班级
this.selectAllClasses()
},
}
</script>
<style>
.line {
text-align: center;
}
</style>
Through the study of this article, we successfully implemented all query functions for student management projects. We built a student management system and added test data to simulate real student information. By calling the showAllStudents() method provided by the system, we can easily view and print the information of all students. This function adds powerful query capabilities to the student management system, making management work more efficient and convenient.
In today's study, we not only consolidated the basic knowledge of Java, but also gained an in-depth understanding of how to process and display large amounts of data. As the project develops, we will continue to explore more interesting and practical features and gradually build a complete student management system. I hope this article has inspired and helped you, and if you have any questions or suggestions, please feel free to share them with us. Looking forward to the next blog and continue to explore the wonderful world of Java development!