IDEA+SSM+SpringBoot+Vue+Element UI实现班级管理增删改查

举报
陶然同学 发表于 2022/06/22 23:16:30 2022/06/22
【摘要】 环境搭建 SQL CREATE TABLE `tb_class` ( `c_id` varchar(32) NOT NULL COMMENT '班级ID', `c_name` varchar(50) DEFAULT NULL COMMENT '班级名称', `desc` varchar(200) DEFAULT NULL CO...

环境搭建

SQL


  
  1. CREATE TABLE `tb_class` (
  2. `c_id` varchar(32) NOT NULL COMMENT '班级ID',
  3. `c_name` varchar(50) DEFAULT NULL COMMENT '班级名称',
  4. `desc` varchar(200) DEFAULT NULL COMMENT '班级描述',
  5. PRIMARY KEY (`c_id`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  7. /*Data for the table `tb_class` */
  8. insert into `tb_class`(`c_id`,`c_name`,`desc`) values ('22','22','22'),('c001','Java12班','花儿222'),('c002','Java34班','艺术223'),('c003','虚拟的班级','111');

查询

前端

  • - 需求:简单的查询所有
  • - 步骤:
  •   - 步骤1:编写查询所有班级的函数
  •   - 步骤2:页面加载成功后调用
  •   - 步骤3:展示查询结果
  • - 实现

  
  1. <template>
  2. <div>
  3. <!-- 1.4 班级列表 start -->
  4. <el-table
  5. :data="classesList"
  6. stripe
  7. style="width: 100%">
  8. <el-table-column
  9. prop="cid"
  10. label="编号"
  11. width="180">
  12. </el-table-column>
  13. <el-table-column
  14. prop="cname"
  15. label="班级名称"
  16. width="180">
  17. </el-table-column>
  18. <el-table-column
  19. prop="desc"
  20. label="描述"
  21. width="280">
  22. </el-table-column>
  23. <el-table-column
  24. label="操作">
  25. <template slot-scope="scope">
  26. <!-- scope.$index, scope.row -->
  27. <el-button size="mini">编辑</el-button>
  28. <el-button size="mini" type="danger">删除</el-button>
  29. </template>
  30. </el-table-column>
  31. </el-table>
  32. <!-- 1.4 班级列表 end -->
  33. </div>
  34. </template>
  35. <script>
  36. // 1.0 导入axios
  37. import axios from 'axios'
  38. export default {
  39. data() {
  40. return {
  41. classesList: [], // 1.1 班级列表
  42. }
  43. },
  44. methods: {
  45. async selectAllClasses() { //1.2 查询班级
  46. // ajax 查询班级
  47. let url = `http://localhost:8888/classes`
  48. let { data:baseResult } = await axios.get(url)
  49. // 保存数据
  50. this.classesList = baseResult.data
  51. }
  52. },
  53. mounted() {
  54. // 1.3 调用班级
  55. this.selectAllClasses()
  56. },
  57. }
  58. </script>
  59. <style>
  60. </style>

后端

ClassesMapper:


  
  1. package com.czxy.mapper;
  2. import com.czxy.domain.Classes;
  3. import tk.mybatis.mapper.common.Mapper;
  4. @org.apache.ibatis.annotations.Mapper
  5. public interface ClassesMapper extends Mapper<Classes> {
  6. }

ClassesService:


  
  1. package com.czxy.service;
  2. import com.czxy.domain.Classes;
  3. import java.util.List;
  4. public interface ClassesService {
  5. /**
  6. * 查询所有班级
  7. * @return
  8. */
  9. public List<Classes> selectAllClasses();
  10. }

ClassesServiceImpl:


  
  1. package com.czxy.service.impl;
  2. import com.czxy.domain.Classes;
  3. import com.czxy.mapper.ClassesMapper;
  4. import com.czxy.service.ClassesService;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.transaction.annotation.Transactional;
  7. import javax.annotation.Resource;
  8. import java.util.List;
  9. @Service
  10. @Transactional
  11. public class ClassesServiceImpl implements ClassesService {
  12. @Resource
  13. private ClassesMapper classesMapper;
  14. @Override
  15. public List<Classes> selectAllClasses() {
  16. return classesMapper.selectAll();
  17. }
  18. }

ClassesController:


  
  1. package com.czxy.controller;
  2. import com.czxy.domain.Classes;
  3. import com.czxy.service.ClassesService;
  4. import com.czxy.vo.BaseResult;
  5. import org.springframework.web.bind.annotation.*;
  6. import javax.annotation.Resource;
  7. import java.util.List;
  8. @RestController
  9. @RequestMapping("/classes")
  10. @CrossOrigin
  11. public class ClassesController {
  12. @Resource
  13. private ClassesService classesService;
  14. @GetMapping
  15. public BaseResult selectAll(){
  16. List<Classes> list = classesService.selectAllClasses();
  17. return BaseResult.ok("查询成功",list);
  18. }
  19. }

添加:弹出框

前端

  • 需求:使用弹出框完成添加
  • 步骤:
  • 步骤1:拷贝弹出框,并调试代码(变量)
  • 步骤2:声明弹出框显示变量、表单变量(表单绑定)
  • 步骤3:添加函数,与按钮绑定
  • 实现

  
  1. <template>
  2. <div>
  3. <!-- 2.4 添加按钮 -->
  4. <el-button type="primary" round @click="openAddDialog">添加</el-button>
  5. <!-- 1.4 班级列表 start -->
  6. <el-table
  7. :data="classesList"
  8. stripe
  9. style="width: 100%">
  10. <el-table-column
  11. prop="cid"
  12. label="编号"
  13. width="180">
  14. </el-table-column>
  15. <el-table-column
  16. prop="cname"
  17. label="班级名称"
  18. width="180">
  19. </el-table-column>
  20. <el-table-column
  21. prop="desc"
  22. label="描述"
  23. width="280">
  24. </el-table-column>
  25. <el-table-column
  26. label="操作">
  27. <template slot-scope="scope">
  28. <!-- scope.$index, scope.row -->
  29. <el-button size="mini">编辑</el-button>
  30. <el-button size="mini" type="danger">删除</el-button>
  31. </template>
  32. </el-table-column>
  33. </el-table>
  34. <!-- 1.4 班级列表 end -->
  35. <!-- 2.1 添加弹出框 start -->
  36. <el-dialog title="添加班级" :visible.sync="classesAddVisible">
  37. <el-form :model="classes" label-width="80px">
  38. <el-form-item label="班级编号">
  39. <el-input v-model="classes.cid"></el-input>
  40. </el-form-item>
  41. <el-form-item label="班级名称">
  42. <el-input v-model="classes.cname"></el-input>
  43. </el-form-item>
  44. <el-form-item label="班级描述">
  45. <el-input type="textarea" v-model="classes.desc"></el-input>
  46. </el-form-item>
  47. </el-form>
  48. <div slot="footer" class="dialog-footer">
  49. <el-button @click="classesAddVisible = false">取 消</el-button>
  50. <el-button type="primary" @click="addClasses">确 定</el-button>
  51. </div>
  52. </el-dialog>
  53. <!-- 2.1 添加弹出框 end -->
  54. </div>
  55. </template>
  56. <script>
  57. // 1.0 导入axios
  58. import axios from 'axios'
  59. export default {
  60. data() {
  61. return {
  62. classesList: [], // 1.1 班级列表
  63. classesAddVisible: false, // 2.2 添加弹出框的变量
  64. classes: {}, // 2.3 班级表单
  65. }
  66. },
  67. methods: {
  68. async selectAllClasses() { //1.2 查询班级
  69. // ajax 查询班级
  70. let url = `http://localhost:8888/classes`
  71. let { data:baseResult } = await axios.get(url)
  72. // 保存数据
  73. this.classesList = baseResult.data
  74. },
  75. openAddDialog() { //2.5 显示弹出框
  76. this.classesAddVisible = true
  77. },
  78. async addClasses() { //2.6 添加班级
  79. // ajax
  80. var url = `http://localhost:8888/classes`
  81. let { data: baseResult } = await axios.post(url, this.classes)
  82. // 处理结果
  83. if(baseResult.code == 20000) {
  84. // 成功
  85. // 提示
  86. this.$message.success(baseResult.message)
  87. // 刷新页面
  88. this.selectAllClasses()
  89. // 关闭弹出框
  90. this.classesAddVisible = false
  91. } else {
  92. // 失败
  93. this.$message.error(baseResult.message)
  94. }
  95. }
  96. },
  97. mounted() {
  98. // 1.3 调用班级
  99. this.selectAllClasses()
  100. },
  101. }
  102. </script>
  103. <style>
  104. </style>

后端

ClassesService


  
  1. package com.czxy.service;
  2. import com.czxy.domain.Classes;
  3. import java.util.List;
  4. public interface ClassesService {
  5. /**
  6. * 添加 | 修改 班级
  7. * @return
  8. */
  9. public boolean addClasses(Classes classes);
  10. }

ClassesServiceImpl


  
  1. package com.czxy.service.impl;
  2. import com.czxy.domain.Classes;
  3. import com.czxy.mapper.ClassesMapper;
  4. import com.czxy.service.ClassesService;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.transaction.annotation.Transactional;
  7. import javax.annotation.Resource;
  8. import java.util.List;
  9. @Service
  10. @Transactional
  11. public class ClassesServiceImpl implements ClassesService {
  12. @Override
  13. public boolean addClasses(Classes classes) {
  14. Classes cls = classesMapper.selectByPrimaryKey(classes.getCid());
  15. int result;
  16. if(cls != null){
  17. result = classesMapper.updateByPrimaryKey(classes);
  18. }else{
  19. result = classesMapper.insert(classes);
  20. }
  21. return result == 1;
  22. }
  23. }

ClassesController


  
  1. package com.czxy.controller;
  2. import com.czxy.domain.Classes;
  3. import com.czxy.service.ClassesService;
  4. import com.czxy.vo.BaseResult;
  5. import org.springframework.web.bind.annotation.*;
  6. import javax.annotation.Resource;
  7. import java.util.List;
  8. @RestController
  9. @RequestMapping("/classes")
  10. @CrossOrigin
  11. public class ClassesController {
  12. @Resource
  13. private ClassesService classesService;
  14. @PostMapping
  15. public BaseResult addupdateClasses(@RequestBody Classes classes){
  16. try {
  17. boolean b = classesService.addClasses(classes);
  18. if(b){
  19. return BaseResult.ok("添加 | 编辑 成功");
  20. }else{
  21. return BaseResult.error("添加 | 编辑 失败");
  22. }
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. return BaseResult.error(e.getMessage());
  26. }
  27. }
  28. }

修改:抽屉

前端

  • 需求:使用抽屉完成修改功能
  • 步骤
  • 步骤1:点击修改按钮,打开一个抽泣,通过id查询详情,需要回显表单
  • 步骤2:表单的绑定
  • 步骤3:修改确定按钮
  • 实现

  
  1. <template>
  2. <div>
  3. <!-- 2.4 添加按钮 -->
  4. <el-button type="primary" round @click="openAddDialog">添加</el-button>
  5. <!-- 1.4 班级列表 start -->
  6. <el-table
  7. :data="classesList"
  8. stripe
  9. style="width: 100%">
  10. <el-table-column
  11. prop="cid"
  12. label="编号"
  13. width="180">
  14. </el-table-column>
  15. <el-table-column
  16. prop="cname"
  17. label="班级名称"
  18. width="180">
  19. </el-table-column>
  20. <el-table-column
  21. prop="desc"
  22. label="描述"
  23. width="280">
  24. </el-table-column>
  25. <el-table-column
  26. label="操作">
  27. <template slot-scope="scope">
  28. <!-- scope.$index, scope.row -->
  29. <!-- 3.1 打开编辑抽屉 -->
  30. <el-button size="mini" @click="openEditDrawer(scope.row.cid)">编辑</el-button>
  31. <el-button size="mini" type="danger">删除</el-button>
  32. </template>
  33. </el-table-column>
  34. </el-table>
  35. <!-- 1.4 班级列表 end -->
  36. <!-- 2.1 添加弹出框 start -->
  37. <el-dialog title="添加班级" :visible.sync="classesAddVisible">
  38. <el-form :model="classes" label-width="80px">
  39. <el-form-item label="班级编号">
  40. <el-input v-model="classes.cid"></el-input>
  41. </el-form-item>
  42. <el-form-item label="班级名称">
  43. <el-input v-model="classes.cname"></el-input>
  44. </el-form-item>
  45. <el-form-item label="班级描述">
  46. <el-input type="textarea" v-model="classes.desc"></el-input>
  47. </el-form-item>
  48. </el-form>
  49. <div slot="footer" class="dialog-footer">
  50. <el-button @click="classesAddVisible = false">取 消</el-button>
  51. <el-button type="primary" @click="addClasses">确 定</el-button>
  52. </div>
  53. </el-dialog>
  54. <!-- 2.1 添加弹出框 end -->
  55. <!-- 3.3 抽屉 start -->
  56. <el-drawer
  57. title="修改班级"
  58. :visible.sync="classesEditDrawerVisible"
  59. direction="rtl">
  60. <!-- 修改表单 start -->
  61. <el-form :model="classes" label-width="80px">
  62. <el-form-item label="班级编号">
  63. <el-input v-model="classes.cid"></el-input>
  64. </el-form-item>
  65. <el-form-item label="班级名称">
  66. <el-input v-model="classes.cname"></el-input>
  67. </el-form-item>
  68. <el-form-item label="班级描述">
  69. <el-input type="textarea" v-model="classes.desc"></el-input>
  70. </el-form-item>
  71. <el-form-item>
  72. <el-button type="primary" @click="editClasses">立即创建</el-button>
  73. <el-button @click="classesEditDrawerVisible = false">取消</el-button>
  74. </el-form-item>
  75. </el-form>
  76. <!-- 修改表单 end -->
  77. </el-drawer>
  78. <!-- 3.3 抽屉 end -->
  79. </div>
  80. </template>
  81. <script>
  82. // 1.0 导入axios
  83. import axios from 'axios'
  84. export default {
  85. data() {
  86. return {
  87. classesList: [], // 1.1 班级列表
  88. classesAddVisible: false, // 2.2 添加弹出框的变量
  89. classes: {}, // 2.3 班级表单
  90. classesEditDrawerVisible: false, // 3.4 抽屉变量
  91. }
  92. },
  93. methods: {
  94. async selectAllClasses() { //1.2 查询班级
  95. // ajax 查询班级
  96. let url = `http://localhost:8888/classes`
  97. let { data:baseResult } = await axios.get(url)
  98. // 保存数据
  99. this.classesList = baseResult.data
  100. },
  101. openAddDialog() { //2.5 显示弹出框
  102. this.classesAddVisible = true
  103. },
  104. async addClasses() { //2.6 添加班级
  105. // ajax
  106. var url = `http://localhost:8888/classes`
  107. let { data: baseResult } = await axios.post(url, this.classes)
  108. // 处理结果
  109. if(baseResult.code == 20000) {
  110. // 成功
  111. // 提示
  112. this.$message.success(baseResult.message)
  113. // 刷新页面
  114. this.selectAllClasses()
  115. // 关闭弹出框
  116. this.classesAddVisible = false
  117. } else {
  118. // 失败
  119. this.$message.error(baseResult.message)
  120. }
  121. },
  122. async openEditDrawer(cid) { //3.2 打开编辑抽屉函数
  123. // 打开抽屉
  124. this.classesEditDrawerVisible = true
  125. // 查询详情
  126. let url = `http://localhost:8888/classes/${cid}`
  127. let { data: baseResult } = await axios.get(url)
  128. this.classes = baseResult.data
  129. },
  130. async editClasses() { //3.4 修改班级函数
  131. // ajax
  132. var url = `http://localhost:8888/classes`
  133. let { data: baseResult } = await axios.put(url, this.classes)
  134. // 处理结果
  135. if(baseResult.code == 20000) {
  136. // 成功
  137. // 提示
  138. this.$message.success(baseResult.message)
  139. // 刷新页面
  140. this.selectAllClasses()
  141. // 关闭抽屉
  142. this.classesEditDrawerVisible = false
  143. } else {
  144. // 失败
  145. this.$message.error(baseResult.message)
  146. }
  147. }
  148. },
  149. mounted() {
  150. // 1.3 调用班级
  151. this.selectAllClasses()
  152. },
  153. }
  154. </script>
  155. <style>
  156. </style>

后端

ClassesService


  
  1. package com.czxy.service;
  2. import com.czxy.domain.Classes;
  3. import java.util.List;
  4. public interface ClassesService {
  5. /**
  6. * 添加 | 修改 班级
  7. * @return
  8. */
  9. public boolean addClasses(Classes classes);
  10. /**
  11. * 查询指定班级
  12. * @param cid
  13. * @return
  14. */
  15. public Classes selectById(String cid);
  16. }

ClassesServiceImpl


  
  1. package com.czxy.service.impl;
  2. import com.czxy.domain.Classes;
  3. import com.czxy.mapper.ClassesMapper;
  4. import com.czxy.service.ClassesService;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.transaction.annotation.Transactional;
  7. import javax.annotation.Resource;
  8. import java.util.List;
  9. @Service
  10. @Transactional
  11. public class ClassesServiceImpl implements ClassesService {
  12. @Resource
  13. private ClassesMapper classesMapper;
  14. @Override
  15. public boolean addClasses(Classes classes) {
  16. Classes cls = classesMapper.selectByPrimaryKey(classes.getCid());
  17. int result;
  18. if(cls != null){
  19. result = classesMapper.updateByPrimaryKey(classes);
  20. }else{
  21. result = classesMapper.insert(classes);
  22. }
  23. return result == 1;
  24. }
  25. @Override
  26. public Classes selectById(String cid) {
  27. Classes classes = classesMapper.selectByPrimaryKey(cid);
  28. return classes;
  29. }
  30. }

ClassesServiceController


  
  1. package com.czxy.controller;
  2. import com.czxy.domain.Classes;
  3. import com.czxy.service.ClassesService;
  4. import com.czxy.vo.BaseResult;
  5. import org.springframework.web.bind.annotation.*;
  6. import javax.annotation.Resource;
  7. import java.util.List;
  8. /**
  9. * @Author 刘嘉俊
  10. * @Date 2022/3/21
  11. */
  12. @RestController
  13. @RequestMapping("/classes")
  14. @CrossOrigin
  15. public class ClassesController {
  16. @Resource
  17. private ClassesService classesService;
  18. @PostMapping
  19. public BaseResult addupdateClasses(@RequestBody Classes classes){
  20. try {
  21. boolean b = classesService.addClasses(classes);
  22. if(b){
  23. return BaseResult.ok("添加 | 编辑 成功");
  24. }else{
  25. return BaseResult.error("添加 | 编辑 失败");
  26. }
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. return BaseResult.error(e.getMessage());
  30. }
  31. }
  32. @GetMapping("/{cid}")
  33. public BaseResult selectById(@PathVariable("cid") String cid){
  34. System.out.println(cid);
  35. Classes classes = classesService.selectById(cid);
  36. return BaseResult.ok("查询成功",classes);
  37. }
  38. }

删除

基本功能

  • 需求:点击删除按钮,完成删除操作

  • 实现


  
  1. <template>
  2. <div>
  3. <!-- 2.4 添加按钮 -->
  4. <el-button type="primary" round @click="openAddDialog">添加</el-button>
  5. <!-- 1.4 班级列表 start -->
  6. <el-table
  7. :data="classesList"
  8. stripe
  9. style="width: 100%">
  10. <el-table-column
  11. prop="cid"
  12. label="编号"
  13. width="180">
  14. </el-table-column>
  15. <el-table-column
  16. prop="cname"
  17. label="班级名称"
  18. width="180">
  19. </el-table-column>
  20. <el-table-column
  21. prop="desc"
  22. label="描述"
  23. width="280">
  24. </el-table-column>
  25. <el-table-column
  26. label="操作">
  27. <template slot-scope="scope">
  28. <!-- scope.$index, scope.row -->
  29. <!-- 3.1 打开编辑抽屉 -->
  30. <el-button size="mini" @click="openEditDrawer(scope.row.cid)">编辑</el-button>
  31. <!-- 4.1 删除 -->
  32. <el-button size="mini" @click="deleteClasses(scope.row.cid)" type="danger">删除</el-button>
  33. </template>
  34. </el-table-column>
  35. </el-table>
  36. <!-- 1.4 班级列表 end -->
  37. <!-- 2.1 添加弹出框 start -->
  38. <el-dialog title="添加班级" :visible.sync="classesAddVisible">
  39. <el-form :model="classes" label-width="80px">
  40. <el-form-item label="班级编号">
  41. <el-input v-model="classes.cid"></el-input>
  42. </el-form-item>
  43. <el-form-item label="班级名称">
  44. <el-input v-model="classes.cname"></el-input>
  45. </el-form-item>
  46. <el-form-item label="班级描述">
  47. <el-input type="textarea" v-model="classes.desc"></el-input>
  48. </el-form-item>
  49. </el-form>
  50. <div slot="footer" class="dialog-footer">
  51. <el-button @click="classesAddVisible = false">取 消</el-button>
  52. <el-button type="primary" @click="addClasses">确 定</el-button>
  53. </div>
  54. </el-dialog>
  55. <!-- 2.1 添加弹出框 end -->
  56. <!-- 3.3 抽屉 start -->
  57. <el-drawer
  58. title="修改班级"
  59. :visible.sync="classesEditDrawerVisible"
  60. direction="rtl">
  61. <!-- 修改表单 start -->
  62. <el-form :model="classes" label-width="80px">
  63. <el-form-item label="班级编号">
  64. <el-input v-model="classes.cid"></el-input>
  65. </el-form-item>
  66. <el-form-item label="班级名称">
  67. <el-input v-model="classes.cname"></el-input>
  68. </el-form-item>
  69. <el-form-item label="班级描述">
  70. <el-input type="textarea" v-model="classes.desc"></el-input>
  71. </el-form-item>
  72. <el-form-item>
  73. <el-button type="primary" @click="editClasses">立即创建</el-button>
  74. <el-button @click="classesEditDrawerVisible = false">取消</el-button>
  75. </el-form-item>
  76. </el-form>
  77. <!-- 修改表单 end -->
  78. </el-drawer>
  79. <!-- 3.3 抽屉 end -->
  80. </div>
  81. </template>
  82. <script>
  83. // 1.0 导入axios
  84. import axios from 'axios'
  85. export default {
  86. data() {
  87. return {
  88. classesList: [], // 1.1 班级列表
  89. classesAddVisible: false, // 2.2 添加弹出框的变量
  90. classes: {}, // 2.3 班级表单
  91. classesEditDrawerVisible: false, // 3.4 抽屉变量
  92. }
  93. },
  94. methods: {
  95. async selectAllClasses() { //1.2 查询班级
  96. // ajax 查询班级
  97. let url = `http://localhost:8888/classes`
  98. let { data:baseResult } = await axios.get(url)
  99. // 保存数据
  100. this.classesList = baseResult.data
  101. },
  102. openAddDialog() { //2.5 显示弹出框
  103. this.classesAddVisible = true
  104. },
  105. async addClasses() { //2.6 添加班级
  106. // ajax
  107. var url = `http://localhost:8888/classes`
  108. let { data: baseResult } = await axios.post(url, this.classes)
  109. // 处理结果
  110. if(baseResult.code == 20000) {
  111. // 成功
  112. // 提示
  113. this.$message.success(baseResult.message)
  114. // 刷新页面
  115. this.selectAllClasses()
  116. // 关闭弹出框
  117. this.classesAddVisible = false
  118. } else {
  119. // 失败
  120. this.$message.error(baseResult.message)
  121. }
  122. },
  123. async openEditDrawer(cid) { //3.2 打开编辑抽屉函数
  124. // 打开抽屉
  125. this.classesEditDrawerVisible = true
  126. // 查询详情
  127. let url = `http://localhost:8888/classes/${cid}`
  128. let { data: baseResult } = await axios.get(url)
  129. this.classes = baseResult.data
  130. },
  131. async editClasses() { //3.4 修改班级函数
  132. // ajax
  133. var url = `http://localhost:8888/classes`
  134. let { data: baseResult } = await axios.put(url, this.classes)
  135. // 处理结果
  136. if(baseResult.code == 20000) {
  137. // 成功
  138. // 提示
  139. this.$message.success(baseResult.message)
  140. // 刷新页面
  141. this.selectAllClasses()
  142. // 关闭抽屉
  143. this.classesEditDrawerVisible = false
  144. } else {
  145. // 失败
  146. this.$message.error(baseResult.message)
  147. }
  148. },
  149. deleteClasses(cid) { // 4.2 删除功能
  150. // 询问
  151. this.$confirm('您确定要删除么?', '删除提示', {
  152. type: 'warning'
  153. }).then(async () => {
  154. // 确认
  155. let url = `http://localhost:8888/classes/${cid}`
  156. let { data:baseResult } = await axios.delete(url)
  157. if(baseResult.code == 20000) {
  158. // 成功
  159. this.$message.success(baseResult.message)
  160. // 刷新
  161. this.selectAllClasses()
  162. } else {
  163. // 失败
  164. this.$message.error(baseResult.message)
  165. }
  166. }).catch(() => {
  167. //
  168. this.$message.info('用户已取消')
  169. });
  170. }
  171. },
  172. mounted() {
  173. // 1.3 调用班级
  174. this.selectAllClasses()
  175. },
  176. }
  177. </script>
  178. <style>
  179. </style>

校验关联信息

  • 需求:如果有关联数据,先查询,再给出提示

  • 步骤:

    • 步骤1:修改student mapper ,提供查询指定班级的学生

    • 步骤2:修改classes serivce,进行数据校验

  • 步骤1:修改student mapper ,提供查询指定班级的学生


  
  1. package com.czxy.mapper;
  2. import com.czxy.domain.Student;
  3. import org.apache.ibatis.annotations.Param;
  4. import org.apache.ibatis.annotations.Select;
  5. import tk.mybatis.mapper.common.Mapper;
  6. import java.util.List;
  7. @org.apache.ibatis.annotations.Mapper
  8. public interface StudentMapper extends Mapper<Student> {
  9. /**
  10. * 通过班级id查询
  11. * @param classesId
  12. * @return
  13. */
  14. @Select("SELECT *,s_id AS sid , c_id AS cid FROM tb_student WHERE c_id = #{classesId}")
  15. public List<Student> selectAllByCid(@Param("classesId") String classesId);
  16. }

步骤2:修改classes serivce,进行数据校验


  
  1. @Override
  2. public boolean delete(String cid) {
  3. // 校验:查询,如果有数据抛异常
  4. List<Student> studentList = studentMapper.selectAllByCid(cid);
  5. if(studentList.size() > 0 ) {
  6. // 有关联数据,不允许删除
  7. throw new RuntimeException("班级关联学生,请先删除学生!");
  8. }
  9. // 删除班级
  10. int delete = classesMapper.deleteByPrimaryKey(cid);
  11. return delete == 1;
  12. }

 配置类

BaseResult


  
  1. package com.czxy.vo;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. public class BaseResult<T> {
  5. //成功状态码
  6. public static final int OK = 20000;
  7. //失败状态码
  8. public static final int ERROR = 0;
  9. //返回码
  10. private Integer code;
  11. //返回消息
  12. private String message;
  13. //存放数据
  14. private T data;
  15. //其他数据
  16. private Map<String,Object> other = new HashMap<>();
  17. public BaseResult() {
  18. }
  19. public BaseResult(Integer code, String message) {
  20. this.code = code;
  21. this.message = message;
  22. }
  23. public BaseResult(Integer code, String message, T data) {
  24. this.code = code;
  25. this.message = message;
  26. this.data = data;
  27. }
  28. /**
  29. * 快捷成功BaseResult对象
  30. * @param message
  31. * @return
  32. */
  33. public static BaseResult ok(String message){
  34. return new BaseResult<>(BaseResult.OK , message);
  35. }
  36. public static BaseResult ok(String message, Object data){
  37. return new BaseResult<>(BaseResult.OK , message, data );
  38. }
  39. /**
  40. * 快捷失败BaseResult对象
  41. * @param message
  42. * @return
  43. */
  44. public static BaseResult error(String message){
  45. return new BaseResult(BaseResult.ERROR , message);
  46. }
  47. /**
  48. * 自定义数据区域
  49. * @param key
  50. * @param msg
  51. * @return
  52. */
  53. public BaseResult append(String key , Object msg){
  54. other.put(key , msg);
  55. return this;
  56. }
  57. public Integer getCode() {
  58. return code;
  59. }
  60. public String getMessage() {
  61. return message;
  62. }
  63. public T getData() {
  64. return data;
  65. }
  66. public Map<String, Object> getOther() {
  67. return other;
  68. }
  69. }

文章来源: blog.csdn.net,作者:陶然同学,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_45481821/article/details/123636755

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。