目录

开发者介绍

什么是前后端分离开发

vue与springboot开发的优势

Vue.js 的优势:

Spring Boot 的优势:

vue与springboot如何实现前后端连接

demo简介

重要部分前端部分代码

重要部分后端代码

后端解决跨域问题

Controller部分

xml部分

service部分

demo示例演示


开发者介绍

什么是前后端分离开发

vue与springboot开发的优势

vue与springboot如何实现前后端连接

demo简介

注:小demo只是做了简单的演示示例,功能齐全但样式简单,望大家谅解!

此小demo适用于刚接触前端后分离开发的小伙伴,尤其针对前端vue+后端spring boot分离开发的小伙伴。

本项目主要完成了通过前端的vue.js与后端的spring boot的连接,实现了简单的实现了通过前端页面来调用后端API接口,从而完成对数据库中内容的增删改查。

重要部分前端部分代码

<template>
  <div class="big_box">
    <!-- 添加数据 -->
    <div style="display:flex;">
      <el-input v-model="newData.product_date" placeholder="请输入日期"></el-input>
      <el-input v-model="newData.brand" placeholder="请输入品牌"></el-input>
      <el-input v-model="newData.commodity_name" placeholder="请输入产品"></el-input>
      <el-input v-model="newData.act" placeholder="请输入产品描述"></el-input>
      <el-input v-model="newData.commodity_id" placeholder="请输入产品编号"></el-input>
      <el-input v-model="newData.price" placeholder="请输入价格"></el-input>
      <el-input v-model="newData.amount" placeholder="请输入库存量"></el-input>
      <el-button @click="add" type="success" size="small">添加</el-button>
    </div>
    <!-- 数据展示 -->
      <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        fixed
        prop="product_date"
        label="日期"
        width="150">
      </el-table-column>
      <el-table-column
        prop="brand"
        label="品牌"
        width="120">
      </el-table-column>
      <el-table-column
        prop="commodity_name"
        label="产品"
        width="120">
      </el-table-column>
      <el-table-column
        prop="act"
        label="产品描述"
        width="300">
      </el-table-column>
      <el-table-column
        prop="commodity_id"
        label="产品编号"
        width="120">
      </el-table-column>
      <el-table-column
        prop="price"
        label="价格"
        width="120">
      </el-table-column>
      <el-table-column
        prop="amount"
        label="库存量"
        width="120">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="warning" size="small">修改</el-button>
          <el-button @click="del_data(scope.row.commodity_id)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 修改数据 -->
    <div v-if="this.update">
      <div style="display:flex;">
      <el-input v-model="upData.product_date" placeholder="请输入日期"></el-input>
      <el-input v-model="upData.brand" placeholder="请输入品牌"></el-input>
      <el-input v-model="upData.commodity_name" placeholder="请输入产品"></el-input>
      <el-input v-model="upData.act" placeholder="请输入产品描述"></el-input>
      <el-input v-model="upData.commodity_id" placeholder="请输入产品编号"></el-input>
      <el-input v-model="upData.price" placeholder="请输入价格"></el-input>
      <el-input v-model="upData.amount" placeholder="请输入库存量"></el-input>
      <el-button @click="edit_put" type="success" size="small">修改</el-button>
    </div>
    </div>
  </div>
  
</template>

<script>
import axios from 'axios'

  export default{
    mounted(){
      // 显示数据
      axios.get('http://localhost:8090/car/query').then(res=>{
        console.log(res.data.data);
        this.tableData = res.data.data
      })
    },
    methods: {
      handleClick(row) {
        this.update = true
        this.row_data = row
        console.log(this.row_data);
        this.id = row.commodity_id
      },
      // 条件修改
      edit_put(){
        axios.post('http://localhost:8090/car/update',{
          commodity_id:this.upData.commodity_id
        }).then(res=>{
          console.log(res.data)
          this.update = false
        })
      },
      // 添加数据
      add(){
        axios.post('http://localhost:8090/car/add',this.newData).then(res=>{
          console.log(res.data)
        })
      },
      // 删除数据
      del_data(commodity_id){
        console.log(commodity_id);
        axios.get('http://localhost:8090/car/del?commodity_id='+commodity_id).then(res=>{
          console.log(res.data)
        })
      }
    },
    data() {
      return {
        update:false,
        id:'',
        row_data:'',
        // 添加数据data
        newData:{
          brand:'',
          commodity_id:'',
          commodity_name:'',
          product_date:'',
          act:'',
          amount:null,
          price:null
        },
        // 修改数据data
        upData:{
          brand:'',
          commodity_id:'',
          commodity_name:'',
          product_date:'',
          act:'',
          amount:null,
          price:null
        },
        tableData: []

      }
    }
  }
