【Web应用】若依框架:基础篇14 源码阅读-后端代码分析-课程管理模块前后端代码分析

news2025/6/7 9:11:03

文章目录

  • 一、课程管理模块前端代码截图
  • 二、前端代码及分析
    • index.vue
    • course.js
  • 三、前端执行流程
      • 1. 组件初始化
      • 2. 查询操作
      • 3. 列表操作
      • 4. 对话框操作
      • 5. API 请求
      • 6. 执行流程总结
      • 关键点
  • 四、课程管理模块后端代码截图
  • 五、后端代码块
    • CourseController
    • ICourseService
    • CourseMapper
    • Course
  • 六、后端执行逻辑
      • 1. `Course` 类
      • 2. `CourseMapper` 接口
      • 3. `ICourseService` 接口
      • 4. `CourseController` 类
      • 调用关系总结
      • 示例流程
  • 七、前后端交互执行流程
      • **1. 初始化阶段**
        • **前端操作**
        • **后端处理**
        • **前端响应**
      • **2. 查询操作**
        • **前端操作**
        • **后端处理**
        • **前端响应**
      • **3. 新增课程**
        • **前端操作**
        • **后端处理**
        • **前端响应**
      • **4. 修改课程**
        • **前端操作**
        • **后端处理**
        • **前端响应**
      • **5. 删除课程**
        • **前端操作**
        • **后端处理**
        • **前端响应**
      • **6. 导出操作**
        • **前端操作**
        • **后端处理**
        • **前端响应**
      • **7. 总结:前后端交互流程**
      • **关键点**


一、课程管理模块前端代码截图

在这里插入图片描述

二、前端代码及分析

index.vue

