• 前端Get Post Put Delect请求 传参数 不传参数给后端


    Get请求不传参、Get请求传不是实体类的参数、Get请求传实体类的参数

    Post 请求不传参数、Post请求传不是实体类的参数、Post请求传实体类的参数  总是分不清,其中Delect 请求使用的地方很少就先记录Delete请求吧

    Delect 删除

    单个删除

    前端

    1. "text" @click="handleDel(scope.row.id)">删除
    2. /**
    3. * 删除单条数据
    4. */
    5. handleDel(id) {
    6. this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
    7. type: 'warning'
    8. }).then(() => {
    9. del(id).then(res => {
    10. })
    11. })
    12. },

    前端跳后端

    1. /**
    2. * 删除单条数据
    3. * @param id 主键值
    4. */
    5. export function del(id) {
    6. return request({
    7. url:define.api+'/user/' + id,
    8. method: 'DELETE'
    9. })
    10. }

    后端

    1. /**
    2. * 删除
    3. * @param id 主键
    4. */
    5. @DeleteMapping("/{id}")
    6. @DSTransactional
    7. public ActionResult delete(@PathVariable("id") String id){
    8. Userentity = service.getInfo(id);
    9. if(entity!=null){
    10. service.delete(entity);
    11. }
    12. return ActionResult.success("删除成功");
    13. }

    批量删除

    前端

    1. "danger" @click="batchDelete()">批量删除
    2. /**
    3. * 批量删除
    4. */
    5. batchDelete() {
    6. // 判断是否勾选了数据
    7. if (!this.multipleSelection.length) {
    8. this.$message({
    9. type: 'error',
    10. message: '请选择一条数据',
    11. duration: 1500,
    12. })
    13. return
    14. }
    15. let length = this.multipleSelection.length;
    16. let size = this.listQuery.pageSize;
    17. const ids = this.multipleSelection.join();
    18. if (length == size) {
    19. this.$confirm('请问您是选择当页数据还是选择全部数据?', '确认信息', {
    20. distinguishCancelAndClose: true,
    21. confirmButtonText: '全部数据',
    22. cancelButtonText: '当页数据',
    23. type: 'warning',
    24. center: true
    25. }).then(() => {
    26. this.$confirm('您确定要删除全部数据吗, 是否继续?', '提示', {
    27. type: 'warning'
    28. }).then(() => {
    29. // 批量删除全部。
    30. batchDeleteAll().then(res => {
    31. this.$message({
    32. type: 'success',
    33. message: res.msg,
    34. onClose: () => {
    35. this.initData()
    36. }
    37. })
    38. })
    39. }).catch(() => {
    40. })
    41. }).catch(action => {
    42. this.$confirm('您确定要删除当页数据吗, 是否继续?', '提示', {
    43. type: 'warning'
    44. }).then(() => {
    45. //批量删除当页
    46. batchDelete(this.multipleSelection).then(res => {
    47. this.$message({
    48. type: 'success',
    49. message: res.msg,
    50. onClose: () => {
    51. this.initData()
    52. }
    53. })
    54. })
    55. });
    56. });
    57. } else {
    58. this.$confirm('您确定要删除所选数据吗, 是否继续?', '提示', {
    59. type: 'warning'
    60. }).then(() => {
    61. batchDelete(this.multipleSelection).then(res => {
    62. this.$message({
    63. type: 'success',
    64. message: res.msg,
    65. onClose: () => {
    66. this.initData()
    67. }
    68. })
    69. })
    70. });
    71. }
    72. },

    前端跳后端

    1. /**
    2. * 批量删除
    3. */
    4. export function batchDeleteAll() {
    5. return request({
    6. url: api+'/user/batchDeleteAll',
    7. method: 'get'
    8. })
    9. }
    10. /**
    11. * 批量删除
    12. */
    13. export function batchDelete(data) {
    14. return request({
    15. url: api+'/user/batchDelete',
    16. method: 'POST',
    17. data
    18. })
    19. }

    后端

    1. @Slf4j
    2. @RestController
    3. @RequestMapping("/user")
    4. public class UserController {
    5. @PostMapping("/batchDelete")
    6. public ActionResult batchDelete(@RequestBody List ids) {
    7. boolean bool = mapper.batchDelete(ids);
    8. if (bool) {
    9. return ActionResult.success("删除成功");
    10. } else {
    11. return ActionResult.fail("删除失败");
    12. }
    13. }
    14. @GetMapping("/batchDeleteAll")
    15. public ActionResult batchDeleteAll() {
    16. boolean bool = mapper.batchDeleteAll();
    17. if (bool) {
    18. return ActionResult.success("删除成功");
    19. } else {
    20. return ActionResult.fail("删除失败");
    21. }
    22. }
    23. }
    1. boolean batchDeleteAll();
    2. <update id="batchDeleteAll">
    3. update user set IZ_DEL=1
    4. update>
    5. boolean batchDelete(List<String> ids);
    6. <update id="batchDelete">
    7. update user set IZ_DEL=1 where ID in
    8. <foreach collection="list" open="(" close=")" item="j" separator=",">
    9. #{j}
    10. foreach>
    11. update>

    Get请求

    Get请求加一个不是实体类的参数--通过id 查详情

    前端

    1. /**
    2. * 初始化表单数据
    3. * @param id 主键值
    4. * @param isDetail 是否是详情页面,控制是否可编辑
    5. */
    6. init(id, isDetail) {
    7. this.dataForm.id = id || 0;
    8. this.visible = true;
    9. this.isDetail = isDetail || false;
    10. this.$nextTick(() => {
    11. this.$refs['elForm'].resetFields();
    12. if (this.dataForm.id) {
    13. this.loading = true
    14. //加载表单数据
    15. getInfoToEdit(this.dataForm.id).then(res => {
    16. this.dataInfo(res.data)
    17. this.loading = false
    18. })
    19. } else {
    20. this.clearData(this.dataForm)
    21. }
    22. });
    23. this.$store.commit('generator/UPDATE_RELATION_DATA', {})
    24. },

    前端跳后端

    1. /**
    2. * 获取表单详细(编辑页面调用)
    3. */
    4. export function getInfoToEdit(id) {
    5. return request({
    6. url: define.api+'/user/' + id,
    7. method: 'GET'
    8. })
    9. }

    后端

    1. @RestController
    2. @RequestMapping("/user")
    3. public class UserController {
    4. /**
    5. * 根据id获取表单信息(编辑表单)
    6. * @param id 主键
    7. */
    8. @GetMapping("/{id}")
    9. public ActionResult info(@PathVariable("id") String id){
    10. User entity = service.getInfo(id);
    11. UserVo vo = JsonUtil.getJsonToBean(entity, UserVo.class);
    12. return ActionResult.success(vo);
    13. }
    14. }

    Get请求加多个不是实体类的参数--前端有模糊查询

    前端

    1. //执行情况
    2. getExecutionRepairMonth(startDay,endDay){
    3. getExecutionRepairMonth(startDay,endDay).then(res=>{
    4. this.listData = res.data.map(item=>{
    5. return {name:item.name,value:item.num}
    6. });
    7. })
    8. },

    前端跳后端

    1. export function getExecution(startDay,endDay) {
    2. return request({
    3. url: define.api + `/repairMonth/getExecution?startDay=${startDay}&endDay=${endDay}`,
    4. method: 'get',
    5. })
    6. }

    后端

    1. @RestController
    2. @RequestMapping("/repairMonth")
    3. public class RepairDayController {
    4. /**
    5. * @description: 通过检修计划编号 获取详情
    6. */
    7. @GetMapping("/getExecution")
    8. public ActionResult getExecution(String startDay,String endDay){
    9. }
    10. }

    Post请求

    Post请求加一个是实体类的参数--前端有模糊查询返回List

    前端

    1. /**
    2. * 初始化加载列表数据
    3. */
    4. initData() {
    5. this.listLoading = true;
    6. let _query = {
    7. ...this.listQuery,
    8. ...this.query,
    9. menuId: this.menuId
    10. };
    11. // 调用查询列表数据api接口
    12. listOLoadAnalysis(_query).then(res => {
    13. var _list =[];
    14. for(let i=0;idata.list.length;i++){
    15. let _data = res.data.list[i];
    16. _list.push(_data)
    17. }
    18. this.list = _list
    19. this.total = res.data.pagination.total
    20. this.listLoading = false
    21. })
    22. },

    前端跳后端

    1. /**
    2. * 查询列表数据
    3. * @param data 查询条件参数对象
    4. */
    5. export function listOLoadAnalysis(data) {
    6. return request({
    7. url: define.api+'/user/getList',
    8. method: 'POST',
    9. data
    10. })
    11. }

    后端

    1. @RestController
    2. @RequestMapping("/user")
    3. public class UserController {
    4. /**
    5. * 列表 表数据获取(带分页)
    6. * @param userQueryVO 查询条件对象
    7. */
    8. @PostMapping("/getList")
    9. public ActionResult list(@RequestBody UserQueryVO userQueryVO ){
    10. }
    11. }

    Put请求

    Put请求携带实体类的参数---更新数据保存接口

    前端

    1. "primary" @click="dataFormSubmit()" v-if="!isDetail"> 确 定
    2. /**
    3. * 表单提交方法,会先进行表单数据校验
    4. */
    5. dataFormSubmit() {
    6. this.$refs['elForm'].validate((valid) => {
    7. if (valid) {
    8. this.request()
    9. }
    10. })
    11. },
    12. /**
    13. * 表单提交调用api接口方法
    14. */
    15. request() {
    16. var _data = this.dataList()
    17. if (!this.dataForm.id) {
    18. // 表单新增保存
    19. addOLoadAnalysis(_data).then((res) => {
    20. this.$message({
    21. message: res.msg,
    22. type: 'success',
    23. duration: 1000,
    24. onClose: () => {
    25. this.visible = false
    26. this.$emit('refresh', true)
    27. }
    28. })
    29. })
    30. } else {
    31. // 表单修改保存
    32. updateOLoadAnalysis(this.dataForm.id, _data).then((res) => {
    33. this.$message({
    34. message: res.msg,
    35. type: 'success',
    36. duration: 1000,
    37. onClose: () => {
    38. this.visible = false
    39. this.$emit('refresh', true)
    40. }
    41. })
    42. })
    43. }
    44. },

    前端跳后端

    1. /**
    2. * 新增表单保存数据
    3. * @param data 提交的表单对象
    4. */
    5. export function addOLoadAnalysis(data) {
    6. return request({
    7. url: define.api+'/user',
    8. method: 'POST',
    9. data
    10. })
    11. }
    12. /**
    13. * 修改表单保存数据
    14. * @param data 提交的表单对象
    15. */
    16. export function updateOLoadAnalysis(id, data) {
    17. return request({
    18. url: define.api+'/user/' + id,
    19. method: 'PUT',
    20. data
    21. })
    22. }

    后端

    1. @RestController
    2. @RequestMapping("/user")
    3. public class UserController {
    4. /**
    5. * 更新数据保存接口
    6. *
    7. * @param id 主键
    8. * @param oLoadAnalysisVO 表单信息
    9. */
    10. @PutMapping("/{id}")
    11. @DSTransactional
    12. public ActionResult update(@PathVariable("id") String id,@RequestBody @Valid UserVo userVo){
    13. }
    14. }
    
                    
  • 相关阅读:
    JuiceFS分布式文件系统源码分析(Java层)
    设计模式:外观模式(C#、JAVA、JavaScript、C++、Python、Go、PHP)
    (1)安装hadoop之虚拟机准备(配置IP与主机名)
    [Unity3D]用C#在unity里面写一个简单的红绿灯
    敏捷.敏捷项目管理第二版.Jim Highsmith
    【LeetCode】36、有效的数独
    【C++初阶(二)】缺省参数&函数重载
    美团前端二面常考react面试题(附答案)
    激发创新,助力研究:CogVLM,强大且开源的视觉语言模型亮相
    JavaScript 正则表达式
  • 原文地址:https://blog.csdn.net/xy58451921/article/details/133383107