springboot企业级抽奖项目业务二(用户模块)

书接上回,梅开二度

开发流程

该业务基于rouyi生成好了mapper和service的代码,现在需要在controller层写接口

实际操作流程:

看接口文档一>controller里定义函数一>看给出的工具类一>补全controller里的函数一>运行测试

接口文档

在用户模块有用户信息和查看奖品方法

用户信息分为成功访问和登录超时两种情况,使用CardUserDto传输

成功访问用户信息才能查看我的奖品,使用PageBean<ViewCardUserHit>传输

controller里定义

在注解框架里加上info和hit两个函数

@RestController
@RequestMapping(value = "/api/user")
@Api(tags = {"用户模块"})
public class UserController {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ViewCardUserHitService hitService;
    @Autowired
    private GameLoadService loadService;

    @GetMapping("/info")
    @ApiOperation(value = "用户信息")
    public ApiResult info(HttpServletRequest request) {
        //TODO
        return null;
    }

    @GetMapping("/hit/{gameid}/{curpage}/{limit}")
    @ApiOperation(value = "我的奖品")
    @ApiImplicitParams({
            @ApiImplicitParam(name="gameid",value = "活动id(-1=全部)",dataType = "int",example = "1",required = true),
            @ApiImplicitParam(name = "curpage",value = "第几页",defaultValue = "1",dataType = "int", example = "1"),
            @ApiImplicitParam(name = "limit",value = "每页条数",defaultValue = "10",dataType = "int",example = "3")
    })
    public ApiResult hit(@PathVariable int gameid,@PathVariable int curpage,@PathVariable int limit,HttpServletRequest request) {
        //TODO
        return null;
    }
}

看给出的工具类

找该模块附近的common和util包

这个PageBean不太常见,没用分页插件而选择自己定义分页类,用于表示分页信息。该类包含了当前页、每页条数、总条数、是否有下一页、总页数、开始索引和本页数据等属性,并提供了构造方法和获取属性值的方法。

@ApiModel("分页信息")
public class PageBean<T> {
    @ApiModelProperty(value = "当前页,1开始")
    private long currentPage = 1;
    @ApiModelProperty(value = "每页条数,默认10")
    private long pageSize = 10;
    @ApiModelProperty(value = "总条数")
    private long totalNum;
    @ApiModelProperty(value = "是否有下一页")
    private Integer isMore;
    @ApiModelProperty(value = "总页数")
    private long totalPage;
    @ApiModelProperty(value = "开始索引")
    private long startIndex;
    @ApiModelProperty(value = "本页数据")
    private List<T> items;

    public PageBean() {
        super();
    }

    public PageBean(long currentPage, long pageSize, long totalNum, List<T> data) {
        super();
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalNum = totalNum;
        this.totalPage = Math.toIntExact((this.totalNum + this.pageSize - 1) / this.pageSize);
        this.startIndex = (this.currentPage-1)*this.pageSize;
        this.isMore = this.currentPage >= this.totalPage ? 0 : 1;
        this.items = data;
    }

    public PageBean(Page<T> page) {
        this.currentPage = page.getCurrent();
        this.pageSize = page.getSize();
        this.totalNum = page.getTotal();
        this.totalPage = page.getPages();
        this.startIndex = (this.currentPage - 1) * this.pageSize;
        this.isMore = page.hasNext() ? 1 : 0;
        this.items = page.getRecords();
    }

    public long getCurrentPage() {
        return currentPage;
    }

    public long getPageSize() {
        return pageSize;
    }

    public long getTotalNum() {
        return totalNum;
    }

    public Integer getIsMore() {
        return isMore;
    }

    public long getTotalPage() {
        return totalPage;
    }

    public long getStartIndex() {
        return startIndex;
    }

    public List<T> getItems() {
        return items;
    }
}

在项目中并不一定要依赖第三方的分页插件,而是可以手写一个简单的分页类来处理分页逻辑,原因如下:

  1. 轻量级需求:项目中对分页功能的需求比较简单,没有复杂的分页逻辑或特殊要求,使用自定义的简单分页类可以满足需求,避免引入过多不必要的依赖。

  2. 定制化需求:自定义的分页类可以根据项目实际情况进行定制,符合项目的特定需求和设计风格,更灵活地控制分页逻辑。

  3. 减少外部依赖:通过自定义分页类,可以减少项目对第三方分页插件的依赖,减小项目的复杂度和维护成本。