</script>
<style scoped>
.big_box{
  width: 1200px;
}

</style>

重要部分后端代码

后端解决跨域问题

@CrossOrigin(origins = "*")

Controller部分

package com.wfit.boot.controller;

import com.wfit.boot.commons.Result;
import com.wfit.boot.model.Car;
import com.wfit.boot.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/car")
public class CarController {

    @Autowired
    private CarService carService;

    /**
     * 新增汽车用品信息
     * @param car
     * @return
     */
    @PostMapping("/add")
    @CrossOrigin(origins = "*")
    public Result addCar(@RequestBody Car car){
        carService.saveCar(car);
        return Result.success("新增汽车用品信息成功:" + car);
    }

    /**
     * 修改
     * @param car
     * @return
   */
   @PostMapping ("/update")
   @CrossOrigin(origins = "*")
    public Result updateCar(@RequestBody Car car){
        carService.updateCar(car);
        return Result.success("修改汽车用品信息成功:" + car);
    }

    /**
     * 删除
     * @param commodity_id
     * @return
     */
    @GetMapping("/del")
    @CrossOrigin(origins = "*")
    public Result delCar(String commodity_id){
        carService.delCar(commodity_id);
        return Result.success("删除编号为" + commodity_id + "汽车用品信息成功!");
    }

    /**
     * 查询
     * @return
     */
    @GetMapping("/query")
    @CrossOrigin(origins = "*")
    public Result queryCar(){
        List<Car> carList = carService.queryCar();
        return Result.success(carList);
    }
}

xml部分

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wfit.boot.mapper.CarMapper">
<!--保存汽车用品信息-->
    <insert id="saveCar" parameterType="com.wfit.boot.model.Car">
        insert into car values (
            #{brand},
            #{commodity_id},
            #{commodity_name},
            #{act},
            #{product_date},
            #{amount},
            #{price}
        )
    </insert>
<!--    修改汽车用品信息-->
    <update id="updateCar" parameterType="com.wfit.boot.model.Car">
        update car
        set price = #{price}
        where commodity_id =#{commodity_id}
    </update>
<!--    删除汽车用品信息-->
    <delete id="delCar" parameterType="java.lang.String">
        delete from car where commodity_id = #{commodity_id};
    </delete>
<!--    查询汽车用品信息-->
    <select id="queryCar" resultType="com.wfit.boot.model.Car">
        select *
        from car
    </select>
</mapper>

service部分

package com.wfit.boot.service.impl;

import com.wfit.boot.mapper.CarMapper;
import com.wfit.boot.model.Car;
import com.wfit.boot.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private CarMapper carMapper;

    @Override
    public void saveCar(Car car) {
        carMapper.saveCar(car);
    }

    @Override
    public void updateCar(Car car) {
        carMapper.updateCar(car);
    }

    @Override
    public void delCar(String commodity_id) {
        carMapper.delCar(commodity_id);
    }

    @Override
    public List<Car> queryCar() {
        return carMapper.queryCar();
    }

}

demo示例演示

二十分钟秒懂:实现前后端分离开发(vue+element+spring boot+mybatis+MySQL)-LMLPHP

 

二十分钟秒懂:实现前后端分离开发(vue+element+spring boot+mybatis+MySQL)-LMLPHP

 

二十分钟秒懂:实现前后端分离开发(vue+element+spring boot+mybatis+MySQL)-LMLPHP

 

二十分钟秒懂:实现前后端分离开发(vue+element+spring boot+mybatis+MySQL)-LMLPHP

 

05-30 05:53