首页 前端知识 基于springboot mybatis vue的项目实战之增删改查CRUD—Restful风格

基于springboot mybatis vue的项目实战之增删改查CRUD—Restful风格

2024-06-26 23:06:13 前端知识 前端哥 847 286 我要收藏

整个代码与前面的案例基于springboot+mybatis+vue的项目实战之增删改查CRUD-CSDN博客

mapper和servie没有任何的变化。

有变化的主要是controller和html页面

1、controller

package com.example.controller;

import com.example.pojo.Peot;
import com.example.pojo.Result;
import com.example.service.PeotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class PeotRestfulController {

    @Autowired
    private PeotService peotService;


    //-------查询所有-------

    @GetMapping("/peots")
    public Result findAllJson1() {
        return Result.seccess(peotService.findAll());
    }


    //-------按id进行删除-------

    @DeleteMapping("/peots/{id}")
    public void deletePeot1(@PathVariable("id") Integer id) {
        peotService.deletePeot(id);
    }

    // -------按id进行查询-------

    @GetMapping("/peots/{id}")
    public Result peotfindById(@PathVariable("id") Integer id) {
        return Result.seccess(peotService.peotfindById(id));
    }

    //-----更新-------

    @PutMapping("/peots")
    public Result updatePeot(@RequestBody Peot peot) {
        boolean r = peotService.updatePeot(peot);

        if (r) {
            // 成功  code==1
            return Result.success();
        } else {
            // 失败  code==0
            return Result.erro("更新失败");
        }
    }

    //-------插入-------

    @PostMapping("/peots")
    public Result insertUser(@RequestBody Peot peot) {
        boolean result = peotService.insertUser(peot);
        if (result) {
            // 成功  code==1
            return Result.success();
        } else {
            // 失败  code==0
            return Result.erro("添加失败");

        }

    }

}

2、html页面

peot_listAll_restful.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>显示所有(删除用按钮和链接均实现)</title>
  <script src="./js/vue.js"></script>
  <script src="./js/axios-0.18.0.js"></script>

</head>
<body>
<h1 align="center">诗人信息列表listAll</h1>
<div id="app" align="center">
  <a href="peot_insert.html">新增</a>
  <table  border="1">
    <tr>
      <th>id</th>
      <th>author</th>
      <th>gender</th>
      <th>dynasty</th>
      <th>title</th>
      <th>style</th>
      <th>操作</th>
    </tr>
    <tr v-for="peot in peotList">
      <td>{{peot.id}}</td>
      <td>{{peot.author}}</td>
      <td>{{peot.gender}}</td>
      <td>{{peot.dynasty}}</td>
      <td>{{peot.title}}</td>
      <td>{{peot.style}}</td>
      <td>
        <!--<button type="button" @click="deleteId(peot.id)">删除</button>-->
        <button type="button" @click="deleteId_restful(peot.id)">删除</button>

        <a :href="'peot_delete2.html?id='+peot.id">删除</a>
        <a :href="'peot_edit.html?id='+peot.id">修改</a>
      </td>
    </tr>

  </table>
</div>

</body>

<script>
  new Vue({
    el:"#app",
    data() {
      return {
        peotList:[]
      }
    },

    methods:{
      findAll:function () {
        var _this = this;
        axios.get('/peots', {
        })
                .then(function (response) {
                  _this.peotList = response.data.data;//响应数据给peotList赋值
                })
                .catch(function (error) {
                  console.log(error);
                });
      },


      // 采用restful模式,url来传递。
      deleteId_restful:function (id) {
        var _thisd = this;
        if (window.confirm("确定要删除该条数据吗???")){
          //${this.id}
          var url = `peots/${id}`  //注意这里是反引号
           // 当您想要引用当前对象或类的实例的 id 属性时,使用 this.id。
          //当您想要引用一个名为 id 的变量(无论它是在函数外部定义的,还是作为参数传递的),使用 id。

          axios.delete(url)
                  .then(function (response) {
                    alert("删除成功")
                    _thisd.findAll();
                  })
                  .catch(function (error) {
                    console.log(error);
                  });
        }
      }

    },
    created() {
      // 获得参数id值
      // this.id = location.href.split("?id=")[1]
      // 通过id查询详情
      this.findAll();
    },
  })


</script>

</html>

peot_edit.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./js/vue.js"></script>
    <script src="./js/axios-0.18.0.js"></script>

</head>
<body>
<div id="app" align="center">
    <table border="1">
        <tr>
            <td>编号</td>
            <td><input type="text" v-model="this.id"> </td>
        </tr>
        <tr>
            <td>姓名</td>
            <td><input type="text" v-model="peot.author"> </td>
        </tr>
        <tr>
            <td>性别</td>
            <td>
                <input type="radio" name="gender" v-model="peot.gender" value="1"> 男
                <input type="radio" name="gender" v-model="peot.gender" value="0"> 女
            </td>
        </tr>
        <tr>
            <td>朝代</td>
            <td><input type="text" v-model="peot.dynasty"> </td>
        </tr>
        <tr>
            <td>头衔</td>
            <td><input type="text" v-model="peot.title"> </td>
        </tr>
        <tr>
            <td>风格</td>
            <td><input type="text" v-model="peot.style"> </td>
        </tr>

        <tr>
            <td></td>
            <td><input type="button" @click="updatePeot" value="更新"> </td>

        </tr>
    </table>
    {{peot}}
