Web项目实战 | 购物系统v2.0 | 开发记录(九)Controller层返回数据的封装 | 商品批量操作 | 五表联立实现商品搜索

本文介绍了在Web项目实战中,如何设计Controller层返回数据的Bean,以及如何实现商品的批量操作功能,包括前端AJAX请求和后端处理。此外,还讲解了五表联立查询实现商品搜索的技巧,利用MyBatis和Map参数进行多条件查询。
摘要由CSDN通过智能技术生成

——若发现文章内容有误,敬请指正,望不吝赐教,感谢!

以往记录


Web项目实战 | 购物系统v2.0 | 开发记录(一)需求分析 | 技术选型 | 系统设计 | 数据表设计 | SpringBoot、SSM、Thymeleaf、Bootstrap…

Web项目实战 | 购物系统v2.0 | 开发记录(二)搭建SpringBoot+SSM框架环境 | 配置Druid+MyBatis | 基于Bootstrap实现登陆页面| 图片验证码接口

Web项目实战 | 购物系统v2.0 | 开发记录(三)分页显示 | 根据商品名称进行模糊查询

Web项目实战 | 购物系统v2.0 | 开发记录(四)单个页面单个请求解决根据商品类型进行分页查询 | 使用省市区三级联动 | 使用JQuery 插件实现图片上传

Web项目实战 | 购物系统v2.0 | 开发记录(五)| 使用base64编码实现头像修改 | 用户个人信息修改 | JQuery动态提示

Web项目实战 | 购物系统v2.0 | 开发记录(六)商品详情页面 | 游客访问主页 | 运用Bootstrap4轻量级弹窗实现提示

Web项目实战 | 购物系统v2.0 | 开发记录(七)SpringBoot整合Shiro框架进行身份认证 | Shiro 加盐(MD5+Salt)验证登陆 | 数据表结构优化避免外键+设置中间表

Web项目实战 | 购物系统v2.0 | 开发记录(八)前后端分离初步思路 | JS处理URL参数实用函数 | AJAX 向后台传递Map类型数据 | MyBatis多表查询优化

运行环境


windows10
IDEA 2021.3 专业版
JDK8
SpringBoot2.6.x
JQuery 3.x
Druid 1.2.5
Bootstrap 4.6.0
MySQL 8
Navicat 15

一、设计Bean用于Controller层返回数据


在之前的学习中用到了伪前后端分离的思路,前端通过像后端AJAX请求获取数据,然后再渲染数据到页面,在数据之间传递时通常需要对数据类型进行序列化,为了方便Controller层返回数据,这里使用泛型进行设计,设计实体类RestResponse,用于返回结果,同时设置一个支持返回JSON字符串的方法,toJSONString()

package com.uni.base;

import com.alibaba.fastjson.JSON;

/*
 * @Description: 自定义响应数据结构
 */
public class RestResponse<T> {
   
    private int code;
    private String message;
    private T response;

    public RestResponse(int code, String message) {
   
        this.code = code;
        this.message = message;
    }

    public RestResponse(int code, String message, T response) {
   
        this.code = code;
        this.message = message;
        this.response = response;
    }

    public static RestResponse fail(Integer code, String msg) {
   
        return new RestResponse<>(code, msg);
    }
    public static RestResponse fail(String msg) {
   
        return new RestResponse<>(-1, msg);
    }

    public static RestResponse ok() {
   
        SystemCodeEnum systemCodeEnum = SystemCodeEnum.OK;
        return new RestResponse<>(systemCodeEnum.getCode(), systemCodeEnum.getMessage());
    }
    public static RestResponse ok(String message) {
   
        SystemCodeEnum systemCodeEnum = SystemCodeEnum.OK;
        return new RestResponse<>(systemCodeEnum.getCode(), message);
    }

    /**
     * @param <F>      the type parameter
     * @param response the response
     * @return the rest response
     */
    public static <F> RestResponse<F> ok(F response) {
   
        SystemCodeEnum systemCodeEnum = SystemCodeEnum.OK;
        return new RestResponse<>(systemCodeEnum.getCode(), systemCodeEnum.getMessage(), response);
    }

    public int getCode() {
   
        return code;
    }

    public void setCode(int code) {
   
        this.code = code;
    }

    public String getMessage() {
   
        return message;
    }


    public void setMessage(String message) {
   
        this.message = message;
    }

    public T getResponse() {
   
        return response;
    }

    public void setResponse(T response) {
   
        this.response = response;
    }

    @Override
    public String toString() {
   
        return "RestResponse{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", response=" + response +
                '}';
    }

    public String toJSONString(){
   
        return JSON.toJSONString(this);
    }
}

在返回消息时,有许多是比较关键的信息,比如返回的状态、返回的信息等,所以可以设计一个枚举类,来表示返回的状态,以及对应的信息

package com.uni.base;

public enum SystemCodeEnum {
   
 
    OK(1, "成功"),
    AccessTokenError(400, "用户登录令牌失效"),
    UNAUTHORIZED(401, "用户未登录"),
    AuthError(402, "用户名或密码错误"),
    InnerError(500, "系统内部错误"),
    ParameterValidError(501, "参数验证错误"),
    AccessDenied(502, "用户没有权限访问");

    int code;
    String message;

    SystemCodeEnum(
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值