整个代码与前面的案例基于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()); |
| } |
| |
| |
| |
| |
| @DeleteMapping("/peots/{id}") |
| public void deletePeot1(@PathVariable("id") Integer id) { |
| peotService.deletePeot(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) { |
| |
| return Result.success(); |
| } else { |
| |
| return Result.erro("更新失败"); |
| } |
| } |
| |
| |
| |
| @PostMapping("/peots") |
| public Result insertUser(@RequestBody Peot peot) { |
| boolean result = peotService.insertUser(peot); |
| if (result) { |
| |
| return Result.success(); |
| } else { |
| |
| 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_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; |
| }) |
| .catch(function (error) { |
| console.log(error); |
| }); |
| }, |
| |
| |
| |
| deleteId_restful:function (id) { |
| var _thisd = this; |
| if (window.confirm("确定要删除该条数据吗???")){ |
| |
| var url = `peots/${id}` |
| |
| |
| |
| axios.delete(url) |
| .then(function (response) { |
| alert("删除成功") |
| _thisd.findAll(); |
| }) |
| .catch(function (error) { |
| console.log(error); |
| }); |
| } |
| } |
| |
| }, |
| created() { |
| |
| |
| |
| 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() { |
| |
| |
| var url = `peots/${this.id}` |
| |
| |
| |
| |
| axios.get(url) |
| .then(response => { |
| var baseResult = response.data |
| if(baseResult.code == 1) { |
| this.peot = baseResult.data |
| } |
| }) |
| .catch( error => {}) |
| }, |
| updatePeot() { |
| |
| 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() { |
| |
| this.id = location.href.split("?id=")[1] |
| |
| 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 = '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("删除成功") |
| |
| location.href = 'peot_listAll.html' |
| }) |
| .catch(function (error) { |
| console.log(error); |
| }); |
| } |
| } |
| |
| }, |
| created() { |
| |
| this.id = location.href.split("?id=")[1] |
| |
| this.deleteId(); |
| }, |
| }) |
| |
| |
| </script> |
| |
| </html> |
复制