</div>


</body>
<script>
    new Vue({
        el: '#app',
        data: {
            id: '',
            peot: {},        //详情
        },
        methods: {
            selectById() {
                //${this.id}
             //   var url = `peotfindById/${this.id}`  //注意这里是反引号
                var url = `peots/${this.id}`
               // 当您想要引用当前对象或类的实例的 id 属性时,使用 this.id。
            //当您想要引用一个名为 id 的变量(无论它是在函数外部定义的,还是作为参数传递的),使用 id。
                //反引号(backticks,也称为模板字符串或模板字面量)是ES6(ECMAScript 2015)中引入的一种新字符串字面量功能,
                // 它允许您在字符串中嵌入表达式。反引号用`(键盘上通常位于Tab键上方)来界定字符串的开始和结束。
                axios.get(url)
                    .then(response => {
                        var baseResult = response.data
                        if(baseResult.code == 1) {
                            this.peot = baseResult.data
                        }
                    })
                    .catch( error => {})
            },
            updatePeot() {
             //   var url = 'peotupdate'
                var url = 'peots'
                axios.put(url,this.peot)
                    .then(res => {
                        var baseResult = res.data
                        if(baseResult.code == 1) {
                            // 成功
                            location.href = 'peot_listAll_restful.html'
                        } else {
                            // 失败
                            alert(baseResult.message)
                        }
                    })
                    .catch(err => {
                        console.error(err);
                    })
            },


        },
        created() {
            // 获得参数id值
            this.id = location.href.split("?id=")[1]
            // 通过id查询详情
            this.selectById()
        },

    })



</script>

</html>

peot_insert.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <script src="./js/vue.js"></script>
  <script src="./js/axios-0.18.0.js"></script>

</head>
<body>
<div id="app" align="center">
    <table border="1">

        <tr>
            <td>姓名</td>
            <td><input type="text" v-model="peot.author"> </td>
        </tr>
        <tr>
            <td>性别</td>
            <td>
                <input type="radio" name="gender" v-model="peot.gender" value="1"> 男
                <input type="radio" name="gender" v-model="peot.gender" value="0"> 女
            </td>
        </tr>
        <tr>
            <td>朝代</td>
            <td><input type="text" v-model="peot.dynasty"> </td>
        </tr>
        <tr>
            <td>头衔</td>
            <td><input type="text" v-model="peot.title"> </td>
        </tr>
        <tr>
            <td>风格</td>
            <td><input type="text" v-model="peot.style"> </td>
        </tr>

        <tr>
            <td></td>
            <td><input type="button" @click="addPeot" value="增加"> </td>

        </tr>
    </table>

</div>
</body>
<script>
    new Vue({
        el: '#app',
        data: {
            peot: {
                "author":"",
                "gender":"",
                "dynasty":"",
                "title":"",
                "style":""
            }        //详情

        },
        methods: {

            addPeot() {
             //   var url = 'peotinsert'
                var url = 'peots'
                axios.post(url,this.peot)
                    .then(res => {
                        var baseResult = res.data
                        if(baseResult.code == 1) {
                            // 成功
                            location.href = 'peot_listAll_restful.html'
                        } else {
                            // 失败
                            alert(baseResult.message)
                        }
                    })
                    .catch(err => {
                        console.error(err);
                    })
            }
        },

    })
</script>

</html>

peot_delete2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./js/vue.js"></script>
    <script src="./js/axios-0.18.0.js"></script>

</head>
<body>
<h1 align="center">诗人信息列表</h1>
<div id="app">
</div>

</body>

<script>
    new Vue({
        el:"#app",
        data() {

        },

        methods:{

            deleteId:function (id) {
                var _thisd = this;
                var url = `peots/${this.id}`  //注意这里是反引号
                if (window.confirm("确定要删除该条数据吗???")){
                    axios.delete(url)
                        .then(function (response) {
                            alert("删除成功")
                            // _thisd.findAll();
                            location.href = 'peot_listAll.html'
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }
            }

        },
        created() {
            // 获得参数id值
            this.id = location.href.split("?id=")[1]
            // 通过id查询详情
            this.deleteId();
        },
    })


</script>

</html>

转载请注明出处或者链接地址:https://www.qianduange.cn//article/13644.html
标签
mybatis
评论
发布的文章

Markdown基础与进阶语法

2024-06-30 22:06:12

零基础 HTML 入门(详细)

2024-06-30 22:06:09

CSS3基本语法

2024-06-30 22:06:51

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!