在Java学生管理项目中,删除和批量删除学生是两个常见且重要的功能。通过删除学生信息,我们可以及时保持学生列表的准确性和完整性。而批量删除学生则能够提高效率,方便同时删除多个学生的信息。本篇文章将介绍如何在Java学生管理项目中实现删除和批量删除学生的功能,让你能够灵活应用这两个关键的操作。
删除
1 分析
2 后端实现
1)Service
-
接口
/** * 通过id删除 * @param sid * @return */ Boolean deleteById(Integer sid);
-
实现类
@Override public Boolean deleteById(Integer sid) { int result = studentMapper.deleteByPrimaryKey(sid); return result == 1; }
2)Controller
@Resource
private CourseService courseService;
@DeleteMapping("/{sid}")
public ResponseEntity<String> delete(@PathVariable("sid") Integer sid) {
//校验
List<Course> courseList = courseService.selectAllBySid(sid);
if(courseList.size() > 0) {
return ResponseEntity.ok(sid + "学生关联"+courseList.size()+"门课程,请先解除关联!");
}
//删除
Boolean result = studentService.deleteById(sid);
if(result) {
return ResponseEntity.ok("删除成功");
}
return ResponseEntity.ok("删除失败");
}
3 前端
deleteStudentById(sid) {
this.$confirm('您确定要删除么?', '删除提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(async () => {
//确认执行代码
// 发送ajax
let { data } = await this.$http.delete(`/student/${sid}`)
// 处理结果
if(data == '删除成功') {
this.$message.success(data)
//刷新
this.selectAllStudent()
return;
}
if(data == '删除失败') {
this.$message.error(data)
return;
}
//其他
this.$message.warning(data)
}).catch(() => {
//取消执行代码
this.$message.info('已取消删除')
});
}
批量删除
1 分析
2 后端
1)Service
-
接口
/** * 批量删除 * @param ids * @return */ Boolean deleteAll(List<Integer> ids);
-
实现类
@Override public Boolean deleteAll(List<Integer> ids) { // 批量条件 Example example = new Example(Student.class); Example.Criteria criteria = example.createCriteria(); criteria.andIn("sid",ids); // 根据条件删除 int i = studentMapper.deleteByExample(example); return i >= 1; }
2)Controller
@PostMapping("/deleteAll")
public ResponseEntity<String> deleteAll(@RequestBody List<Integer> ids) {
//校验
for(Integer sid: ids) {
List<Course> courseList = courseService.selectAllBySid(sid);
if(courseList.size() > 0) {
return ResponseEntity.ok(sid + "学生关联"+courseList.size()+"门课程,请先解除关联!");
}
}
//删除
Boolean result = studentService.deleteAll(ids);
if(result) {
return ResponseEntity.ok("删除成功");
}
return ResponseEntity.ok("删除失败");
}
3)测试
3 前端
-
复选框:记录需要删除的学生id
-
批量删除
-
前端完整代码
<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 @selection-change="handleSelectionChange" style="width: 100%"> <el-table-column type="selection" width="55"> </el-table-column> <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" @click="openUpdateStudentDialog(scope.row.sid)">编辑</el-button> <el-button size="mini" type="danger" @click="deleteStudentById(scope.row.sid)">删除</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 --> <el-button size="mini" type="danger" @click="deleteAll()">批量删除</el-button> <!-- 修改 弹出框 start --> <el-dialog title="编辑学生" :visible.sync="dialogUpdateStudentVisible"> <el-form ref="form" :model="student" label-width="80px"> <el-form-item label="姓名"> <el-input v-model="student.sname"></el-input> </el-form-item> <el-form-item label="班级"> <el-select v-model="student.cid" clearable 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="student.age"></el-input> </el-form-item> <el-form-item label="性别"> <el-radio-group v-model="student.gender"> <el-radio label="1">男</el-radio> <el-radio label="0">女</el-radio> </el-radio-group> </el-form-item> <el-form-item label="生日"> <el-date-picker type="date" v-model="student.birthday" value-format="yyyy-MM-dd" placeholder="选择您的生气" ></el-date-picker> </el-form-item> <el-form-item label="城市"> <el-cascader v-model="student.cityArr" :props="cityProps"> </el-cascader> </el-form-item> <el-form-item label="选课"> <el-checkbox-group v-model="student.courseIds"> <el-checkbox v-for="(course,index) in courseList" :key="index" :label="course.cid" name="type">{{course.cname}}</el-checkbox> </el-checkbox-group> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="dialogUpdateStudentVisible = false">取 消</el-button> <el-button type="primary" @click="updateStudent">确 定</el-button> </div> </el-dialog> <!-- 修改 弹出框 end --> </div> </template> <script> import axios from 'axios' export default { data() { return { studentVo: { //查询条件 classId: '', studentName: '', startAge: '', endAge: '' }, pageInfo: { //分页条件 pageNum: 1, //当前页(第几页) pageSize: 2 //每页个数 }, classesList: [], //所有班级 student: { //修改表单回显对象 cityArr: [], //城市数组 courseIds: [], //选课的id }, courseList: [], //课程列表 dialogUpdateStudentVisible: false, //修改的弹出框控制变量 cityProps: { expandTrigger: 'hover', value: 'cid', label: 'cityName', children: 'children', lazy: true, lazyLoad: async (node, resolve) => { // debugger /* //模拟数据 let nodes = [ { "cid": "320100", "cityName": "南京市", "parentId": "320000" }, { "cid": "321300", "cityName": "宿迁市", "parentId": "320000" } ]*/ let parentId; if(node.root) { // 如果页面加载(第一层:省) parentId = 0 } else { parentId = node.value } // ajax查询 let { data: nodes } = await this.$http.get(`/city/parent/${parentId}`) // 完善代码:给县数据设置成叶子节点 nodes.map(n=>{ n.leaf = node.level >= 2 return n }) // 通过调用resolve将子节点数据返回,通知组件数据加载完成 resolve(nodes); } }, selectedIds: [] } }, 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() }, async selectStudentById(sid) { let { data: student} = await this.$http.get(`/student/${sid}`) this.student = student //将城市cityIds字符串,转换成cityArr数组 this.student.cityArr = this.student.cityIds.split(",") }, async selectAllCourse() { let { data: courseList } = await this.$http.get('/course') this.courseList = courseList }, openUpdateStudentDialog(sid) { //班级列表 this.selectAllClasses() //课程列表 this.selectAllCourse() //查询学生详情 this.selectStudentById(sid) //打开修改弹出框 this.dialogUpdateStudentVisible = true }, async updateStudent() { // 处理数据 this.student.cityIds = this.student.cityArr.join(",") //debugger // 添加ajax let { data } = await this.$http.put('/student', this.student) //提示 this.$message.success(data) //this.$message.error(data) //失败 //刷新列表 this.selectAllStudent() //关闭弹出框 this.dialogUpdateStudentVisible = false }, deleteStudentById(sid) { this.$confirm('您确定要删除么?', '删除提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(async () => { //确认执行代码 // 发送ajax let { data } = await this.$http.delete(`/student/${sid}`) // 处理结果 if(data == '删除成功') { this.$message.success(data) //刷新 this.selectAllStudent() return; } if(data == '删除失败') { this.$message.error(data) return; } //其他 this.$message.warning(data) }).catch(() => { //取消执行代码 this.$message.info('已取消删除') }); }, handleSelectionChange(val) { //根据选中的内容,获得一组id this.selectedIds = val.map(student => student.sid) }, deleteAll() { //非空判断 if(this.selectedIds.length == 0) { this.$message.warning('请先选择'); return; } this.$confirm(`您确定要删除选中的${this.selectedIds.length}个学生么?`, '批量删除提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(async () => { //确认执行代码 // 发送ajax let { data } = await this.$http.post(`/student/deleteAll`, this.selectedIds) // 处理结果 if(data == '删除成功') { this.$message.success(data) //刷新 this.selectAllStudent() return; } if(data == '删除失败') { this.$message.error(data) return; } //其他 this.$message.warning(data) }).catch(() => { //取消执行代码 this.$message.info('已取消删除') }); } }, mounted() { //页面加载成功 //查询所有学生 this.selectAllStudent() //查询所有班级 this.selectAllClasses() }, } </script> <style> .line { text-align: center; } </style>
通过本文的学习,我们了解了如何在Java学生管理项目中实现删除和批量删除学生的功能。删除学生可以帮助我们确保学生列表的准确性,而批量删除学生则可以提高操作效率。希望本文对你在学生管理项目中应用删除和批量删除学生功能有所帮助。