springboot+vue 前后端交互实现(mysql+springboot+vue)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: springboot+vue 前后端交互实现(mysql+springboot+vue)

前言


编译器:vscode、idea、mysql5.7

技术:springboot+mybatis+mysql+lombok+vue

实现内容:实现前后端数据交互


实现效果:






一、准备工作


因为vue和springboot是前后端分离的,所以在开始交互前首先需要解决跨域问题,可以在前端做也可以在后端加配置类,这里我是在后端加的CORS策略配置类。


@Configuration
public class cors {
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOriginPatterns("*")
                        .allowedMethods("GET", "POST", "PUT", "DELETE")
                        .allowedHeaders("*")
                        .allowCredentials(true)
                        .maxAge(3600);
            }
        };
    }
}



还需要在前端下载并配置好axios库,vscode的终端里面输入命令行即可下载


npm install axios


下载成功后点击main.js并配置


import Vue from 'vue'
import App from './App.vue'
import axios from 'axios'
Vue.config.productionTip = false
// 全局挂载axios
Vue.prototype.$axios=axios
new Vue({
  axios,
  render: h => h(App),
}).$mount('#app')
//若本地项目调试使用
axios.defaults.baseURL = 'http://localhost:8080'; 


首先导入axios库


import axios from `axios`


导入成功后在new Vue中挂载在app中


最后设置我们的基础url


axios.defaults.baseURL='基础路径'



二、实现过程


1.后端


