SpringBoot+Vue项目新手快速入门指南

目录
  • 1. 项目技术选型
  • 2.数据库设计
  • 3. 后台搭建
    • 3.1 引入依赖
    • 3.2 swagger配置
    • 3.3实体类
    • 3.4 自动填充配置
    • 3.5 Mapper
    • 3.6 service
    • 3.7 controller
  • 4. 前端搭建
    • 4.1 环境搭建
      • 4.1.1 Node环境
      • 4.1.2 项目构建
      • 4.1.3 安装插件
      • 4.1.4 引入插件
    • 4,2.搭建路由
    • 4.3. echarts使用
    • 4.4 element-ui使用
  • 总结

前言:本人目前从事java开发,但同时也在学习各种前端技术,下面是我做的一个前后端分离项目的一个小案例,不足之处请多多指教

1. 项目技术选型

Springboot+MyBatis-Plus+vue+element-ui+echarts

2.数据库设计

SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `fruit`;
CREATE TABLE `fruit` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL COMMENT '名称',
  `sale` double DEFAULT NULL COMMENT '价格',
  `num` int(11) DEFAULT NULL COMMENT '库存',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `del_flag` tinyint(4) DEFAULT '0' COMMENT '删除标记',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

3. 后台搭建

3.1 引入依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>velocity</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.22</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
       </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        </dependencies>

3.2 swagger配置

@Configuration //什么此类为配置类
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
}

3.3实体类

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@ApiModel
public class Fruit {
    @ApiModelProperty("id")
    private int id;

    @ApiModelProperty("name")
    private String name;

    @ApiModelProperty("sale")
    private double sale;

    @ApiModelProperty("num")
    private int num;

    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    @TableLogic
    private boolean delFlag;
}

3.4 自动填充配置

@Component
public class DateHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime",new Date(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }

    //使用mp实现更新操作,执行此方法
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }
}

3.5 Mapper

@Repository
public interface FruitMapper extends BaseMapper<Fruit> {
}

3.6 service

public interface FruitService extends IService<Fruit> {
    public FruitVO FruitVOList();
}

实现类

@Service
public class FruitServiceImpl extends ServiceImpl<FruitMapper, Fruit> implements FruitService {
    @Autowired
    private FruitMapper fruitMapper;

    @Override
    public FruitVO FruitVOList() {
        List<Fruit> fruits = fruitMapper.selectList(null);
        ArrayList<String> name = new ArrayList<>();
        ArrayList<Integer> num = new ArrayList<>();
        for(Fruit fruit:fruits){
            name.add(fruit.getName());
            num.add(fruit.getNum());
        }
        FruitVO fruitVO = new FruitVO();
        fruitVO.setName(name);
        fruitVO.setNum(num);
        return fruitVO;
    }
}

3.7 controller

@RequestMapping("/fruit")
@RestController
@Api(tags = "水果")
@CrossOrigin
public class FruitController {

    @Autowired
    private FruitService fruitService;

    @GetMapping("/list")
    @ApiOperation("获取水果列表")
    public ResponseData list(){
        List<Fruit> list = fruitService.list();
        return new ResponseData(200,list,"ok");
    }

