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

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

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

数据成功修改

相关推荐
点云SLAM17 分钟前
Eigen中Dense 模块简要介绍和实战应用示例(最小二乘拟合直线、协方差矩阵计算和稀疏求解等)
线性代数·算法·机器学习·矩阵·机器人/slam·密集矩阵与向量·eigen库
renhongxia134 分钟前
大模型微调RAG、LORA、强化学习
人工智能·深度学习·算法·语言模型
DdduZe1 小时前
8.19作业
数据结构·算法
PyHaVolask1 小时前
链表基本运算详解:查找、插入、删除及特殊链表
数据结构·算法·链表
高山上有一只小老虎1 小时前
走方格的方案数
java·算法
吧唧霸1 小时前
golang读写锁和互斥锁的区别
开发语言·算法·golang
花火|3 小时前
算法训练营day55 图论⑤ 并查集理论基础、107. 寻找存在的路径
算法·图论
花火|3 小时前
算法训练营day56 图论⑥ 108. 109.冗余连接系列
算法·图论
上海迪士尼353 小时前
力扣子集问题C++代码
c++·算法·leetcode