SpringBoot接收前端数据的几种常用方式

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>
  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值