    @GetMapping("/list1")
    @ApiOperation(("获取水果列表1"))
    public List<Fruit> list1(){
        List<Fruit> list = fruitService.list();
        return list;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(("通过id删除水果信息"))
    public ResponseData deleteFruit(@PathVariable("id") int id){
//        int id=1;
//        System.out.println(name);
            System.out.println(id);
        boolean b = fruitService.removeById(id);
        return new ResponseData().ok(b,"ok");
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("通过id获取水果信息")
    public ResponseData getById(@PathVariable("id") int id){
        Fruit fruit = fruitService.getById(id);
        return new ResponseData().ok(fruit,"查找成功");
    }

    @PutMapping("/update")
    @ApiOperation("添加水果信息")
    public ResponseData update(@RequestBody Fruit fruit){
        boolean b = fruitService.updateById(fruit);
        if(b == true){
            return new ResponseData().ok(fruit,"更新成功");
        }else {
            return new ResponseData(404,fruit,"更新失败");
        }
    }

    @PostMapping("/save")
    @ApiOperation("保存水果信息")
    public ResponseData save(@RequestBody Fruit fruit){
        boolean save = fruitService.save(fruit);
        if(save == true){
            return new ResponseData().ok(fruit,"保存成功");
        }else {
            return new ResponseData().error(fruit,"保存失败");
        }
    }

    @GetMapping("barVo")
    @ApiOperation("获取统计信息")
    public ResponseData barVo(){
        FruitVO fruitVO = fruitService.FruitVOList();
        return new ResponseData().ok(fruitVO,"查找成功");
    }
}

4. 前端搭建

4.1 环境搭建

4.1.1 Node环境

官方下载node

检查安装情况

node –v
npm –v

安装cnpm

npm install –g cnpm --registry=https://registry.npm.taobao.org

安装vue-cli

cnpm install vue-cli -g

4.1.2 项目构建

vue init webpack 项目名称

创建成功后,进入项目根目录,初始化项目并运行

cnpm install
cnpm run dev

4.1.3 安装插件

安装element-ui插件

cnpm install element-ui

安装axios插件

cnpm install axios

安装echarts插件

cnpm install echarts -S

4.1.4 引入插件

在main.js中引入插件

import Vue from 'vue'
import App from './App'
import router from './router'
import echarts from 'echarts'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
Vue.prototype.$axios = axios
Vue.use(ElementUI)
Vue.prototype.$echarts = echarts
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

4,2.搭建路由

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import About from '@/views/About'
import Pie from '@/views/Pie'
import Table from '@/views/Table'
import Edit from '@/views/Edit'
import Add from '@/views/Add'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/about',
      name: 'About',
      component: About
    },
    {
      path: '/pie',
      name: 'Pie',
      component: Pie
    },
    {
      path: '/table',
      name: 'Table',
      component:Table
    },
    {
      path: '/edit',
      name: 'Edit',
      component:Edit
    },
    {
      path: '/add',
      name: 'Add',
      component:Add
    }
  ]
})

4.3. echarts使用

在pages下创建一个名为pie.vue的文件

<template>
  <div>
    <h2>vue中插入Echarts示例</h2>
    <div id="chart_example" :style="{width:'800px',height:'600px'}">
    </div>
  </div>
</template>

<script>
const axios = require('axios');
export default {
    data() {

    },
    mounted(){
        let _this = this;
        axios.get('http://localhost:9001/fruit/barVo').then(function(response) {
            console.log(response.data.data)

        let myCharts = _this.$echarts.init(document.getElementById('chart_example'))
        myCharts.setOption( {
        title:{
            text:'数量统计表',
            top:20
        },
        xAxis: {
            type: 'category',
            data: response.data.data.name
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
            data: response.data.data.num,
            type: 'bar',
            name:'销量',
            showBackground: true,
            backgroundStyle: {
                color: 'rgba(180, 180, 180, 0.2)'
            }
            }
        ]
        })
        })
    }
}
</script>

4.4 element-ui使用

表格的使用

在views下面创建table.vue

<template>
  <el-table
    :data="tableData"
    border
    style="width: 100%">
    <el-table-column
      fixed
      prop="id"
      label="id"
      width="150">
    </el-table-column>
    <el-table-column
      prop="name"
      label="名称"
      width="120">
    </el-table-column>
    <el-table-column
      prop="num"
      label="数量"
      width="120">
    </el-table-column>
    <el-table-column
      prop="sale"
      label="价格"
      width="120">
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作"
      width="100">
      <template slot-scope="scope">
        <el-button @click="fruitDel(scope.row)" type="text" size="small">删除</el-button>
        <el-button @click="getFruitById(scope.row)" type="text" size="small">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
const axios = require('axios');
  export default {
    methods: {
      handleClick(row) {
        console.log(row);
      },
      fruitDel(row){
        alert(row.id);
        axios.delete("http://localhost:9001/fruit/delete/"+row.id)
        location.reload();
      },
      getFruitById(object){
       this.$router.push('/edit?id='+object.id)
      }
    },
    created(){
        let _this=this;
        axios.get("http://localhost:9001/fruit/list")
        .then(response => {
            console.log(response);
            console.log(response.data.data)
            _this.tableData=response.data.data
        })
    },
    data() {
     return{
       tableData:null
     }
    }
  }
