Springboot+vue创建单页面应用

简介: Springboot+vue创建单页面应用

介绍

本篇介绍以简单的页面进行做单表的CRUD操作,目的是为了掌握CRUD的基本操作,简单掌握前端vue,axios进行请求后端进行操作。

实体表dict


-- test.dict definition

CREATE TABLE `dict` (

 `dict_id` int NOT NULL AUTO_INCREMENT COMMENT '主键',

 `dict_type` int NOT NULL COMMENT '字典类别',

 `dict_code` varchar(10) NOT NULL COMMENT '代码值',

 `dict_name` varchar(100) NOT NULL COMMENT '代码名称',

 `dict_parent` int DEFAULT NULL COMMENT '父节点',

 `dict_status` varchar(10) DEFAULT NULL COMMENT '字典状态',

 `create_by` varchar(100) DEFAULT NULL COMMENT '创建人',

 `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',

 `update_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '更新人',

 `update_by` varchar(20) DEFAULT NULL COMMENT '创建人',

 PRIMARY KEY (`dict_id`),

 KEY `dict_dict_code_IDX` (`dict_code`,`dict_type`) USING BTREE

) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='字典表';

controller类

package com.elite.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.elite.springboot.common.R;

import com.elite.springboot.entity.Dict;

import com.elite.springboot.entity.Order;

import com.elite.springboot.service.IDictService;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.StringUtils;

import org.springframework.web.bind.annotation.*;

/**

* <p>

* 字典表 前端控制器

* </p>

*

* @author elite

* @since 2022-08-24

*/

@Api(tags = "字典接口")

@RestController

@RequestMapping("/springboot/dict")

public class DictController {

   @Autowired

   IDictService dictService;

   /**

    * 获取字段列表

    */

   @ApiOperation(value = "获取字段列表")

   @PostMapping("/getDictListByPage/{currentPage}/{PageSize}")

   public R getOrderListPage(@PathVariable("currentPage")Integer currentPage,

                             @PathVariable("PageSize") Integer PageSize,

                             @RequestBody Dict dict){

       QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();

       IPage<Dict> page = new Page<>();

       page.setCurrent(currentPage);

       page.setSize(PageSize);

      if (!StringUtils.isEmpty(dict.getDictType())){

          queryWrapper.eq("dict_type",dict.getDictType());

      }

       if (!StringUtils.isEmpty(dict.getDictCode())){

           queryWrapper.eq("dict_code",dict.getDictCode());

       }

       IPage<Dict> Orders = dictService.page(page, queryWrapper);

       return R.ok(Orders);

   }

   /**

    * 根据字典ID获取字典

    */

   @PostMapping("/getDictByDictId/{dict_id}")

   @ApiOperation(value = "获取字段列表")

   public R getDictByDictId(@PathVariable("dict_id") Integer dict_id){

       Dict dict = dictService.getById(dict_id);

       return R.ok(dict);

   }

   /**

    * 新增字典

    */

   @PostMapping("/saveDict")

   @ApiOperation(value = "新增字典")

   public R saveDict(@RequestBody Dict dict){

       boolean save = dictService.save(dict);

       if (save){

        return R.ok(null);

       }

       return R.fail();

   }

   /**

    * 更新字典

    */

   @ApiOperation(value = "更新字典")

   @PutMapping("/updateDictById")

   public R updateDictById(@RequestBody Dict dict){

       boolean success = dictService.updateById(dict);

       if (success){

           return R.ok(null);

       }

       return R.fail();

   }

   /**

    * 删除字典

    */

   @ApiOperation(value = "删除字典")

   @DeleteMapping("/removeDictById/{dict_id}")

   public R removeDictById(@PathVariable("dict_id") Integer dict_id){

       boolean success = dictService.removeById(dict_id);

       //删除成功

       if (success){

           return R.ok(null);

       }

       return R.fail();

   }

}

service层

字典接口服务类

/**

* <p>

* 字典表 服务类

* </p>

*

* @author elite

* @since 2022-08-24

*/

public interface IDictService extends IService<Dict> {

}

字典接口服务实现类

/**

* <p>

* 字典表 服务实现类

* </p>

*

* @author elite

* @since 2022-08-24

*/

@Service

public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

}

mapper类

/**

* <p>

* 字典表 Mapper 接口

* </p>

*

* @author elite

* @since 2022-08-24

*/

