SpringBoot+mysql通过前后端分离实现增删查改
上篇文章介绍了如何写后端接口
SpringBoot连接MySQL获取数据写后端接口
这篇文章,我们就介绍一下怎么写前端
点击修改跳出修改界面,点击添加书籍跳出添加书籍界面
点击删除,直接删除一行数据
其他的也没什么解释了,直接上代码(看不懂的可以私聊我哟)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>图书管理信息</title>
<script src="js/v3.2.8/vue.global.prod.js" charset="utf-8"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.0/axios.js"></script>
</head>
<body>
<div id="bookInfo" style="margin: 20px;">
<div>
<table border="1" style="margin: auto;">
<thead>
<tr>
<th>书籍名称</th>
<th>书籍数量</th>
<th>书籍作者</th>
<th>操作</th>
</tr>
</thead>
<tbody v-for="list in newlist">
<tr>
<td>{{list.name}}</td>
<td>{{list.number}}</td>
<td>{{list.author}}</td>
<td>
<button type="button" v-on:click="updateclickShow(list)">修改</button>
<button type="button" v-on:click="deleteclick(list)">删除</button>
</td>
</tr>
</tbody>
</table>
<div>
<button type="button" v-on:click="addclickShow" style="display:block;margin:0 auto">添加书籍</button>
</div>
<div id="addclick" v-show="addIsShow">
<h3>添加书籍</h3>
<div>书籍名称:<input type="text" v-model="addName" placeholder="请输入需添加书籍名称"></div>
<div>书籍数量:<input type="text" v-model="addNumber" placeholder="请输入需添加书籍数量"></div>
<div>书籍作者:<input type="text" v-model="addAuthor" placeholder="请输入需添加书籍作者"></div>
<div>
<button type="button" v-on:click="addclick">提交</button>
<button type="button" v-on:click="addclickShow">关闭</button>
</div>
</div>
<div id="updateclick" v-show="updateIsShow">
<h3>修改书籍</h3>
<div>书籍名称:<input type="text" v-model="updateName" placeholder="请输入需修改书籍名称"></div>
<div>书籍数量:<input type="text" v-model="updateNumber" placeholder="请输入需修改书籍数量"></div>
<div>书籍作者:<input type="text" v-model="updateAuthor" placeholder="请输入需修改书籍作者"></div>
<div>
<button type="button" v-on:click="updateclick">提交</button>
<button type="button" v-on:click="updateclickShow1">关闭</button>
</div>
</div>
</div>
</div>
<script>
const requstVueAPP = {
data() {
return{
deleteName:'',
updateName:'',
updateNumber:'',
updateAuthor:'',
addName:'',
addNumber:'',
addAuthor:'',
newlist:[{}],
addIsShow:false,//添加书籍是否显示
updateIsShow:false,//更改书籍时候显示
}
},
mounted() {
//页面加载时启动
var that = this
axios.post("http://localhost:8080/springboot/getbookList")
.then(function(res){
console.log(res.data)
that.newlist = res.data
console.log("获取成功")
}).catch(function(err){
console.log("查询失败")
})
},
methods:{
deleteclick(list){//删除书籍
var that = this
//console.log(list.name)
axios.post("http://localhost:8080/springboot/deleteBook",{
deleteName:list.name,
})
.then(function(res){
console.log("删除成功")
that.newlist = res.data
}).catch(function(err){
console.log("删除失败")
})
},
updateclick(){//修改书籍
var that = this
axios.post("http://localhost:8080/springboot/updateBook",{
updateName: this.updateName,
updateNumber:this.updateNumber,
updateAuthor: this.updateAuthor,
}).then(function(res){
console.log("更新成功")
that.newlist = res.data
}).catch(function(err){
console.log("更新失败")
})
this.updateName=''
this.updateNumber=''
this.updateAuthor=''
this.updateIsShow = false
},
addclick(){//添加书籍
var that = this
console.log(this.addName)
axios.post("http://localhost:8080/springboot/addBook",{
addName: this.addName,
addNumber: this.addNumber,
addAuthor: this.addAuthor,
}).then(function(res){
console.log("添加成功")
that.newlist = res.data
}).catch(function(err){
console.log("添加失败")
})
this.addName=''
this.addNumber=''
this.addAuthor=''
this.addIsShow = false
},
addclickShow(){
this.addIsShow = !this.addIsShow;
this.updateIsShow = false;
},
updateclickShow(list){
if(this.updateIsShow == false)
this.updateIsShow =true
this.addIsShow = false
},
updateclickShow1(){
this.updateIsShow=false
}
}
}
Vue.createApp(requstVueAPP).mount("#bookInfo")
</script>
</body>
</html>
相对来说,代码很简单
攀上一座高山后,你才会发现,原来还有更多的山头等着你。 ——曼德拉