</script>

表单的使用

在views下面常见文件Add.vue

<template>
<el-form ref="form" :model="fruit" label-width="80px">
   <el-form-item label="水果名称">
    <el-input v-model="fruit.name"></el-input>
  </el-form-item>
   <el-form-item label="水果数量">
    <el-input v-model="fruit.num"></el-input>
  </el-form-item>
  <el-form-item label="水果售价">
    <el-input v-model="fruit.sale"></el-input>
  </el-form-item>
   <el-form-item>
    <el-button type="primary" @click="onSubmit('fruit')">立即创建</el-button>
    <el-button>取消</el-button>
  </el-form-item>
</el-form>
</template>
<script>
const axios = require('axios');
  export default {
    data() {
      return {
        fruit: {
          id:'',
          name:'',
          num:'',
          sale: ''
        }
      }
    },
    methods: {
        onSubmit(){
           let _this = this;
           axios.post('http://localhost:9001/fruit/save',this.fruit)
           .then(function (response) {
             if(response.data==200) {
                this.$message({
                  message: '保存水果成功',
                  type: 'success'
                });
             }
            _this.$router.push('/table')
           })
        }
    }
  }
</script>

在views项目常见edit.vue

<template>
<el-form ref="form" :model="fruit" label-width="80px">
  <el-form-item label="水果ID">
    <el-input v-model="fruit.id"></el-input>
  </el-form-item>
   <el-form-item label="水果名称">
    <el-input v-model="fruit.name"></el-input>
  </el-form-item>
   <el-form-item label="水果数量">
    <el-input v-model="fruit.num"></el-input>
  </el-form-item>
  <el-form-item label="水售价">
    <el-input v-model="fruit.sale"></el-input>
  </el-form-item>
   <el-form-item>
    <el-button type="primary" @click="onSubmit('fruit')">编辑</el-button>
    <el-button>取消</el-button>
  </el-form-item>
</el-form>
</template>
<script>
const axios = require('axios');
  export default {
    data() {
      return {
        fruit: {
          id:'',
          name:'',
          num:'',
          sale: ''
        }
      }
    },
    created() {
        let id= this.$route.query.id
        let _this=this
        axios.get('http://localhost:9001/fruit/getById/'+id)
        .then(response=>{
            console.log(response)
            _this.fruit=response.data.data
        })
    },
    methods: {
        onSubmit(){
            alert(1)
                   let _this = this
                   axios.put("http://localhost:9001/fruit/update",this.fruit)
                   .then(function (response) {
                       console.log(response)
                       if(response.data.code==200){
                           _this.$alert(_this.fruit.name+'修改成功',"修改数据",{
                               confirmButtonText:'确定',
                               callback:action=>{
                                   _this.$router.push('/table')
                               }
                           })
                       }
                   })
        }
    }
  }
</script>

总结

