116.【SpringBoot和Vue结合-图书馆管理系统】(八)

简介: 116.【SpringBoot和Vue结合-图书馆管理系统】
<template>
  <div>
    <el-button type="text" @click="open" v-show="'false'"></el-button>
    <!--  :model 用于绑定我们的数据  :rules用于绑定规则-->
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
          <el-form-item label="图书名称" prop="id">
        <el-input v-model="ruleForm.id" readonly></el-input>
      </el-form-item>
      <el-form-item label="图书名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="图书作者" prop="author">
        <el-input v-model="ruleForm.author"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即修改</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button @click="test">测试</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      // 1. 表单的数据-->这里的字段名一定要和数据库的对应上
      ruleForm: {
        id:1,
        name: "",
        author: "",
      },
      isSuccess: false,
      // 2. 校验的规则
      rules: {
        BookName: [
          // 是否强制? 提示信息? 触发条件?
          { required: true, message: "请输入图书名称", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
        BookAuthor: [
          { required: true, message: "请输入图书作者", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      // 这里传递过来的是data区域的值
      const _this = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 假如规则都正确
          // TODO: post形式传递一个对象
          axios
            .post("http://localhost:8181/update", _this.ruleForm)
            .then((Response) => {
              if (Response.status == 200) {
                console.log(Response);
                // 跳转路由
                // _this.$router.push('/BookMange')
                _this.$alert("数据修改成功", "小提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    _this.$router.push("/BookMange");
                  },
                });
              }
              console.log(Response);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    test() {
      // 我们通过测试发现我们的文本会出现在这里...
      console.log(this.ruleForm);
    },
  },
  created(){
        // 获取传递过来的参数
        const _this = this;
        axios.get('http://localhost:8181/findbyid/'+this.$route.query.id).then(function(response){
            _this.ruleForm = response.data
        })
    },
    mounted() {
         console.log('更新',this)
    },
};
</script>

6.操作表单数据-(删除数据)

(1).SpringBoot后端布局
package com.jsxs.controller;
import com.jsxs.pojo.Book;
import com.jsxs.repository.BookRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
 * @Author Jsxs
 * @Date 2023/5/14 15:23
 * @PackageName:com.jsxs.controller
 * @ClassName: BookHandler
 * @Description: TODO 以前我们的方法返回值为String目的是为了跳转页面,现在只是为了传递数据
 * @Version 1.0
 */
@RestController
public class BookHandler {
    @Resource
    private BookRepository bookRepository;
    // 1.查找数据的方法
    @GetMapping("/findAll/{page}/{size}")
    public Page<Book> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
//      页数是从0开始的所以-1
        Pageable pageable = PageRequest.of((page - 1), size);  // 第一个参数是 : 页数、 第二个数是: 一页几张
        return bookRepository.findAll(pageable);
    }
    //  2.新增数据的方法
    @PostMapping("/save")
    public String save(@RequestBody Book book) { //这个注解是将传递过来的Json对象转换为Java对象
        Book result = bookRepository.save(book);
        if (result != null) {
            System.out.println("数据" + book);
            return "恭喜添加成功";
        } else {
            return "对不起添加失败";
        }
    }
    //  3.通过id进行查找的方法
    @GetMapping("/findbyid/{id}")
    public Book findById(@PathVariable("id") Integer id) {
        System.out.println("-------通过id查询数据成功!!");
        return bookRepository.findById(id).get();
    }
//  4.通过id进行数据的修改
    @PostMapping("/update")
    public String updateById(@RequestBody Book book){
        Book save = bookRepository.save(book);
        if (save!=null){
            return "修改数据成功";
        }else {
            return "修改数据失败";
        }
    }
//   5.删除的操作
    @GetMapping("/deletedid/{id}")
    public String deleteById(@PathVariable("id") Integer id){
        bookRepository.deleteById(id);
        return "删除成功";
    }
}
}
(2).Vue前端布局
1.传递本行的信息
 <el-button type="text" size="small" @click="delete_one(scope.row)">删除</el-button
 2.进行删除
methods{
 // 删除的操作
    delete_one(row) {
      console.log("sdsdsd", row);
      const _this = this;
      axios;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          axios
            .get("http://localhost:8181/deletedid/" + row.id)
            .then(function (respsone) {
              console.log("删除->", respsone);
              _this.$router.push("/AddBook");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },

BookMange.vue

<template>
  <div>
    <el-button type="text" @click="open" v-show="'false'"></el-button>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column
        fixed
        prop="id"
        label="编号"
        width="150"
      ></el-table-column>
      <el-table-column prop="name" label="书名" width="120"> </el-table-column>
      <el-table-column prop="author" label="作者" width="120"></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="edit(scope.row)" type="text" size="small"
            >修改</el-button
          >
          <el-button type="text" size="small" @click="delete_one(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页的操作 : 这里的页数是: 总条数/每页的条数-->
    <el-pagination
      background
      layout="prev, pager, next"
      page-size="6"
      :total="totalPage"
      @current-change="page"
    ></el-pagination>
  </div>
</template>
<script>
import axios from "axios";
export default {
  methods: {
    edit(row) {
      // 获取本行所有的属性封装成一个对象
      console.log(row);
      // 利用query传递参数
      this.$router.push({
        path: "/BookUpdate",
        query: {
          id: row.id,
        },
      });
    },
    page(currentpage) {
      //得到的参数是我们的页码
      const _this = this;
      axios
        .get("http://localhost:8181/findAll/" + currentpage + "/6")
        .then(function (response) {
          console.log(response);
          // 传送数据- 页面信息
          _this.tableData = response.data.content;
          // 传递总页数-
          _this.totalPage = response.data.totalElements;
        })
        .catch({})
        .finally({});
    },
    // 删除的操作
    delete_one(row) {
      console.log("sdsdsd", row);
      const _this = this;
      axios;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          axios
            .get("http://localhost:8181/deletedid/" + row.id)
            .then(function (respsone) {
              console.log("删除->", respsone);
              _this.$router.push("/AddBook");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  data() {
    return {
      tableData: [],
      totalPage: 0,
    };
  },
  mounted() {
    const _this = this;
    axios
      .get("http://localhost:8181/findAll/1/6")
      .then(function (response) {
        console.log(response);
        // 传送数据- 页面信息
        _this.tableData = response.data.content;
        // 传递总页数-
        _this.totalPage = response.data.totalElements;
      })
      .catch({})
      .finally({});
  },
};
</script>

(四)、总结

1.SpringBoot接受数据风格

  1. 只能用Resultful风格进行接受前端传来的数据

2.前端传向后端

  1. axios.get(‘http:xxxx’,).then()
  2. axios.post(‘http:xxxx’,参数).then()

3.前端传向前端

  1. vm.$router.push({path:‘/xxx’,query{},params{}})
  2. vm.$router.replace({path:‘/xxx’,query{},params{}})

4.自动刷新页面

window.location.reload() // 自动刷新页面

相关文章
|
21天前
|
JavaScript Java 关系型数据库
基于springboot的项目管理系统
本文探讨项目管理系统在现代企业中的应用与实现,分析其研究背景、意义及现状,阐述基于SSM、Java、MySQL和Vue等技术构建系统的关键方法,展现其在提升管理效率、协同水平与风险管控方面的价值。
|
14天前
|
监控 安全 JavaScript
2025基于springboot的校车预定全流程管理系统
针对传统校车管理效率低、信息不透明等问题,本研究设计并实现了一套校车预定全流程管理系统。系统采用Spring Boot、Java、Vue和MySQL等技术,实现校车信息管理、在线预定、实时监控等功能,提升学校管理效率,保障学生出行安全,推动教育信息化发展。
|
14天前
|
JavaScript Java 关系型数据库
基于springboot的高校运动会系统
本系统基于Spring Boot、Vue与MySQL,实现高校运动会报名、赛程安排及成绩管理的全流程信息化,提升组织效率,杜绝信息错漏与冒名顶替,推动体育赛事智能化发展。
|
11天前
|
JavaScript 安全 Java
基于springboot的大学生兼职系统
本课题针对大学生兼职信息不对称、权益难保障等问题,研究基于Spring Boot、Vue、MySQL等技术的兼职系统,旨在构建安全、高效、功能完善的平台,提升大学生就业竞争力与兼职质量。
|
14天前
|
JavaScript Java 关系型数据库
基于springboot的美食城服务管理系统
本系统基于Spring Boot、Java、Vue和MySQL技术,构建集消费者服务、商家管理与后台监管于一体的美食城综合管理平台,提升运营效率与用户体验。
|
16天前
|
Java 关系型数据库 MySQL
基于springboot的网咖网吧管理系统
本文探讨了基于Java、MySQL和SpringBoot的网吧管理系统的设计与实现。随着信息化发展,传统管理方式难以满足需求,而该系统通过先进技术提升管理效率、保障数据安全、降低运营成本,具有重要意义。
|
18天前
|
JavaScript Java 关系型数据库
基于springboot的摄影师分享交流社区系统
本系统基于Spring Boot与Vue构建摄影师分享交流平台,旨在打造专业社区,支持作品展示、技术交流与合作互动。采用Java、MySQL等成熟技术,提升摄影爱好者创作水平,推动行业发展。
|
19天前
|
JavaScript 搜索推荐 Java
基于SpringBoot的社区老年食堂系统
针对老龄化社会饮食难题,智慧社区老年食堂系统应运而生。融合Spring Boot、Vue、Java与MySQL技术,实现餐饮服务智能化、个性化,提升老年人生活质量与幸福感,推动社区养老服务升级。
|
22天前
|
JavaScript 搜索推荐 Java
基于springboot的民宿预定管理系统
本研究针对民宿市场管理效率低、信息化程度不足等问题,设计并实现基于Spring Boot、Vue和MySQL的民宿预订管理系统。系统提升预订效率与用户体验,助力行业数字化转型。
下一篇
开通oss服务