首页 > 其他分享 >springboot+vue前后端分离项目-项目搭建13-树形表

springboot+vue前后端分离项目-项目搭建13-树形表

时间:2024-07-16 17:56:46浏览次数:7  
标签:category 13 vue springboot res Result import message id

效果

一、后端

1. 新建表category

 2.新建entity,com/example/demo/entity/Category.java

3.新建Mapper,com/example/demo/mapper/CategoryMapper.java

 4.新建Controller,com/example/demo/controller/CategoryController.java

主要代码逻辑如下, 封装成List<Category>,前端结合element plus就能显示,这里主要是有一段递归逻辑

package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.Category;
import com.example.demo.mapper.CategoryMapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    //正常Mapper是在Service里引用,Controllerl里引用Service,本案例是为了方便调用,非正规操作
    @Resource
    CategoryMapper categoryMapper;

    @PostMapping
    public Result<?> save(@RequestBody Category category){
        categoryMapper.insert(category);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Category category){
        categoryMapper.updateById(category);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        categoryMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping
    public Result<?> getAll(){
        List<Category> allCategories = categoryMapper.selectList(null);
        return Result.success(loopQuery(null,allCategories));
    }

    private List<Category> loopQuery(Integer pid, List<Category> allCategories) {
        List<Category> categoryList = new ArrayList<>();
        for (Category category : allCategories) {
            if (pid == null){
                if (category.getPid() == null){
                    categoryList.add(category);
                    category.setChildren(loopQuery(category.getId(), allCategories));
                }
            } else if (pid.equals(category.getPid())) {
                categoryList.add(category);
                category.setChildren(loopQuery(category.getId(), allCategories));
            }
        }
        return categoryList;
    }
}

二、前端

1.新建vue/src/views/Category.vue

<template>
  <div style="width: 100%; padding: 10px">
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add()" v-if="user.role === 1">新增</el-button><!--管理员才有新增按钮-->
    </div>
    <el-table v-loading="loading" :data="tableData" border stripe style="width: 100%"
              row-key="id" default-expand-all>
      <el-table-column prop="name" label="名称" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleEdit(scope.row)" v-if="user.role === 1">
            编辑
          </el-button>
          <el-popconfirm title="确认删除吗?" @confirm="handleDelete(scope.row.id)" v-if="user.role === 1">
            <template #reference>
              <el-button link type="danger" size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="dialogVisible" title="分类编辑" width="30%">
      <el-form label-width="auto" :model="form" style="width: 600px">
        <el-form-item label="名称">
          <el-input v-model="form.name" style="width: 80%"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">
            确 定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>

import request from "@/utils/request";

