学生管理
近年来,Java作为一门广泛应用于后端开发的编程语言,具备了广泛的应用领域和丰富的开发资源。在前几天的博客中,我们探讨了如何搭建前后端环境,为接下来的开发工作打下了坚实的基础。今天,我们将进一步扩展我们的项目,并实现一个令人期待的功能——查询所有学生的信息。
这个功能对于学生管理系统来说至关重要。我们可以通过这个功能一次性查看系统中所有学生的信息,包括他们的姓名、年龄、以及其他重要的个人数据。无论是学生导员、教工还是学生自己,都可以方便地获取到所需的信息。让我们继续深入学习并实现这个功能吧!
2.1 查询所有:后端
2.1.2 分析
2.1.2 domain
1)学生
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)班级
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)选课
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 dao
1)学生
package com.czxy.mapper;
import com.czxy.domain.Student;
import tk.mybatis.mapper.common.Mapper;
/**
* @author 薛慕昭
* @description
*/
public interface StudentMapper extends Mapper<Student> {
}
2)班级
package com.czxy.mapper;
import com.czxy.domain.Classes;
import tk.mybatis.mapper.common.Mapper;
/**
* @author 薛慕昭
* @description
*/
public interface ClassesMapper extends Mapper<Classes> {
}
3)选课
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
-
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); }
-
实现类
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 测试
2.2 查询所有:前端
2.2.1 需求
2.2.2 列表展示
<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 条件表单
<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 分页条
<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>
通过本文的学习,我们成功地实现了学生管理项目的查询所有功能。我们通过构建一个学生管理系统,并添加了测试数据,以模拟真实的学生信息。通过调用系统提供的showAllStudents()方法,我们可以轻松地查看并打印所有学生的信息。这个功能为学生管理系统增添了强大的查询能力,使得管理工作更加高效和便捷。
在今天的学习中,我们不仅巩固了Java的基础知识,还深入了解了如何处理和展示大量的数据。随着项目的发展,我们将继续探索更多有趣和实用的功能,并逐步建立一个完整的学生管理系统。希望本文对你有所启发和帮助,如果你有任何疑问或建议,请随时与我们分享。期待下一篇博客,继续探索Java开发的奇妙世界!
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>
通过本文的学习,我们成功地实现了学生管理项目的查询所有功能。我们通过构建一个学生管理系统,并添加了测试数据,以模拟真实的学生信息。通过调用系统提供的showAllStudents()方法,我们可以轻松地查看并打印所有学生的信息。这个功能为学生管理系统增添了强大的查询能力,使得管理工作更加高效和便捷。
在今天的学习中,我们不仅巩固了Java的基础知识,还深入了解了如何处理和展示大量的数据。随着项目的发展,我们将继续探索更多有趣和实用的功能,并逐步建立一个完整的学生管理系统。希望本文对你有所启发和帮助,如果你有任何疑问或建议,请随时与我们分享。期待下一篇博客,继续探索Java开发的奇妙世界!
原文地址:https://blog.csdn.net/xuewenyu_/article/details/134707446
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_50308.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!