<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入课程编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程学科" prop="subject">
        <el-select v-model="queryParams.subject" placeholder="请选择课程学科" clearable>
          <el-option
            v-for="dict in course_subject"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="适用人群" prop="applicablePerson">
        <el-input
          v-model="queryParams.applicablePerson"
          placeholder="请输入适用人群"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['course:course:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['course:course:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['course:course:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['course:course:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程id" align="center" prop="id" />
      <el-table-column label="课程编码" align="center" prop="code" />
      <el-table-column label="课程学科" align="center" prop="subject">
        <template #default="scope">
          <dict-tag :options="course_subject" :value="scope.row.subject"/>
        </template>
      </el-table-column>
      <el-table-column label="课程名称" align="center" prop="name" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="适用人群" align="center" prop="applicablePerson" />
      <el-table-column label="课程介绍" align="center" prop="info" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['course:course:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['course:course:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="courseRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入课程编码" />
        </el-form-item>
        <el-form-item label="课程学科" prop="subject">
          <el-select v-model="form.subject" placeholder="请选择课程学科">
            <el-option
              v-for="dict in course_subject"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="适用人群" prop="applicablePerson">
          <el-input v-model="form.applicablePerson" placeholder="请输入适用人群" />
        </el-form-item>
        <el-form-item label="课程介绍" prop="info">
          <el-input v-model="form.info" placeholder="请输入课程介绍" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Course">
import { listCourse, getCourse, delCourse, addCourse, updateCourse } from "@/api/course/course"

const { proxy } = getCurrentInstance()
const { course_subject } = proxy.useDict('course_subject')

const courseList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    code: null,
    subject: null,
    name: null,
    applicablePerson: null,
  },
  rules: {
    code: [
      { required: true, message: "课程编码不能为空", trigger: "blur" }
    ],
    subject: [
      { required: true, message: "课程学科不能为空", trigger: "change" }
    ],
    name: [
      { required: true, message: "课程名称不能为空", trigger: "blur" }
    ],
    price: [
      { required: true, message: "价格不能为空", trigger: "blur" }
    ],
    applicablePerson: [
      { required: true, message: "适用人群不能为空", trigger: "blur" }
    ],
    info: [
      { required: true, message: "课程介绍不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询课程管理列表 */
function getList() {
  loading.value = true
  listCourse(queryParams.value).then(response => {
    courseList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    code: null,
    subject: null,
    name: null,
    price: null,
    applicablePerson: null,
    info: null,
    createTime: null,
    updateTime: null
  }
  proxy.resetForm("courseRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加课程管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getCourse(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改课程管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["courseRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCourse(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addCourse(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除课程管理编号为"' + _ids + '"的数据项?').then(function() {
    return delCourse(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('course/course/export', {
    ...queryParams.value
  }, `course_${new Date().getTime()}.xlsx`)
}

getList()
</script>

course.js

import request from '@/utils/request'

// 查询课程管理列表
export function listCourse(query) {
  return request({
    url: '/course/course/list',
    method: 'get',
    params: query
  })
}

// 查询课程管理详细
export function getCourse(id) {
  return request({
    url: '/course/course/' + id,
    method: 'get'
  })
}

// 新增课程管理
export function addCourse(data) {
  return request({
    url: '/course/course',
    method: 'post',
    data: data
  })
}

// 修改课程管理
export function updateCourse(data) {
  return request({
    url: '/course/course',
    method: 'put',
    data: data
  })
}

// 删除课程管理
export function delCourse(id) {
  return request({
    url: '/course/course/' + id,
    method: 'delete'
  })
}

三、前端执行流程

根据提供的 Vue 组件代码和 API 请求代码,我们可以分析出前端在执行过程中的主要步骤和逻辑。以下是详细的执行过程分析:

1. 组件初始化

  1. 模板渲染

    • 组件模板中定义了一个课程管理的界面,包括搜索表单、操作按钮、课程列表表格、分页组件以及添加/修改课程的对话框。
    • 使用了 Element Plus 组件库(如 el-formel-tableel-dialog 等)。
  2. 脚本初始化

    • 引入了课程管理的 API 请求函数(listCoursegetCoursedelCourseaddCourseupdateCourse)。
    • 定义了组件的响应式数据,包括课程列表(courseList)、查询参数(queryParams)、表单数据(form)、对话框状态(open)等。
    • 从字典中获取课程学科数据(course_subject)。
  3. 初始化请求

    • 调用 getList() 方法获取课程列表数据,渲染到表格中。

2. 查询操作

  1. 搜索表单

    • 用户可以在搜索表单中输入课程编码、选择课程学科、输入课程名称和适用人群。
    • 点击“搜索”按钮时,触发 handleQuery() 方法。
    • handleQuery() 方法将 pageNum 重置为 1,然后调用 getList() 方法。
  2. 重置表单

    • 点击“重置”按钮时,触发 resetQuery() 方法。
    • resetQuery() 方法重置查询表单,然后调用 handleQuery() 方法重新获取数据。

3. 列表操作

  1. 表格数据

    • 课程列表数据通过 el-table 渲染,每行数据包括课程 ID、编码、学科、名称、价格、适用人群和介绍。
    • 表格支持多选,选中数据后更新 idssinglemultiple 状态。
  2. 操作按钮

    • 新增:点击“新增”按钮时,触发 handleAdd() 方法,重置表单并打开对话框。
    • 修改:点击“修改”按钮时,触发 handleUpdate() 方法,获取课程详情并打开对话框。
    • 删除:点击“删除”按钮时,触发 handleDelete() 方法,弹出确认对话框,确认后调用 delCourse() 删除课程。
    • 导出:点击“导出”按钮时,触发 handleExport() 方法,调用 proxy.download() 导出课程数据。

4. 对话框操作

  1. 添加/修改课程

    • 对话框中包含课程表单,用户可以输入课程编码、选择学科、输入名称、价格、适用人群和介绍。
    • 点击“确定”按钮时,触发 submitForm() 方法。
    • submitForm() 方法验证表单数据,根据是否有 id 决定是新增还是修改课程,调用 addCourse()updateCourse() 方法。
    • 操作成功后,关闭对话框并重新获取课程列表。
  2. 取消操作

    • 点击“取消”按钮时,触发 cancel() 方法,关闭对话框并重置表单。

5. API 请求

  1. 请求函数
    • listCourse(query):发送 GET 请求,获取课程列表数据。
    • getCourse(id):发送 GET 请求,获取单个课程的详细信息。
    • addCourse(data):发送 POST 请求,新增课程。
    • updateCourse(data):发送 PUT 请求,修改课程。
    • delCourse(id):发送 DELETE 请求,删除课程。

6. 执行流程总结

  1. 组件初始化时,调用 getList() 获取课程列表。
  2. 用户通过搜索表单或分页组件触发查询操作,调用 getList() 更新数据。
  3. 用户点击操作按钮(新增、修改、删除、导出)时,触发相应的方法。
  4. 对话框中的表单提交时,调用 submitForm() 方法,根据操作类型调用对应的 API 请求。
  5. API 请求成功后,更新界面数据(如关闭对话框、重新获取列表等)。

关键点

  • 响应式数据:使用 refreactive 定义响应式数据,确保界面实时更新。
  • API 请求:通过 request 工具函数封装 HTTP 请求,简化代码。
  • 表单验证:使用 el-form 的验证规则确保用户输入的有效性。
  • 组件通信:通过 propsemit 实现父子组件通信(如 right-toolbar 组件)。

通过以上分析,可以清晰地了解该前端组件的执行过程和逻辑。

四、课程管理模块后端代码截图

在这里插入图片描述

五、后端代码块

CourseController

package com.ruoyi.course.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.Course;
import com.ruoyi.course.service.ICourseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程管理Controller
 * 
 * @author ruoyi
 * @date 2025-05-27
 */
@RestController
@RequestMapping("/course/course")
public class CourseController extends BaseController
{
    @Autowired
    private ICourseService courseService;

    /**
     * 查询课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(Course course)
    {
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        return getDataTable(list);
    }

    /**
     * 导出课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:export')")
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Course course)
    {
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        util.exportExcel(response, list, "课程管理数据");
    }

    /**
     * 获取课程管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(courseService.selectCourseById(id));
    }

    /**
     * 新增课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course:add')")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Course course)
    {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Course course)
    {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course:remove')")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(courseService.deleteCourseByIds(ids));
    }
}

ICourseService

package com.ruoyi.course.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.Course;
import com.ruoyi.course.service.ICourseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程管理Controller
 * 
 * @author ruoyi
 * @date 2025-05-27
 */
@RestController
@RequestMapping("/course/course")
public class CourseController extends BaseController
{
    @Autowired
    private ICourseService courseService;

    /**
     * 查询课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(Course course)
    {
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        return getDataTable(list);
    }

    /**
     * 导出课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:export')")
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Course course)
    {
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        util.exportExcel(response, list, "课程管理数据");
    }

    /**
     * 获取课程管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(courseService.selectCourseById(id));
    }

    /**
     * 新增课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course:add')")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Course course)
    {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Course course)
    {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course:remove')")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(courseService.deleteCourseByIds(ids));
    }
}

CourseMapper

package com.ruoyi.course.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.Course;
import com.ruoyi.course.service.ICourseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程管理Controller
 * 
 * @author ruoyi
 * @date 2025-05-27
 */
@RestController
@RequestMapping("/course/course")
public class CourseController extends BaseController
{
    @Autowired
    private ICourseService courseService;

    /**
     * 查询课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(Course course)
    {
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        return getDataTable(list);
    }

    /**
     * 导出课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:export')")
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Course course)
    {
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        util.exportExcel(response, list, "课程管理数据");
    }

    /**
     * 获取课程管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(courseService.selectCourseById(id));
    }

    /**
     * 新增课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course:add')")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Course course)
    {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Course course)
    {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course:remove')")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(courseService.deleteCourseByIds(ids));
    }
}

Course

package com.ruoyi.course.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.Course;
import com.ruoyi.course.service.ICourseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程管理Controller
 * 
 * @author ruoyi
 * @date 2025-05-27
 */
@RestController
@RequestMapping("/course/course")
public class CourseController extends BaseController
{
    @Autowired
    private ICourseService courseService;

    /**
     * 查询课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(Course course)
    {
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        return getDataTable(list);
    }

    /**
     * 导出课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('course:course:export')")
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Course course)
    {
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        util.exportExcel(response, list, "课程管理数据");
    }

    /**
     * 获取课程管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(courseService.selectCourseById(id));
    }

    /**
     * 新增课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course:add')")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Course course)
    {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Course course)
    {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程管理
     */
    @PreAuthorize("@ss.hasPermi('course:course:remove')")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(courseService.deleteCourseByIds(ids));
    }
}

六、后端执行逻辑

在这个项目结构中,course 包下的四个绿色类文件(CourseControllerCourseCourseMapperICourseService)通常在若依(RuoYi)框架中扮演不同的角色,它们之间的调用关系如下:

1. Course

  • 角色:领域模型(Domain Model)或实体类。
  • 作用:表示课程(Course)的基本信息,通常与数据库中的 course 表对应。它包含了课程的属性,如课程名称、描述、创建时间等。
  • 调用关系:不直接调用其他类,而是被其他类(如 CourseMapperCourseService)使用。

2. CourseMapper 接口

  • 角色:数据访问层(DAO)。
  • 作用:定义了与数据库交互的方法,如插入、查询、更新和删除课程信息。通常使用 MyBatis 框架,通过 XML 映射文件或注解实现 SQL 语句。
  • 调用关系
    • CourseService 调用,用于执行数据库操作。
    • 依赖于 Course 类,因为它的方法通常以 Course 对象作为参数或返回值。

3. ICourseService 接口

  • 角色:服务层接口。
  • 作用:定义了与课程相关的业务逻辑方法,如添加课程、查询课程列表等。它是对外暴露的业务接口。
  • 调用关系
    • CourseController 调用,用于处理业务逻辑。
    • 依赖于 CourseMapper,因为业务逻辑可能需要访问数据库。

4. CourseController

  • 角色:控制层(Controller)。
  • 作用:处理 HTTP 请求,接收前端传来的参数,调用 CourseService 的方法执行业务逻辑,并返回响应结果。
  • 调用关系
    • 调用 ICourseService 的方法,执行业务逻辑。
    • 依赖于 Course 类,因为请求参数和响应数据通常以 Course 对象的形式传递。

调用关系总结

  1. 前端请求CourseController
  2. CourseController → 调用 ICourseService 的方法。
  3. ICourseService 的实现类 → 调用 CourseMapper 的方法。
  4. CourseMapper → 执行数据库操作,操作 Course 对象。
  5. 最终结果通过 CourseController 返回给前端。

示例流程

假设有一个添加课程的请求:

  1. 前端发送添加课程的请求到 CourseController
  2. CourseController 调用 ICourseService.addCourse() 方法。
  3. CourseServiceImplICourseService 的实现类)调用 CourseMapper.insertCourse() 方法。
  4. CourseMapper 执行 SQL 插入语句,将课程信息存入数据库。
  5. 返回操作结果,最终由 CourseController 返回给前端。

这种分层架构(Controller-Service-Mapper-Domain)是典型的 Web 应用设计模式,有助于代码的模块化和可维护性。

七、前后端交互执行流程

我们可以详细讲解整个前后端交互的过程与步骤。以下是完整的交互流程:


1. 初始化阶段

前端操作
  1. 组件加载
    • Vue 组件初始化时,调用 getList() 方法。
    • getList() 方法调用 listCourse(queryParams) API 请求,向后端发送 GET 请求,携带查询参数(如 pageNumpageSizecode 等)。
后端处理
  1. 接收请求
    • 后端 CourseController 接收 /course/course/list 的 GET 请求。
  2. 业务逻辑
    • 调用 ICourseService.listCourse(queryParams) 方法,查询数据库获取课程列表。
  3. 返回数据
    • 返回查询结果,格式通常为 { rows: [...], total: number }
前端响应
  1. 数据渲染
    • 前端接收后端返回的数据,更新 courseListtotal,渲染到表格和分页组件。

2. 查询操作

前端操作
  1. 用户输入查询条件
    • 用户在搜索表单中输入课程编码、选择学科等。
  2. 触发查询
    • 点击“搜索”按钮,调用 handleQuery() 方法。
    • handleQuery() 重置 pageNum 为 1,并调用 getList()
后端处理
  1. 接收请求
    • 后端接收新的查询参数,重复初始化阶段的查询逻辑。
  2. 返回数据
    • 返回符合查询条件的课程列表。
前端响应
  1. 更新界面
    • 前端更新 courseListtotal,表格重新渲染。

3. 新增课程

前端操作
  1. 打开对话框
    • 点击“新增”按钮,调用 handleAdd() 方法,打开对话框并重置表单。
  2. 提交表单
    • 用户填写课程信息,点击“确定”按钮,调用 submitForm() 方法。
    • submitForm() 验证表单数据,调用 addCourse(form) API 请求,向后端发送 POST 请求,携带课程数据。
后端处理
  1. 接收请求
    • 后端 CourseController 接收 /course/course 的 POST 请求。
  2. 业务逻辑
    • 调用 ICourseService.addCourse(course) 方法,将课程数据插入数据库。
  3. 返回结果
    • 返回操作结果(如成功或失败信息)。
前端响应
  1. 成功处理
    • 前端接收成功响应,关闭对话框,调用 getList() 重新获取课程列表。
  2. 失败处理
    • 显示错误提示(如 proxy.$modal.msgError("新增失败"))。

4. 修改课程

前端操作
  1. 打开对话框
    • 点击“修改”按钮,调用 handleUpdate(row) 方法。
    • 调用 getCourse(id) API 请求,获取课程详情。
  2. 填充表单
    • 后端返回课程详情,填充到表单中。
  3. 提交表单
    • 用户修改信息后点击“确定”,调用 submitForm() 方法。
    • submitForm() 验证数据,调用 updateCourse(form) API 请求,向后端发送 PUT 请求,携带更新后的课程数据。
后端处理
  1. 接收请求
    • 后端 CourseController 接收 /course/course 的 PUT 请求。
  2. 业务逻辑
    • 调用 ICourseService.updateCourse(course) 方法,更新数据库中的课程数据。
  3. 返回结果
    • 返回操作结果。
前端响应
  1. 成功处理
    • 前端接收成功响应,关闭对话框,调用 getList() 重新获取课程列表。
  2. 失败处理
    • 显示错误提示。

5. 删除课程

前端操作
  1. 触发删除
    • 点击“删除”按钮,调用 handleDelete(row) 方法。
    • 弹出确认对话框,用户确认后调用 delCourse(ids) API 请求,向后端发送 DELETE 请求,携带课程 ID。
后端处理
  1. 接收请求
    • 后端 CourseController 接收 /course/course/{id} 的 DELETE 请求。
  2. 业务逻辑
    • 调用 ICourseService.deleteCourseById(id) 方法,从数据库中删除课程数据。
  3. 返回结果
    • 返回操作结果。
前端响应
  1. 成功处理
    • 前端接收成功响应,调用 getList() 重新获取课程列表。
  2. 失败处理
    • 显示错误提示。

6. 导出操作

前端操作
  1. 触发导出
    • 点击“导出”按钮,调用 handleExport() 方法。
    • 调用 proxy.download() 方法,向后端发送导出请求,携带查询参数。
后端处理
  1. 接收请求
    • 后端 CourseController 接收 /course/course/export 的请求。
  2. 业务逻辑
    • 调用 ICourseService.exportCourse(queryParams) 方法,生成 Excel 文件。
  3. 返回文件
    • 返回文件流,前端下载。
前端响应
  1. 下载文件
    • 前端接收文件流,触发浏览器下载。

7. 总结:前后端交互流程

  1. 前端发起请求
    • 用户操作(如查询、新增、修改、删除、导出)触发前端 API 请求。
  2. 后端处理请求
    • 后端 Controller 接收请求,调用 Service 层处理业务逻辑,操作数据库。
  3. 后端返回响应
    • 返回 JSON 数据或文件流。
  4. 前端处理响应
    • 成功时更新界面数据或触发下载。
    • 失败时显示错误提示。

关键点

  • API 设计:前后端通过 RESTful API 交互,明确请求路径、方法和参数。
  • 数据格式:通常使用 JSON 格式传输数据,导出时使用文件流。
  • 错误处理:前后端均需处理异常情况(如网络错误、数据验证失败)。
  • 用户体验:前端通过加载状态(如 v-loading)和提示信息(如 msgSuccess)优化用户体验。

通过以上流程,前后端协同工作,实现了课程管理的完整功能。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2401088.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

智能升级:中国新能源汽车充电桩规模化建设与充电桩智慧管理方案

近年来&#xff0c;中国新能源汽车产业快速发展&#xff0c;市场规模持续扩大&#xff0c;但充电基础设施的建设与管理仍面临布局不均、利用率低、智能化水平不足等问题。为推动新能源汽车普及&#xff0c;国家正加速充电桩的规模化建设&#xff0c;并通过智慧化管理提升运营效…

接口自动化测试之pytest接口关联框架封装

&#x1f345; 点击文末小卡片&#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 一般情况下&#xff0c;我们是通过一个yaml文件进行关联实现 在根目录下新建一个文件yaml&#xff0c;通过上述conftest.py文件实现全局变量的更新: 1.首先需要建…

M1安装并使用Matlab2024a进行java相机标定

安装 Matlab下载地址&#xff1a;https://www.macxin.com/archives/23771.html注意⚠️&#xff1a;如若需要java调用Matlab函数&#xff0c;则需要java版本为21 使用 安装完成之后运行此节目可以看到&#xff1a; 构建jar 命令行输入deploytool&#xff0c;会有一个弹窗&a…

02-Redis常见命令

02-Redis常见命令 Redis数据结构介绍 Redis是一个key-value的数据库&#xff0c;key一般是String类型&#xff0c;不过value的类型多种多样&#xff1a; 贴心小建议&#xff1a;命令不要死记&#xff0c;学会查询就好啦 Redis为了方便学习&#xff0c;将操作不同数据类型的命…

华为OD机试真题——告警抑制(2025A卷:100分)Java/python/JavaScript/C/C++/GO最佳实现

2025 A卷 100分 题型 本专栏内全部题目均提供Java、python、JavaScript、C、C++、GO六种语言的最佳实现方式; 并且每种语言均涵盖详细的问题分析、解题思路、代码实现、代码详解、3个测试用例以及综合分析; 本文收录于专栏:《2025华为OD真题目录+全流程解析+备考攻略+经验分…

Java转Go日记(五十七):gin 中间件

1. 全局中间件 所有请求都经过此中间件 package mainimport ("fmt""time""github.com/gin-gonic/gin" )// 定义中间 func MiddleWare() gin.HandlerFunc {return func(c *gin.Context) {t : time.Now()fmt.Println("中间件开始执行了&quo…

嵌入式学习笔记 - freeRTOS的两种临界禁止

一 禁止中断 通过函数taskENTER_CRITICAL() &#xff0c;taskEXIT_CRITICAL()实现 更改就绪列表时&#xff0c;通常是通过禁止中断的方式&#xff0c;进入临界段&#xff0c;因为systick中断中有可以更改就绪列表的权利&#xff0c; 就绪列表&#xff08;如 pxReadyTasksLis…

202403-02-相似度计算 csp认证

其实这个问题就是求两篇文章的词汇的交集和并集&#xff0c;首先一说到并集&#xff0c;我就想到了set集合数据结构&#xff0c;set中的元素必须唯一。 STL之set的基本使用–博客参考 所以将两个文章的词汇全部加入set中&#xff0c;并求出set的大小&#xff0c;即为并集的大小…

【Oracle】游标

个人主页&#xff1a;Guiat 归属专栏&#xff1a;Oracle 文章目录 1. 游标基础概述1.1 游标的概念与作用1.2 游标的生命周期1.3 游标的分类 2. 显式游标2.1 显式游标的基本语法2.1.1 声明游标2.1.2 带参数的游标 2.2 游标的基本操作2.2.1 完整的游标操作示例 2.3 游标属性2.3.1…

<4>, Qt窗口

目录 一&#xff0c;菜单栏 二&#xff0c;工具栏 三&#xff0c;状态栏 四&#xff0c;浮动窗口 五&#xff0c;对话框 一&#xff0c;菜单栏 MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow) {ui->setupUi(this);// 创建菜单栏…

6.04打卡

浙大疏锦行 DAY 43 复习日 作业&#xff1a; kaggle找到一个图像数据集&#xff0c;用cnn网络进行训练并且用grad-cam做可视化 进阶&#xff1a;并拆分成多个文件 损失: 0.502 | 准确率: 75.53% 训练完成 import torch import torch.nn as nn import torch.optim as optim from…

【基于SpringBoot的图书购买系统】操作Jedis对图书图书的增-删-改:从设计到实战的全栈开发指南

引言 在当今互联网应用开发中&#xff0c;缓存技术已成为提升系统性能和用户体验的关键组件。Redis作为一款高性能的键值存储数据库&#xff0c;以其丰富的数据结构、快速的读写能力和灵活的扩展性&#xff0c;被广泛应用于各类系统的缓存层设计。本文将围绕一个基于Redis的图…

Spring Boot微服务架构(十):Docker与K8S部署的区别

Spring Boot微服务在Docker与Kubernetes&#xff08;K8S&#xff09;中的部署存在显著差异&#xff0c;主要体现在技术定位、管理能力、扩展性及适用场景等方面。以下是两者的核心区别及实践对比&#xff1a; 一、技术定位与核心功能 Docker 功能&#xff1a;专注于单节点容器化…

vue3:Table组件动态的字段(列)权限、显示隐藏和左侧固定

效果展示 根据后端接口返回&#xff0c;当前登录用户详情中的页面中el-table组件的显示隐藏等功能。根据菜单id查询该菜单下能后显示的列。 后端返回的数据类型: 接收到后端返回的数据后处理数据结构. Table组件文件 <!-- 自己封装的Table组件文件 --> onMounted(()>…

pikachu靶场通关笔记13 XSS关卡09-XSS之href输出

目录 一、href 1、常见取值类型 2、使用示例 3、安全风险 二、源码分析 1、进入靶场 2、代码审计 3、渗透思路 三、渗透实战 1、注入payload1 2、注入payload2 3、注入payload3 本系列为通过《pikachu靶场通关笔记》的XSS关卡(共10关&#xff09;渗透集合&#xff…

MCP客户端Client开发流程

1. uv工具入门使用指南 1.1 uv入门介绍 MCP开发要求借助uv进行虚拟环境创建和依赖管理。 uv 是一个Python 依赖管理工具&#xff0c;类似于pip 和 conda &#xff0c;但它更快、更高效&#xff0c;并且可以更好地管理 Python 虚拟环境和依赖项。它的核心目标是 替代 pip 、…

学习日记-day21-6.3

完成目标&#xff1a; 目录 知识点&#xff1a; 1.集合_哈希表存储过程说明 2.集合_哈希表源码查看 3.集合_哈希表无索引&哈希表有序无序详解 4.集合_TreeSet和TreeMap 5.集合_Hashtable和Vector&Vector源码分析 6.集合_Properties属性集 7.集合_集合嵌套 8.…

C语言探索之旅:深入理解结构体的奥秘

目录 引言 一、什么是结构体&#xff1f; 二、结构体类型的声明和初始化 1、结构体的声明 2、结构体的初始化 3、结构体的特殊声明 4、结构体的自引用 5、结构体的重命名 三、结构体的内存对齐 1、对齐规则 2、为什么存在内存对齐&#xff1f; 3、修改默认对齐数 三…

经典算法回顾之最小生成树

最小生成树&#xff08;Minimum Spanning Tree&#xff0c;简称MST&#xff09;是图论中的一个重要概念&#xff0c;主要用于解决加权无向图中连接所有顶点且总权重最小的树结构问题。本文对两种经典的算法即Prim算法和Kruskal算法进行回顾&#xff0c;并对后者的正确性给出简单…

Ubuntu下实现nginx反向代理

1. 多个ngx实例安装 脚本已经在deepseek的指导下完成啦&#xff01; deepseek写的脚本支持ubuntu/centos两种系统。 ins_prefix"/usr/local/" makefile_gen() {ngx$1 ngx_log_dir"/var/log/"$ngx"/"ngx_temp_path"/var/temp/"${ngx}…