补全controller里的函数

info部分

代码相当简单,从session里取user,如果是空就说明登录超时了,有的话就转为dto,使用操作活动表的GameLoadService装填dto并返回。

public ApiResult info(HttpServletRequest request) {
    HttpSession session = request.getSession();
    CardUser user = (CardUser) session.getAttribute("user");
    if (user == null) {
        return new ApiResult(0, "登录超时", null);
    } else {
        CardUserDto dto = new CardUserDto(user);
        dto.setGames(loadService.getGamesNumByUserId(user.getId()));
        dto.setProducts(loadService.getPrizesNumByUserId(user.getId()));
        return new ApiResult(1, "成功", dto);
    }
}

hit部分

也是从session里取user,QueryWrapper 是用于构建查询条件的类,QueryWrapper<ViewCardUserHit> 则是基于泛型的 QueryWrapper,告诉 QueryWrapper 这个查询条件是针对 ViewCardUserHit 类型的,这样在创建查询条件时就会根据 ViewCardUserHit 类的属性来构建条件,避免了因使用错误实体类而导致的运行时异常。

wrapper.eq("userid", user.getId());设置查询条件为查询实体中 userid 字段等于 user.getId() 的记录。

hitService.page(new Page(curpage, limit), wrapper) 的作用是调用 hitService 中的 page 方法来执行查询操作。这个方法接受两个参数:一个是 Page 对象,用于指定当前页数和每页显示的记录数;另一个是 QueryWrapper 对象 wrapper,用于指定查询条件。

public ApiResult hit(@PathVariable int gameid, @PathVariable int curpage, @PathVariable int limit, HttpServletRequest request) {
    HttpSession session = request.getSession();
    CardUser user = (CardUser) session.getAttribute("user");
    QueryWrapper<ViewCardUserHit> wrapper = new QueryWrapper<>();
    wrapper.eq("userid", user.getId());
    if (gameid != -1) {
        wrapper.eq("gameid", gameid);
    }
    Page<ViewCardUserHit> all = hitService.page(new Page(curpage, limit), wrapper);
    return new ApiResult(1, "成功", new PageBean<ViewCardUserHit>(all));
}

完整代码

@RestController
@RequestMapping(value = "/api/user")
@Api(tags = {"用户模块"})
public class UserController {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ViewCardUserHitService hitService;
    @Autowired
    private GameLoadService loadService;

    @GetMapping("/info")
    @ApiOperation(value = "用户信息")
    public ApiResult info(HttpServletRequest request) {
        HttpSession session = request.getSession();
        CardUser user = (CardUser) session.getAttribute("user");
        if (user == null) {
            return new ApiResult(0,"登录超时", null);
        } else {
            CardUserDto dto = new CardUserDto(user);
            dto.setGames(loadService.getGamesNumByUserId(user.getId()));
            dto.setProducts(loadService.getPrizesNumByUserId(user.getId()));
            return new ApiResult(1,"成功",dto);
        }
    }

    @GetMapping("/hit/{gameid}/{curpage}/{limit}")
    @ApiOperation(value = "我的奖品")
    @ApiImplicitParams({
            @ApiImplicitParam(name="gameid",value = "活动id(-1=全部)",dataType = "int",example = "1",required = true),
            @ApiImplicitParam(name = "curpage",value = "第几页",defaultValue = "1",dataType = "int", example = "1"),
            @ApiImplicitParam(name = "limit",value = "每页条数",defaultValue = "10",dataType = "int",example = "3")
    })
    public ApiResult hit(@PathVariable int gameid,@PathVariable int curpage,@PathVariable int limit,HttpServletRequest request) {
        HttpSession session = request.getSession();
        CardUser user = (CardUser) session.getAttribute("user");
        QueryWrapper<ViewCardUserHit> wrapper = new QueryWrapper<>();
        wrapper.eq("userid",user.getId());
        if (gameid == -1) {
            wrapper.eq("gameid",gameid);
        }
        Page<ViewCardUserHit> all = hitService.page(new Page(curpage,limit),wrapper);
        return new ApiResult(1,"成功",new PageBean<ViewCardUserHit>(all));
    }
}

运行测试

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值