这里的后端唯一的难点可能就是mapper的sql语句了,因为我这里是单表连接,所以就非常简洁高效,这里我只展示mapper.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.wjm.hxx.mapper.mapper">
    <insert id="addtest">
        insert into test(cname,direction,project,introduce,classtime,tid,semester) values(#{cname},#{direction},#{project},#{introduce},#{classtime},#{tid},#{semester})
    </insert>
    <update id="updtest">
        update test set cname=#{cname},direction=#{direction},project=#{project},introduce=#{introduce},classtime=#{classtime},tid=#{tid},semester=#{semester} where id=#{id}
    </update>
    <delete id="deltest">
        delete from test where id=#{id}
    </delete>
    <select id="querytest" resultType="com.wjm.hxx.pojo.test">
        select * from test
    </select>
    <select id="queryByIdtest" resultType="com.wjm.hxx.pojo.test">
        select * from test where cname like concat('%',#{cname},'%')
    </select>
</mapper>


2.前端


用axiosHTTP库调用get、post方法的语法格式如下,


 this.$axios.post/get(`/url?id=${id}`)
      .then(res=>{
        console.log(res)
      })


在上述代码块中,我们调用axios库的post或get方法,并且用then方法进行回调函数的调用,同时如果我们如果想要传递参数需要用到参数名=${this.传递参数},这样就可以进行前后端交互,而样式和template模板这里就不再赘述。


前端代码如下:


<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <div>
      <input type="text" placeholder="请输入课程名称" v-model="cname">
      <button class="btn" v-on:click="queryById(cname)">搜索</button>
      <button class="btn" style="float:left;left:20px;" v-on:click="addshow">添加课程</button>
      <button v-on:click="query" class="btn">全局搜索</button>
    </div>
    <div class="overlay" id="overlay"></div>
    <div class="auform" id="auform">
      <label>课程名称:</label>
      <input type="text" placeholder="请输入课程名称" v-model="cname"><br><br>
      <label>专业方向:</label>
      <input type="text" placeholder="请输入专业方向" v-model="direction"><br><br>
      <label>项目名称:</label>
      <input type="text" placeholder="请输入项目名称" v-model="project"><br><br>
      <label>项目简介:</label>
      <input type="text" placeholder="请输入项目简介" v-model="introduce"><br><br>
      <label>上课时间:</label>
      <input type="text" placeholder="请输入上课时间" v-model="classtime"><br><br>
      <label>任课老师:</label>
      <input type="text" placeholder="请输入任课老师" v-model="tid"><br><br>
      <label>授课学期:</label>
      <input type="text" placeholder="请输入授课学期" v-model="semester"><br><br>
      <button v-if="addflag" class="btn" @click="add">添加</button>
      <button v-else-if="updflag" class="btn" @click="upd">修改</button>
      <button class="btn" v-on:click="close">关闭</button>
    </div>
    <div class="table">
        <div class="tr">
            <span class="th">课程名称</span>
            <span class="th">专业方向</span>
            <span class="th">项目名称</span>
            <span class="th">项目简介</span>
            <span class="th">上课时间</span>
            <span class="th">任课老师</span>
            <span class="th">授课学期</span>
            <span class="th">操作</span> 
        </div>
        <div class="tr" v-for="(item,index) in arrayList" :key="index">
          <span class="td">{{ item.cname }}</span>
            <span class="td">{{ item. direction}}</span>
            <span class="td">{{ item.project }}</span>
            <span class="td">{{ item.introduce }}</span>
            <span class="td">{{ item.classtime }}</span>
            <span class="td">{{ item.tid }}</span>
            <span class="td">{{ item.semester }}</span>
            <span class="td"><button class="sbtn" v-on:click="updshow(item.id)">修改</button><button class="sbtn" v-on:click="del(item.id)">删除</button></span> 
        </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'HelloWorld',
  data(){
    return{
      arrayList:[],
      cname:"",
      direction:"",
      project:"",
      introduce:"",
      classtime:"",
      tid:"",
      semester:"",
      id:1,
      addflag:false,
      updflag:false
    }
  },
  props: {
    msg: String
  },
  mounted(){
    this.query()
  },
  methods:{
    query:function(){
      this.$axios.get(`http://localhost:8080/querytest`).then((res) => {
   if(res.data){
    this.arrayList=res.data
    console.log(res.data)
   }else{
     console.log("fail")
   }
})
    },
    add:function(){
      this.$axios.post(`/addtest?cname=${this.cname}&&direction=${this.direction}&&project=${this.project}&&introduce=${this.introduce}&&classtime=${this.classtime}&&tid=${this.tid}&&semester=${this.semester}`)
      .then((res=>{
        console.log(res)
        this.close()
        this.query();
      }))
    },
    del:function(item){
        this.$axios.get(`/deltest?id=${item}`).then((res=>{
          console.log(res)
          this.query()
        }))
    },
    queryById:function(cname){
        this.$axios.get(`/queryByIdtest?cname=${cname}`).then(res=>{
          console.log(res.data)
          this.arrayList=res.data
        })
    },
    upd:function(){
      this.$axios.post(`/updtest?id=${this.id}&cname=${this.cname}&direction=${this.direction}&project=${this.project}&introduce=${this.introduce}&classtime=${this.classtime}&tid=${this.tid}&semester=${this.semester}`)
      .then((res=>{
        console.log(res.data)
        this.close()
        this.query()
      }))
    },
    addshow(){
      this.addflag=true;
      document.getElementById('overlay').style.display='block';
      document.getElementById('auform').style.display='block'
    },
    updshow(id){
      this.id=id
      this.updflag=true;
      document.getElementById('overlay').style.display='block';
      document.getElementById('auform').style.display='block'
    },
    close(){
      this.addflag=false,
      this.updflag=false,
      document.getElementById('overlay').style.display='none';
      document.getElementById('auform').style.display='none'
    }
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.btn{
  color:white;
  margin-left:10px;
  height:30px;
  width:70px;
  border:none;
  background-color: rgb(188, 21, 239);
  border-radius:5px;
}
input{
  margin-top:20px;
  border:none;
  border-bottom:1px solid lightgray;
  width:200px;
  height:30px;
}
.table{
  margin-left:30%;
  margin-top:20px;
  width:auto;
  height:auto;
}
.tr{
  width:100vh;
  border-bottom:1px solid lightgray;
  height:50px;
}
.th,.td{
  line-height:50px;
  display:inline-block;
  width:80px;
  margin-right:10px;
}
.sbtn{
  line-height:15px;
  border:none;
  font-size:1px;
  width:37.5px;
  display:inline;
}
button{
  background-color: white;
}
button:hover{
  color:red;
}
.auform{
  position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            padding: 20px;
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
            display: none; /* 默认隐藏 */
}
 /* 遮罩层样式 */
 .overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
            display: none; /* 默认隐藏 */
        }
</style>
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2天前
|
JSON JavaScript Java
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
10 0
|
15天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的4S店客户管理系统的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的4S店客户管理系统的详细设计和实现
39 4
|
15天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的在线课堂微信小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的在线课堂微信小程序的详细设计和实现
28 3
|
15天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的微信课堂助手小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的微信课堂助手小程序的详细设计和实现
47 3
|
15天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的商品展示的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的商品展示的详细设计和实现
29 3
|
15天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的电子商城购物平台的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的电子商城购物平台的详细设计和实现
44 3
|
15天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的英语学习交流平台的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的英语学习交流平台的详细设计和实现
28 2
|
15天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的助农扶贫微信小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的助农扶贫微信小程序的详细设计和实现
31 2
|
15天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的微信阅读网站小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的微信阅读网站小程序的详细设计和实现
39 2
|
15天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的小说阅读器的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的小说阅读器的详细设计和实现
28 2