2

我的第一个项目(十二) :分数和生命值的更新(后端增删查改的"改") - 养肥胖...

 1 year ago
source link: https://www.cnblogs.com/FatTiger4399/p/17360339.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

好家伙,写后端,这多是一件美逝.

关于这个项目的代码前面的博客有写 

我的第一个独立项目 - 随笔分类 - 养肥胖虎 - 博客园 (cnblogs.com)

现在,我们登陆进去了,我开始和敌人战斗,诶,打到一百分了,我现在要把这个分数保存起来

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)
    }
  },

2501855-20230427215234448-825166537.png

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

2.随后我们来到后端

来到controller类中添加接口

2501855-20230427215437014-1051289478.png
    @PostMapping("update")
    public CommonResp update(@RequestBody SysUserUpdateReq req){
//        zxcv1234
        CommonResp resp = new CommonResp<>();
        sysUserService.update(req);
        return resp;
    }

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

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

2501855-20230427215725006-779008028.png
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方法

嘶,参数看不懂

2501855-20230427215953659-407566950.png

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

必应我来了

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

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

直接就对着抄

2501855-20230427215805468-790318744.png
//数据更新
    @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;
    }

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

2501855-20230427220233167-731465390.png

 数据成功修改


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK