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

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;
    }

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

数据成功修改

相关推荐
Swift社区5 分钟前
LeetCode 377 组合总和 Ⅳ
算法·leetcode·职场和发展
漫随流水5 分钟前
leetcode算法(404.左叶子之和)
数据结构·算法·leetcode·二叉树
wanghu20245 分钟前
ABC440_D题_题解
算法
Tisfy6 分钟前
LeetCode 2975.移除栅栏得到的正方形田地的最大面积:暴力枚举所有可能宽度
算法·leetcode·题解·模拟·暴力
啊阿狸不会拉杆6 分钟前
《数字图像处理》第 1 章 绪论
图像处理·人工智能·算法·计算机视觉·数字图像处理
Loo国昌7 分钟前
【LangChain1.0】第二篇 快速上手实战
网络·人工智能·后端·算法·microsoft·语言模型
BHXDML8 分钟前
第二章:决策树与集成算法
算法·决策树·机器学习
橘颂TA9 分钟前
【剑斩OFFER】算法的暴力美学——力扣 692 题:前 K 个高频单词
网络·算法·leetcode·哈希算法·结构与算法
练习时长一年9 分钟前
LeetCode热题100(乘积最大子序列)
数据结构·算法·leetcode
仰泳的熊猫10 分钟前
题目1109:Hanoi双塔问题
数据结构·c++·算法·蓝桥杯