public interface DictMapper extends BaseMapper<Dict> {

}

前端页面

前端主要制作的单页面实现CRDU,我们需要在静态文件index中引入vue.js,axios.js.

index.html

<!DOCTYPE html>

<html lang="en">

<head>

   <meta charset="UTF-8">

   <title>字典管理</title>

</head>

<!--script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script-->

<!--引入静态的路径-->

<script type="text/javascript" src="js/vue.min.js"></script>

<!--script src="https://unpkg.com/element-ui/lib/index.js"></script-->

<script type="text/javascript" src="js/index.js"></script>

<!-- 引入样式 -->

<!--link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"-->

<link rel="stylesheet" href="css/index.css">

<!-- 引入组件库 -->

<!--引入通信框架-->

<!--script src="https://unpkg.com/axios/dist/axios.min.js"></script-->

<script type="text/javascript" src="js/axios.min.js"></script>

<body>

<div id="app">

   <h1 style="background-color: #409EFF;text-align: center;">字典管理</h1>

   <div>

       <el-form :inline="true" class="demo-form-inline">

           <el-form-item label="字典类别">

               <el-select v-model="dictType" clearable  placeholder="选择字典类型">

                   <el-option  v-for="item in dicTypeList"

                               :key="item.dictId"

                               :label="item.dictName"

                               :value="item.dictType">

                   </el-option>

               </el-select>

               <el-form-item label="字典码值">

                   <el-input v-model="dictCode"></el-input>

               </el-form-item>

           </el-form-item>

           <el-form-item>

               <el-button type="primary"  @click="btnQuery">查询</el-button>

               <el-button type="primary" @click="btnAdd">添加</el-button>

      </el-form-item>

       </el-form>

       <!--数据表格--->

       <template>

           <el-table

                   ref="multipleTable"

                   :data="dicList"

                   size="mini"

                   border

                   @selection-change="handleSelectionChange">

               style="width: 100%">

               <el-table-column

                       type="selection"

                       width="55">

               </el-table-column>

               <el-table-column

                       prop="dictId"

                       label="字典ID"

                       width="120">

               </el-table-column>

               <el-table-column

                       prop="dictType"

                       label="字典类别"

                       width="150">

               </el-table-column>

               <el-table-column

                       prop="dictCode"

                       label="字典码值"

                       width="100">

               </el-table-column>

               <el-table-column

                       prop="dictName"

                       label="字典码值名称"

                       width="120">

               </el-table-column>

               <el-table-column

                       label="状态"

                       width="100">

                   <template slot-scope="scope">

                       <el-switch

                               v-model="scope.row.dictStatus"

                               active-value="1"

                               inactive-value="0"

                               active-color="#13ce66"

                               inactive-color="#ff4949">

                       </el-switch>

                   </template>

               </el-table-column>

               <el-table-column

                       label="操作"

                       width="200px"

               >

                   <template slot-scope="scope">

                       <el-button

                               size="mini"

                               type="primary"

                               @click="handleEdit(scope.$index, scope.row)">修改</el-button>

                       <el-button

                               size="mini"

                               type="danger"

                               @click="handleDelete(scope.$index, scope.row)">删除</el-button>

                   </template>

               </el-table-column>

           </el-table>

           <el-pagination

                   @size-change="handleSizeChange"

                   @current-change="handleCurrentChange"

                   :current-page="currentPage"

                   :page-sizes="[10, 20, 50, 100]"

                   :page-size="pageSize"

                   layout="total, sizes, prev, pager, next, jumper"

                   :total="total">

           </el-pagination>

       </template>

       <!--添加对象-->

       <el-dialog

               title="添加"

               :visible.sync="centerDialogVisible"

               width="30%"

               center>

           <el-form ref="SysDicData" :model="SysDicData" label-width="80px">

               <el-form-item label="字典类别">

                   <el-select v-model="SysDicData.dictType"  clearable placeholder="请选择字典类别">

                       <el-option v-for="item in dicTypeList"  :key="item.dictId"

                                  :label="item.dictName"

                                  :value="item.dictType"></el-option>

                   </el-select>

               </el-form-item>

               <el-form-item label="字典码值">

                   <el-input v-model="SysDicData.dictCode"></el-input>

               </el-form-item>

               <el-form-item label="字典名称">

                   <el-input v-model="SysDicData.dictName"></el-input>

               </el-form-item>

               <el-form-item label="字典状态">

                   <el-input v-model="SysDicData.dictStatus"></el-input>

               </el-form-item>

               <el-form-item label="父级字典">

                   <el-input v-model="SysDicData.dictParent"></el-input>

               </el-form-item>

           </el-form>

           <span slot="footer" class="dialog-footer">

                   <el-button @click="Cancel">取 消</el-button>

                   <el-button type="primary" @click="submit">确 定</el-button>

               </span>

       </el-dialog>

   </div>

