浪花 - 组队功能后端开发

一、基础增删改查功能

  1. 添加队伍
java 复制代码
    @PostMapping("/add")
    public BaseResponse<Boolean> addTeam(@RequestBody Team team) {
        if (team == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = teamService.save(team);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "添加失败");
        }
        return ResultUtils.success(result);
    }
  1. 删除队伍
java 复制代码
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTeamById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = teamService.removeById(id);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return ResultUtils.success(result);
    }
  1. 修改 / 更新队伍信息
java 复制代码
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeam(@RequestBody Team team) {
        if (team == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = teamService.updateById(team);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "更新失败");
        }
        return ResultUtils.success(result);
    }
  1. 查找队伍
java 复制代码
    @GetMapping("/get")
    public BaseResponse<Team> getTeamById(long teamId) {
        if (teamId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Team team = teamService.getById(teamId);
        if (team == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        return ResultUtils.success(team);
    }

二、列表查询

  1. 封装请求包装类
java 复制代码
package com.example.usercenter.model.dto;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.example.usercenter.common.PageRequest;
import lombok.Data;


/**
 * @author 乐小鑫
 * @version 1.0
 * @Date 2024-01-22-20:14
 */
@Data
public class TeamQuery extends PageRequest {
    private static final long serialVersionUID = -8434935321943948180L;

    /**
     * id
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 队伍名称
     */
    private String name;

    /**
     * 最大人数
     */
    private Integer maxNum;

    /**
     * 用户id
     */
    private Long userId;

    /**
     * 0 - 公开,1 - 私有,2 - 加密
     */
    private Integer status;
}
  1. 创建查询构造器

  2. 执行列表查询

java 复制代码
    @GetMapping("/list")
    public BaseResponse<List<Team>> listTeams(TeamQuery teamQuery) {
        if (teamQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Team team = new Team();
        BeanUtils.copyProperties(teamQuery, team);
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>();
        List<Team> teamList = teamService.list(queryWrapper);
        return ResultUtils.success(teamList);
    }

三、分页查询

  1. 通用分页查询请求参数
java 复制代码
package com.example.usercenter.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 通用分页查询参数
 * @author 乐小鑫
 * @version 1.0
 * @Date 2024-01-22-20:16
 */
@Data
public class PageRequest implements Serializable {

    private static final long serialVersionUID = 1395844225639844641L;

    /**
     * 页面大小
     */
    private int pageSize = 10;

    /**
     * 当前页数
     */
    private int pageNum = 1;

}
  1. 创建查询构造器

  2. 执行分页查询

java 复制代码
    @GetMapping("/list/page")
    public BaseResponse<Page<Team>> listTeamsByPage(TeamQuery teamQuery) {
        if (teamQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<Team> page = new Page<>(teamQuery.getPageNum(),teamQuery.getPageSize());
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>();
        Page<Team> resultPage = teamService.page(page, queryWrapper);
        return ResultUtils.success(resultPage);
    }

四、请求参数包装类和包装类

  1. 为什么需要请求参数包装类?
  • 请求参数名称 / 类型和实体类型不一致
  • 有一些参数用不到,如果要生成接口文档,会增加理解成本
  • 多个实体类映射到一个对象的场景
  1. 为什么需要(响应数据)包装类?
  • 有些数据需要隐藏,不能返回给前端(封装 VO 类)
  • 有些字段某些方法不关心
相关推荐
Coder_Boy_2 分钟前
基于SpringAI的在线考试系统-0到1全流程研发:DDD、TDD与CICD协同实践
java·人工智能·spring boot·架构·ddd·tdd
sheji34168 分钟前
【开题答辩全过程】以 面向高校校园的物物交换系统设计与实现为例,包含答辩的问题和答案
java·eclipse
科技块儿20 分钟前
IP定位技术:游戏反外挂体系中的精准识别引擎
数据库·tcp/ip·游戏
衫水24 分钟前
[特殊字符] MySQL 常用指令大全
数据库·mysql·oracle
卓怡学长30 分钟前
m115乐购游戏商城系统
java·前端·数据库·spring boot·spring·游戏
2501_9445264233 分钟前
Flutter for OpenHarmony 万能游戏库App实战 - 蜘蛛纸牌游戏实现
android·java·python·flutter·游戏
打工的小王1 小时前
java并发编程(三)CAS
java·开发语言
老陈聊架构1 小时前
『AI辅助Skill』掌握三大AI设计Skill:前端独立完成产品设计全流程
前端·人工智能·claude·skill
小句1 小时前
SQL中JOIN语法详解 GROUP BY语法详解
数据库·sql
尤老师FPGA1 小时前
使用ZYNQ芯片和LVGL框架实现用户高刷新UI设计系列教程(第四十五讲)
android·java·ui