我的第一个项目:分数和生命值的更新(后端增删查改的“改“)

1.前端先把测试样例写好

随便写一个测试样例

复制代码
<template>
  <div>
    <div ref="stage"></div>
    <button @click="http">网络请求测试</button>
  </div>
</template>
  
<script>
import { canvas, main_1 } from "panghu-planebattle-esm"
import bus from '../js/eventBus'
export default {
  data() {
    return {
      player: {
        id:'',
        loginName: 123456,
        life: 100,
        score: score,
      },
    }
  },
  methods:{
    http(){
      setInterval(() => {
      this.axios.post('http://localhost:3312/sys-user/update', this.player)
        .then((resp) => {

          console.log("this is update", resp);
          let data = resp.data;
          //
          if (data.success) {
            console.log({
              message: '修改成功',
              type: 'success'
            });
          }
        })
    }, 5000)
    }
  },

(确实是非常朴实无华的测试样例)

2.随后我们来到后端

来到controller类中添加接口

复制代码
    @PostMapping("update")
    public CommonResp update(@RequestBody SysUserUpdateReq req){
//        zxcv1234
        CommonResp resp = new CommonResp<>();
        sysUserService.update(req);
        return resp;
    }

3.在req文件下添加一个SysUserUpdateReq类

因为这个我们只做对数据的更新,所以只用LoginName,life,score就可以了

复制代码
package com.wulaoda.loginhouduan.req;

public class SysUserUpdateReq {
    private String LoginName;

    private int life;

    private int score;

    public String getLoginName() {
        return LoginName;
    }

    public void setLoginName(String loginName) {
        LoginName = loginName;
    }

    public int getLife() {
        return life;
    }

    public void setLife(int life) {
        this.life = life;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "SysUserUpdateReq{" +
                "LoginName='" + LoginName + '\'' +
                ", life=" + life +
                ", score=" + score +
                '}';
    }
}

4.编写业务

这里Mybatis-plus提供的update方法

嘶,参数看不懂

然后,改怎么写啊...不会啊...

必应我来了

mybatis-plus入门学习-BaseMapper - 掘金 (juejin.cn)

mybatis-plus update更新操作的三种方式_mybatisplus的uodate_波神小波的博客-CSDN博客

直接就对着抄

复制代码
//数据更新
    @Override
    public SysUserUpdateResp update(SysUserUpdateReq req){//重写
          //网上的例子
//        LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//        lambdaUpdateWrapper.eq(User::getName, "rhb").set(User::getAge, 18);
//        Integer rows = userMapper.update(null, lambdaUpdateWrapper);
          LambdaUpdateWrapper<SysUserEntity> wrapper1 = new LambdaUpdateWrapper<>();
          wrapper1.eq(SysUserEntity::getLoginName, req.getLoginName()).set(SysUserEntity::getLife, req.getLife());
          sysUserMapper.update(null,wrapper1);
          return null;
    }

前端点下按钮,开始测试,

数据成功修改

相关推荐
橙几14 分钟前
击败了90%的解法?Two Sum 从 O(n²) 到 O(n) 的优化之路
算法
叶子爱分享28 分钟前
经典排序算法之归并排序(Merge Sort)
算法·排序算法
珹洺34 分钟前
C++算法竞赛篇:DevC++ 如何进行debug调试
java·c++·算法
呆呆的小鳄鱼1 小时前
leetcode:冗余连接 II[并查集检查环][节点入度]
算法·leetcode·职场和发展
墨染点香1 小时前
LeetCode Hot100【6. Z 字形变换】
java·算法·leetcode
沧澜sincerely1 小时前
排序【各种题型+对应LeetCode习题练习】
算法·leetcode·排序算法
CQ_07121 小时前
自学力扣:最长连续序列
数据结构·算法·leetcode
弥彦_1 小时前
cf1925B&C
数据结构·算法
YuTaoShao2 小时前
【LeetCode 热题 100】994. 腐烂的橘子——BFS
java·linux·算法·leetcode·宽度优先
Wendy14419 小时前
【线性回归(最小二乘法MSE)】——机器学习
算法·机器学习·线性回归