IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> 【小项目】Vue 整合 Axios -> 正文阅读

[JavaScript知识库]【小项目】Vue 整合 Axios

Vue 整合 Axios

提示:本篇基于【小项目】Axios 实现前后端交互

一、需求说明

  1. 用户发起请求 http://localhost:8080/vue/findAll,获取所有的 student 数据
  2. 通过 Vue.js 在页面中以表格的形式展现
  3. 为每行数据添加修改、删除的按钮
  4. 在一个新的 div 中,编辑 3 个文本框 name/age/sex 通过提交按钮实现新增
  5. 如果用户点击修改按钮,则在用户修改的 div 中回显数据
  6. 用户修改完成后,通过提交按钮实现数据的修改
  7. 当用户点击删除按钮时,要求实现数据的删除

二、数据库

把 student 表清空即可

三、前端

文件名:StudentTable.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生列表</title>
    <script src="../js/axios.js"></script>
    <script src="../js/vue.js"></script>
</head>
<body>
    <div id="app">
        <div align="center">
            <h3>学生新增</h3>
            姓名:<input type="text" v-model="addStu.name">
            邮箱:<input type="text" v-model="addStu.email">
            年龄:<input type="text" v-model="addStu.age">
            <button @click="addStuBtn">新增</button>
        </div>
        <div align="center">
            <h3>学生修改</h3>
            <p>
                编号:<input type="text" v-model="updateStu.id" disabled>
                姓名:<input type="text" v-model="updateStu.name">
            </p>
            <p>
                邮箱:<input type="text" v-model="updateStu.email">
                年龄:<input type="text" v-model="updateStu.age">
            </p>
            <button @click="submitBtn">提交修改</button>
        </div>
        <table align="center" border="1px" width="80%" style="margin-top: 10px">
            <tr align="center">
                <th colspan="5"><h3>学生列表</h3></th>
            </tr>
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>邮箱</th>
                <th>年龄</th>
                <th>操作</th>
            </tr>
            <tr v-cloak v-for="student in studentList" align="center">
                <th v-text="student.id"></th>
                <th v-text="student.name"></th>
                <th v-text="student.email"></th>
                <th v-text="student.age"></th>
                <th width="20%">
                    <button @click="updateStuBtn(student)">修改</button>
                    <button @click="deleteStuBtn(student)">删除</button>
                </th>
            </tr>
        </table>
    </div>
</body>
</html>

<script>

    axios.defaults.baseURL = "http://localhost:8080/Vue"

    const app = new Vue({
        el:"#app",
        data:{
            studentList : [],
            addStu : {
                name : '',
                email : '',
                age : ''
            },
            updateStu : {
                id : '',
                name : '',
                email : '',
                age : ''
            }
        },
        methods:{
            getStuList(){
                axios.get("/findAll").then(promise => {
                    this.studentList = promise.data
                })
            },
            addStuBtn(){
                axios.post("/addStu", this.addStu).then(promise => {
                    alert(promise.data)
                    this.getStuList()
                    this.addStu = {}
                })
            },
            deleteStuBtn(student){
                axios.delete("/deleteStu?id=" + student.id).then(promise => {
                    this.getStuList()
                    alert(promise.data)
                })
            },
            updateStuBtn(student){
                let temp = JSON.parse(JSON.stringify(student))
                this.updateStu = temp

            },
            submitBtn(){
                axios.put("/updateStu", this.updateStu).then(promise => {
                    alert(promise.data)
                    this.updateStu = {}
                    this.getStuList()
                })
            }
        },

        created(){
            this.getStuList()
        }
    })
</script>

四、后端

1.StudentTableService.java

package com.sisyphus.studentssm.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.sisyphus.studentssm.mapper.StudentMapper;
import com.sisyphus.studentssm.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @Description: $
 * @Param: $
 * @return: $
 * @Author: Sisyphus
 * @Date: $
 */
@Service
public class StudentTableService implements IService<Student> {

    @Autowired
    private StudentMapper studentMapper;

    public List<Student> findAll(){
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .select("id","name","email","age");
        return studentMapper.selectList(queryWrapper);
    }

    public void addStu(Student student){
        studentMapper.insert(student);
    }

    public void deleteStu(Integer whereId){
        studentMapper.deleteById(whereId);
    }

    public void updateStu(Student student){
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .eq(true, "id", student.getId())
                .set(true, "name",student.getName())
                .set(true, "email",student.getEmail())
                .set(true, "age",student.getAge());
        studentMapper.update(student, updateWrapper);
    }

    @Override
    public boolean saveBatch(Collection<Student> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Student> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<Student> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Student entity) {
        return false;
    }

    @Override
    public Student getOne(Wrapper<Student> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Student> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<Student> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<Student> getBaseMapper() {
        return null;
    }

    @Override
    public Class<Student> getEntityClass() {
        return null;
    }
}

2.StudentTableController.java

package com.sisyphus.studentssm.controller;

import com.sisyphus.studentssm.pojo.Student;
import com.sisyphus.studentssm.service.StudentTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: $
 * @Param: $
 * @return: $
 * @Author: Sisyphus
 * @Date: $
 */
@RestController
@RequestMapping("Vue")
@CrossOrigin
public class StudentTableController {

    @Autowired
    private StudentTableService studentTableService;

    @GetMapping("findAll")
    public List<Student> findAll(){
        return studentTableService.findAll();
    }

    @PostMapping("addStu")
    public String addStu(@RequestBody Student student){
        studentTableService.addStu(student);
        return "新增成功";
    }

    @DeleteMapping("deleteStu")
    public String deleteStu(@RequestParam("id") Integer whereId){
        studentTableService.deleteStu(whereId);
        return "删除成功";
    }

    @PutMapping("updateStu")
    public String updateStu(@RequestBody Student student){
        studentTableService.updateStu(student);
        return "修改成功";
    }

}

五、测试

1.新增

在这里插入图片描述

2.修改

在这里插入图片描述

3.删除

在这里插入图片描述
在这里插入图片描述

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-28 21:57:59  更:2021-08-28 21:59:12 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年6日历 -2024/6/2 9:18:10-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码