</div>

</body>

</html>

<script>

   new Vue({

       el: "#app",

       //数据

       data() {

           return {

               dicTypeList:[

                   {

                       "dictId":1,

                       "dictName":"性别",

                       "dictType":"1"

                   },

                   {

                       "dictId":2,

                       "dictName":"民族",

                       "dictType":"2"

                   },

                   {

                       "dictId":3,

                       "dictName":"婚姻",

                       "dictType":"3"

                   }

               ],

               dicList:[],

               options:[],

               dictType:"", //字典类型

               dictCode:"", //字典码值

               centerDialogVisible:false,

               SysDicData:{},

               diagTitle:"",

               formLabelWidth: '120px',

               multipleSelection: [],

               currentPage:1,

               pageSize:10,

               total:0,

               dialogImportVisible:false

           }

       },

       //创建之前

       created() {

       },

       //初始化加载数据

       mounted() {

           //获取字典列表

           this.getDicList(this.currentPage,this.pageSize,{"dictType":"","dictCode":""})

       },

       methods: {

           //页数大小改变

           handleSizeChange(pagesize){

               this.pageSize = pagesize

               this.getDicList(this.currentPage,this.pageSize,{"dictType":""})

           },

           //处理当前页变化

           handleCurrentChange(CurrentChange){

               this.currentPage = CurrentChange

               this.getDicList(this.currentPage,this.pageSize,{"dictType":""})

           },

           //多选

           handleSelectionChange(selection){

              this.multipleSelection = selection;

           },

           //编辑数据

           handleEdit(index, row) {

               //数据赋值

               this.SysDicData = row;

               this.centerDialogVisible = true

               this.diagTitle = "修改字典"

           },

           //删除数据

           handleDelete(index, row) {

               this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {

                   confirmButtonText: '确定',

                   cancelButtonText: '取消',

                   type: 'warning'

               }).then(() => {

                   //删除字典

                   axios.delete("/springboot/dict/removeDictById/"+row.dictId).then((res => {

                       this.$notify({

                           title: res.data.code === 200  ? "修改成功" : "修改失败",

                           message: res.data.msg,

                           type: res.data.code === 200  ? "success" : "error"

                       });

                       //绑定表格数据

                       if (res.data.code === 200) {

                           //调用获取列表

                           this.btnQuery();

                       }

                   }));

               }).then((response) => {

                   this.getDictList()

                   this.$message.success("删除成功")

               }).catch(error => {

                   this.$message.info('取消删除')

               })

           },

           //查询数据

           btnQuery(){

               this.getDicList(this.currentPage,this.pageSize,{"dictType":this.dictType,"dictCode":this.dictCode})

           },

           //添加字典

           btnAdd(){

               this.SysDicData = {}

               this.centerDialogVisible = true;

           },

           Cancel(){

               this.centerDialogVisible = false;

               tihs.SysDicData = {}

           },

           //添加字典

           submit(){

               console.log(this.SysDicData)

               //获取字典ID

               if(this.SysDicData.dictId){

                   //修改

                   axios.put("/springboot/dict/updateDictById", this.SysDicData).then((res => {

                       this.$notify({

                           title: res.data.code === 200  ? "修改成功" : "修改失败",

                           message: res.data.msg,

                           type: res.data.code === 200  ? "success" : "error"

                       });

                       //绑定表格数据

                       if (res.data.code === 200) {

                           this.centerDialogVisible = false

                           this.SysDicData = {}

                           //调用获取列表

                           this.btnQuery();

                       }else{

                       }

                   }));

               }else{

                   //新增

                   axios.post("/springboot/dict/saveDict",this.SysDicData).then((res => {

                   this.$notify({

                           title: res.data.code === 200  ? "新增成功" : "新增失败",

                           message: res.data.msg,

                           type: res.data.code === 200  ? "success" : "error"

                       });

                       //绑定表格数据

                       if (res.data.code == 200) {

                           this.centerDialogVisible = false

                            tihs.SysDicData = {}

                            //调用获取列表

                           this.getDicList(this.currentPage,this.pageSize,{"dictType":"","dictCode":""})

                       }

                   }));

               }

           },

           //获取字段数据

           getDicList(currentPage,pageSize,params){

               axios.post("/springboot/dict/getDictListByPage/"+currentPage+"/"+pageSize, params).then((res => {

                   console.log(res.data)

                   this.$notify({

                       title: res.data.code === 200  ? "成功" : "失败",

                       message: res.data.msg,

                       type: res.data.code === 200  ? "success" : "error"

                   });

                   //绑定表格数据

                   if (res.data.code === 200) {

                       this.dicList = res.data.data.records

                       this.total = res.data.data.total

                       this.currentPage = res.data.data.pages

                       this.pageSize= res.data.data.size

                   }

               }));

           }

       }

   });