到此这篇关于SpringBoot+Vue项目新手快速入门指南的文章就介绍到这了,更多相关SpringBoot+Vue项目入门内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Springboot项目与vue项目整合打包的实现方式

    我的环境 * JDK 1.8  * maven 3.6.0  * node环境 1.为什么需要前后端项目开发时分离,部署时合并? 在一些公司,部署实施人员的技术无法和互联网公司的运维团队相比,由于各种不定的环境也无法做到自动构建,容器化部署等.因此在这种情况下尽量减少部署时的服务软件需求,打出的包数量也尽量少.针对这种情况这里采用的在开发中做到前后端独立开发,打包时在后端springboot打包发布时将前端的构建输出一起打入,最后只需部署springboot的项目即可,无需再安装nginx服务器

  • 部署vue+Springboot前后端分离项目的步骤实现

    单页应用 vue经常被用来开发单页应用(SinglePage Web Application,SPA),什么叫做单页应用呢,也就是只有一张web页面的应用,单页应用的跳转只需要刷新局部资源,大大加快的了我们页面的响应速度 前端页面打包 打开vue工程,在项目根目录下创建一个配置文件:vue.config.js,然后在里面写入以下内容: module.exports = { assetsDir: 'static', // 静态资源保存路径 outputDir: 'dist', // 打包后生成的文

  • 一文教会你如何搭建vue+springboot项目

    目录 前言 开发使用的软件 vue项目搭建 环境配置 cmd命令 vue ui 创建项目 vue项目制作方向梳理 通过软件vscode打开项目 vue 配置 vue-router 如何配置 axios ui框架 引入 结构 router配置 request.js get和post请求 vue.config.js vue完成 springboot 创建springboot项目 依赖 项目结构 启动类配置 配置 跨域 数据库配置.服务端口 数据交互 springboot运行顺序 controller

  • springboot+vue制作后台管理系统项目

    目录 一.所使用的环境配置: 二.项目简介 三.知识点总结(代码和配置) SpringBoot: 1.Mybatis-Plus配置文件,实现分页查询:MybatisPlusConfig 2.跨域配置文件:CorsConfig 3.请求返回类!:Result 4.pom.xml配置文件 Vue: 其余知识点总结: 总结: 学习资源来自于B站UP,up他讲的非常详细,对于熟悉两大框架很有用. 我的作业源代码在文章末尾,欢迎有需要的同学,学习参考使用,内置SQL文件,导入后,开启springboot和

  • Spring Boot和Vue前后端分离项目架构的全过程

    目录 Spring Boot+Vue 前后端分离项目架构 1. SpringBoot 后端项目 2. Vue 前端项目 总结 Spring Boot+Vue 前后端分离项目架构 项目流程: 1. SpringBoot 后端项目 1.新建一个 SpringBoot 工程,并添加项目开发过程中需要的相关依赖: 2.数据库新建 book 数据表: -- ---------------------------- -- Table structure for book -- ---------------

  • Java SpringBoot+vue+实战项目详解

    目录 1.<锋迷商城>业务流程设计-接⼝规范 1.1 前后端分离与单体架构流程实现的区别 1.1.1单体架构 1.1.2 前后端分离架构 1.2 接口介绍 1.2.1接口概念 1.2.2接口规范 1.3 Swagger 1.3.1作用 1.3.2 Swagger整合 1.3.3 Swagger注解说明 1.3.4 Swagger-ui 插件 1.4 RESTful 总结 1.<锋迷商城>业务流程设计-接⼝规范 在企业项⽬开发中,当完成项⽬的需求分析.功能分析.数据库分析与设计之后,

  • springboot整合vue项目(小试牛刀)

    序 本文主要研究一下如何在springboot工程整合vue maven <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> 新建springboot的web工程,默认会在resources目录下生成static以及templates文件夹 temp

  • SpringBoot+Vue项目新手快速入门指南

    目录 1. 项目技术选型 2.数据库设计 3. 后台搭建 3.1 引入依赖 3.2 swagger配置 3.3实体类 3.4 自动填充配置 3.5 Mapper 3.6 service 3.7 controller 4. 前端搭建 4.1 环境搭建 4.1.1 Node环境 4.1.2 项目构建 4.1.3 安装插件 4.1.4 引入插件 4,2.搭建路由 4.3. echarts使用 4.4 element-ui使用 总结 前言:本人目前从事java开发,但同时也在学习各种前端技术,下面是我做

  • Spring Boot 快速入门指南

    最近因为项目的缘故,需要接触 Spring Boot,详细的介绍可以参考官方的文档,这里主要根据自己学习的实践进行简单分享.版本:1.3.6 简介 Spring 框架是非常著名的 Java 开源框架,历经十多年的发展,整个生态系统已经非常完善甚至是繁杂,Spring Boot 正是为了解决这个问题而开发的,为 Spring 平台和第三方库提供了开箱即用的设置,只需要很少的配置就可以开始一个 Spring 项目.当然,建议使用 Java 8 来进行开发. Spring Boot 实际上走的是 Se

  • Oracle RMAN快速入门指南

    正在看的ORACLE教程是:Oracle RMAN快速入门指南.前言: 这篇文章主要介绍RMAN的常用方法,其中包含了作者一些自己的经验,里面的实验也基本全在WIN 2K和ORACLE 8.1.6环境下测试成功(因为这个环境比较容易实现). 本文借鉴了网上一些高手的相关文章,希望大侠们不要见怪,此处一并谢过. 这篇文章主要是在北京出差期间写的,回到家后整理修改了一下,时间比较仓促,同时因为篇幅有限,一些技术细节不能一一覆盖了,只希望能够帮助新手入门的作用,想真正熟练掌握RMAN,必须经过较长时间

  • 国产PHP开发框架myqee新手快速入门教程

    一.环境. 笔者的环境是win7 32bit 旗舰版.用的xampp1.7.4(1.8.x版的php版本太高,个人觉得php 5.3X更实用些)+mq最新版.重点是配置虚拟机, 参考了http://www.jb51.net/article/52123.htm 本机xampp安装在D盘,给出我的配置:虚拟机配置文件路径 D:\xampp\apache\conf\extra\httpd-vhosts 复制代码 代码如下: #mq <VirtualHost *:80>  DocumentRoot &

  • SpringBoot + Vue 项目部署上线到Linux 服务器的教程详解

    前言 给大家分享以下我是如何部署 SpringBoot + Vue 前后端分离的项目的,我用的 Linux 发行版是 CentOS7.5 有了一个基于 ElementUI 的电商后台管理系统,在开发一个相似的后台就会轻松很多.不过前面的系统的后端是使用 node 完成的,对于我们 Java 开发者来说,用不到.我学习的是 ElementUI 的使用,就足够了,然后后端服务就全部可以自己使用 SpringBoot 来完成 最近貌似 Vue3 正式版也发布了,正好有空看可以去看一看 提示:以下是本篇

  • springboot vue项目后端列表接口分页模糊查询

    目录 基于 springboot+vue 的测试平台开发 一.分页插件 二.实现接口 1. 编写 Service 层 2. 编写 Controller 层 三.测试接口 1. 测试分页 2. 测试条件查询 基于 springboot+vue 的测试平台开发 继续更新 打开项目管理,就需要看到列表里展示项目数据,比如这样(截图是这个前端框架的demo,仅作示意): 那么对应到我们平台的项目管理功能,就需要有: 列表展示添加的项目数据 可以通过项目名称查询指定的项目 新增项目 编辑项目 其他功能..

  • mybatis新手快速入门以及一些错误汇总

    一.使用maven加载依赖 加载了连接数据库的依赖.mybatis的依赖以及lombok的依赖 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> &

  • vue 虚拟DOM快速入门

    虚拟 DOM 什么是虚拟 dom dom 是文档对象模型,以节点树的形式来表现文档. 虚拟 dom 不是真正意义上的 dom.而是一个 javascript 对象. 正常的 dom 节点在 html 中是这样表示: <div class='testId'> <p>你好</p> <p>欢迎光临</p> </div> 而在虚拟 dom 中大概是这样: { tag: 'div', attributes:{ class: ['testId']

  • Log4j新手快速入门教程

    简介 Log4J 是 Apache 的一个开源项目(官网 http://jakarta.apache.org/log4j),通过在项目中使用 Log4J,我们可以控制日志信息输出到控制台.文件.GUI 组件.甚至是数据库中.我们可以控制每一条日志的输出格式,通过定义日志的输出级别,可以更灵活的控制日志的输出过程.方便项目的调试. 组成 Log4J 主要由 Loggers (日志记录器).Appenders(输出端)和 Layout(日志格式化器)组成.其中 Loggers 控制日志的输出级别与日

  • Vue项目打包优化实践指南(推荐!)

    目录 业务背景 项目打包时间分析方法 优化配置的基本思路 前置操作,先通过 webpack-bundle-analyzer 查看资源树 1.vue.config.js 文件中修改:productionSourceMap 为 false 2.针对首屏请求数进行优化—关闭 Prefetch,关闭 preload 一.采用路由懒加载 二.element-ui 组件按需加载 1.在 vue-cli3 项目中,如果没有 babel.config.js,则需要安装 Babel: 2.按照官网文档安装 3.b

随机推荐