export default {
  name: 'Category',
  components: {

  },
  data() {
    return {
      user: {},
      loading: false,
      form: {},
      dialogVisible: false,
      tableData: []
    }
  },
  created() {
    let userStr = sessionStorage.getItem("user") || {}
    this.user = JSON.parse(userStr)
    // 请求服务器,确认当前登录用户的合法性
    request.get("/user/" + this.user.id).then(res => {
      if (res.data === '0'){
        this.user = res.data
      }
    })
    this.load()
  },
  methods: {
    load() {
      this.loading = true
      request.get("/category").then(res=>{
        this.loading = false
        this.tableData = res.data
      })
    },
    add(){
      this.dialogVisible = true
      this.form = {}
    },
    save(){
      if(this.form.id){  //更新
        request.put("/category", this.form).then(res => {
          console.log(res)
          if(res.code === '0'){
            this.$message({
              type: "success",
              message: "更新成功"
            })
          }else {
            this.$message({
              type: "error",
              message: "res.msg"
            })
          }
          this.load()      //更新后刷新表格数据
          this.dialogVisible = false   //关闭弹窗
        })
      } else {           //新增
        request.post("/category", this.form).then(res => {
          console.log(res)
          if(res.code === '0'){
            this.$message({
              type: "success",
              message: "新增成功"
            })
          }else {
            this.$message({
              type: "error",
              message: "res.msg"
            })
          }
          this.load()      //更新后刷新表格数据
          this.dialogVisible = false   //关闭弹窗
        })
      }
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    handleDelete(id) {
      console.log(id)
      request.delete("/category/" + id).then(res => {
        if(res.code === '0'){
          this.$message({
            type: "success",
            message: "删除成功"
          })
        }else {
          this.$message({
            type: "error",
            message: "res.msg"
          })
        }
        this.load()      //删除后刷新表格数据
      })
    }
  }
}

</script>

2.改造vue/src/components/Aside.vue 和 vue/src/router/index.js

--以上查询、编辑、删除正常

--新增功能暂时没做关联新增pid

标签:category,13,vue,springboot,res,Result,import,message,id
From: https://www.cnblogs.com/xiexieyc/p/18305811

相关文章

  • python+flask计算机毕业设计基于Vue.js的付费阅读小程序(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着互联网技术的飞速发展,数字化阅读已成为现代人获取知识、娱乐休闲的重要方式之一。然而,在海量信息面前,如何有效保护知识产权,激励内容创......
  • P1350 车的放置
    P1350车的放置-洛谷|计算机科学教育新生态(luogu.com.cn)非递推做法,对于这个题,这个图形之间统计很麻烦,由此我们可以把它分成两个矩形。如直接沿\(b\)边切割。但这样我们发现还是不好统计,因为左边矩形会受到右边不一定的限制,于是沿着\(b,c\)边再次切割,分成三个矩形,从上......
  • Vue3项目配置Vue-Router
            在使用 Vue 作为前端开发框架时,我们通常以单页面应用(SPA)的形式进行开发。而单页面应用中,我们通常通过路由跳转的方式来实现我们页面上组件之间的跳转。在本文中,博主将详细介绍在Vue3 项目中,如何进行Vue-Router的安装与配置。在开始安装和配置Vue-Ro......
  • Springboot Study-入门&配置
    SpringbootStudy入门&配置1.入门构建了Springboot工程,创建springboot项目,完成了第一个项目helloworld2.配置2.1配置分类:properties>yml>yaml(优先级)2.2yaml基本语法:大小写敏感,数值前要有空格,空格缩进表示层级关系数据格式:*对象*数组(使用“-”表示数组每个元......
  • vue-echarts/echarts结合flex布局,v-show,charts无法自动计算容器大小自适应
    vue-echarts/echarts结合flex布局,v-show,charts无法自动计算容器大小自适应<template><v-chartref="child"class="chart":autoresize="true":option="option"/></template>问题1:首先设置一个非常简单的echarts或v-charts,注意autores......
  • 基于Springboot的博物馆管理系统
    全文内容包括:1、采用技术;2、系统功能;3、系统截图;4、配套内容。索取方式见文末微信号,欢迎关注收藏!一、采用技术语言:Java1.8框架:SpringBoot数据库:MySQL5.7、8.0开发工具:IntelliJIDEA旗舰版其他:Maven3.8以上二、系统功能用户管理:负责注册用户的信息维护,包括用户账号的创建......
  • SpringBoot-集成 webSocket
    1.WebSocket简介2.springboot集成javax注解方式pom<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency>配置类/*****blog.coder4j.cn*......
  • 在 JeecgBoot 项目中基于 Vue 3 配置多页面入口
    在现代Web开发中,使用Vue框架构建项目已经成为一种常见的选择。而JeecgBoot作为一个优秀的后台管理系统框架,也提供了丰富的功能和组件,方便开发人员快速搭建企业级应用。本文将介绍如何在JeecgBoot项目中基于Vue3配置多页面入口,实现更灵活的页面管理和定制化需求。前提......
  • [2024] springboot Hadoop技术下的校园二手交易系统的设计与实现
    博主介绍:✌CSDN新星计划导师、Java领域优质创作者、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流✌技术范围:SpringBoot、Vue、SSM、HTML、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、小程序、安卓app、大数......
  • Vue3新特性defineOptions和defineModel 面试总结
    在Vue3中,defineOptions和defineModel是两个重要的新特性,它们分别在组件定义和v-model双向绑定方面提供了更为便捷和高效的解决方案。defineOptions定义与用途:defineOptions是Vue3.3+版本中引入的一个宏(macro),用于在<scriptsetup>语法糖中定义组件的选项,如组件名(name)、透传属......