</script>

<style scoped>

</style>

测试

05939918690a4465bd185995a7e9b494.jpg


相关文章
|
4月前
|
JavaScript 安全 Java
如何使用 Spring Boot 和 Ant Design Pro Vue 实现动态路由和菜单功能,快速搭建前后端分离的应用框架
本文介绍了如何使用 Spring Boot 和 Ant Design Pro Vue 实现动态路由和菜单功能,快速搭建前后端分离的应用框架。首先,确保开发环境已安装必要的工具,然后创建并配置 Spring Boot 项目,包括添加依赖和配置 Spring Security。接着,创建后端 API 和前端项目,配置动态路由和菜单。最后,运行项目并分享实践心得,包括版本兼容性、安全性、性能调优等方面。
256 1
|
24天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue实现的留守儿童爱心网站设计与实现(计算机毕设项目实战+源码+文档)
博主是一位全网粉丝超过100万的CSDN特邀作者、博客专家,专注于Java、Python、PHP等技术领域。提供SpringBoot、Vue、HTML、Uniapp、PHP、Python、NodeJS、爬虫、数据可视化等技术服务,涵盖免费选题、功能设计、开题报告、论文辅导、答辩PPT等。系统采用SpringBoot后端框架和Vue前端框架,确保高效开发与良好用户体验。所有代码由博主亲自开发,并提供全程录音录屏讲解服务,保障学习效果。欢迎点赞、收藏、关注、评论,获取更多精品案例源码。
59 10
|
24天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue实现的家政服务管理平台设计与实现(计算机毕设项目实战+源码+文档)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
43 8
|
24天前
|
JavaScript 搜索推荐 Java
基于SpringBoot+Vue实现的家乡特色推荐系统设计与实现(源码+文档+部署)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
53 8
|
24天前
|
JavaScript NoSQL Java
基于SpringBoot+Vue实现的大学生就业服务平台设计与实现(系统源码+文档+数据库+部署等)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
62 6
|
24天前
|
JavaScript Java 测试技术
基于Java+SpringBoot+Vue实现的车辆充电桩系统设计与实现(系统源码+文档+部署讲解等)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
55 6
|
24天前
|
JavaScript NoSQL Java
基于SpringBoot+Vue的班级综合测评管理系统设计与实现(系统源码+文档+数据库+部署等)
✌免费选题、功能需求设计、任务书、开题报告、中期检查、程序功能实现、论文辅导、论文降重、答辩PPT辅导、会议视频一对一讲解代码等✌
40 4
|
24天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue实现的高校食堂移动预约点餐系统设计与实现(源码+文档+部署)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
66 3
|
24天前
|
JavaScript NoSQL Java
基于SpringBoot+Vue实现的大学生体质测试管理系统设计与实现(系统源码+文档+数据库+部署)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
36 2
|
1月前
|
监控 JavaScript 数据可视化
建筑施工一体化信息管理平台源码,支持微服务架构,采用Java、Spring Cloud、Vue等技术开发。
智慧工地云平台是专为建筑施工领域打造的一体化信息管理平台,利用大数据、云计算、物联网等技术,实现施工区域各系统数据汇总与可视化管理。平台涵盖人员、设备、物料、环境等关键因素的实时监控与数据分析,提供远程指挥、决策支持等功能,提升工作效率,促进产业信息化发展。系统由PC端、APP移动端及项目、监管、数据屏三大平台组成,支持微服务架构,采用Java、Spring Cloud、Vue等技术开发。

热门文章

最新文章