状态码转文字!!!(表格数字转文字)

1、应用场景:在我们的数据库表中经常会有status这个字段,这个字段经常表示此类商品的状态,例如:0->删除,1->上架,0->下架,等等。

2、我们返回给前端数据时,如果在页面显示0、1、2,显然是不合适的。 这时就需要我们定义一个枚举类来解决这个问题。

2.1 写一个枚举,用于描述状态的意思
java 复制代码
package com.by.enmus;

import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.Optional;

/**
 * <p>Project: wms-root - ProduceStatus</p>
 * <p>Powered by scl On 2024-02-27 14:54:47</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
public enum ProductStatus {
    SALE(1, "上架"),
    OFF_SALE(2, "下架"),
    DELETE(0, "删除");

    @Setter
    @Getter
    private Integer code; //状态
    @Setter
    @Getter
    private String desc; //状态描述

    ProductStatus(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static ProductStatus findByCode(Integer code) {
        //jdk1.8提供了哪些新特性
        Optional<ProductStatus> optional = Arrays.stream(ProductStatus.values()).filter(item -> item.getCode().equals(code)).findFirst();
        /*
        if (optional.isPresent()){
            return optional.get();
        }
        return null;
        */
        return optional.orElse(null);
    }
}
2.2 在商品的pojo 里对状态码(status)做个增强 statusx
java 复制代码
/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.model;

import com.by.enmus.ProductStatus;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>Project: pages - Product</p>
 * <p>Powered by scl On 2024-02-18 15:56:32</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
@Data
public class Product extends BaseModel {
    private Integer id;
    private String name;
    private String subName;
    private Integer categoryId;
    private String img;
    //status默认0为删除
    private Integer status = 1;
    private String statusX;

    // 根据状态码获取状态值返回给前端,前端直接使用statusX来显示状态值
    public String getStatusX() {
        ProductStatus productStatus = ProductStatus.findByCode(this.status);
        if (productStatus != null) {
            return productStatus.getDesc();
        }
        return "未定义";
    }
    private BigDecimal price;
    private String brief;
    private Integer seq;
    private String tags;
    private String lastUpdateBy;

}

3、前端只需要渲染statusX这个属性值就可以了

4、附加:这时也许你在查询时会出现一个bug,状态为0 的也能显示出来,这就需要修改我们的sql语句了。只需将状态status值大于或不等于0即可

sql 复制代码
 select * from 205_product
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="categoryId != null">
                and categoryId = #{categoryId}
            </if>
            <if test="ids != null">
                and id in
                <foreach collection="ids" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="name != null">
                and name like CONCAT('%',#{name}, '%')
            </if>
                 and status>0
        </where>
        order by seq desc
相关推荐
zhangbaolin2 天前
open webui源码分析4-初级RAG
状态模式
small_wh1te_coder5 天前
GCC深度剖析:从编译原理到嵌入式底层实战
汇编·c++·面试·嵌入式·状态模式·c
一个高效工作的家伙7 天前
amis表单较验
状态模式
zhysunny11 天前
20.万物皆可变身术:状态模式架构全景解析
java·状态模式
橘色的喵13 天前
嵌入式C语言编程:策略模式、状态模式和状态机的应用
c语言·状态模式·策略模式·状态机
跟着珅聪学java14 天前
Spring MVC 教程
状态模式
小傅哥16 天前
【分享】拼团交易平台系统,分布式、高并发、微服务
分布式·微服务·状态模式
mit6.82418 天前
[Agent开发平台] API网关 | 业务领域 | DTO格式 | 分页令牌
人工智能·golang·状态模式
飏旎19 天前
对于前端闭包的详细理解
前端·状态模式
曾经的三心草19 天前
微服务的编程测评系统9-竞赛新增-竞赛编辑
微服务·架构·状态模式