1.@PostMapping请求实现添加
在实现添加时,前台一般会传一个json对象给后台,这个对象不能直接使用,需要进行转化,使用@RequestBody
注解即可实现。
其中@RequestBody
的常用方式如下链接:
@RequestBody注解常见的使用场景和使用方式
addPost() {
if (this.post.name == null || this.post.name == "") {
this.$message.error('职位不能为空!');
} else {
this.postRequest("/system/basic/post/addPost", this.post).then(resp => {
if (resp) {
this.initPost();
this.post.name = "";
}
})
}
}
@PostMapping("/addPost")
public RespBean addPosition(@RequestBody Position position) {
int num = positionService.addPosition(position);
if (num == 1) {
return RespBean.ok("添加成功!");
} else {
return RespBean.error("添加失败!");
}
}
2.@PutMapping请求实现修改
修改操作时,json对象的转化是一样的,同post请求,使用@RequestBody
注解。
doEditPost() {
this.putRequest("/system/basic/post/editPost", this.editPost).then(resp => {
if (resp) {
this.dialogFormVisible = false;
this.initPost();
}
})
}
@PutMapping("/editPost")
public RespBean editPostById(@RequestBody Position position) {
if (positionService.editPostById(position) == 1) {
return RespBean.ok("修改成功!");
} else {
return RespBean.error("修改失败!");
}
}
3.1 DeleteMapping请求实现单点删除
实现删除操作一般需要一个id即可,此时可以在url上将id传到后台即可,不需要通过json的形式传值。后台接收使用@PathVariable
注解。
handleDelete(index, data) {
this.$confirm('此操作将永久删除[' + data.name + ']职位, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.deleteRequest("/system/basic/post/" + data.id).then(resp => {
if (resp) {
this.initPost()
}
})
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
});
});
}
@DeleteMapping("/{id}")
public RespBean deletePostById(@PathVariable Integer id) {
int num = positionService.deletePostById(id);
if (num == 1) {
return RespBean.ok("删除成功!");
} else {
return RespBean.error("删除失败!");
}
}
3.2 DeleteMapping请求实现批量删除
实现批量删除需要获得要删除记录的所有id,可以存放在数组中。
前台可以直接封装一个id数组传到后台,在数据库进行批量操作。
//批量删除前台
handleManyDelete() {
this.$confirm('此操作将永久删除[' + this.multipleSelection.length + ']条记录, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
//单引号表示变量
let ids = '?';
this.multipleSelection.forEach(item=>{
ids+='ids='+item.id+'&';
})
this.deleteRequest("/system/basic/post/many" + ids).then(resp => {
if (resp) {
this.initPost()
}
})
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
});
});
}
//批量删除后台控制层
@DeleteMapping("/many")
public RespBean deleteManyPost(Integer[] ids) {
if (positionService.deleteManyPost(ids) == ids.length) {
return RespBean.ok("批量删除成功!");
} else {
return RespBean.error("批量删除失败!");
}
}
//批量删除数据库操作,使用的mybatis。
<delete id="deleteManyPost">
delete from position
where id in (
<foreach collection="ids" item="id" separator=",">
#{id}
</foreach>)
</delete>