枚举实现: 在service层业务代码控制中,在代码中简洁控制代码,报错提示明显意见,在这里我用枚举实现。

枚举类:

/**
 * 错误码
 *
 * @author 方先森
 */

public enum ResultCode {

    /**
     * 请求成功
     */
    SUCCESS_CODE(0, "请求处理成功"),

    /**
     * 认证失败
     */
    UN_AUTHORIZATION(401, "认证失败"),

    /**
     * 权限不足
     */
    ACCESS_DENIED(403, "无访问权限"),

    /**
     * 服务间远程调用错误
     */
    SERVICE_REMOTE_INVOKE_ERROR_CODE(503, "服务调用异常"),
    ANT_INSERT_ERROR_CODE(100101, "新增失败"),
    ANT_UPDATE_ERROR_CODE(200102, "编辑失败"),
    ANT_DELETE_ERROR_CODE(200103, "删除失败"),
    ANT_DELETE_BATCH_ERROR_CODE(200104, "批量删除失败"),
    ANT_QUERY_ERROR_CODE(200105, "查询失败"),
    ANT_QUERY_LIST_ERROR_CODE(200106, "列表查询失败"),
    ANT_PARAMS_NOT_WISH(200107, "参数不符合规范"),
    TEST_ERROR(200421, "异常测试"),
    ;

    private int code;

    private String message;

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

    public int code() {
        return code;
    }

    public String message() {
        return message;
    }

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

service层实现方式: 为了代码优雅,尽量减少"CODE"这样的代码出现,尽量定义常量进行拼接;

private static final String ERROR = "触发条件";

     /**
     * 批量删除触发条件
     *
     * @param ids 需要删除的触发条件主键
     * @return 结果
     */
    @Override
    public int deleteByIds(Long[] ids) {
        try {
            return baseMapper.update(new Cat(), new LambdaUpdateWrapper<Cat>()
                    .in(Cat::getId, (Object[]) ids)
                    .set(Cat::getDelFlag, DeleteFlagEnum.DELETE_FLAG_ONE.getCode()));
        } catch (Exception e) {
            log.error(ERROR + ResultCode.ANT_DELETE_BATCH_ERROR_CODE + ":", e);
            throw new ServiceException(ResultCode.ANT_DELETE_BATCH_ERROR_CODE, ERROR );
        }
    }

DeleteFlagEnum 类:

/**
 * 删除标志
 *
 * @author 方先森
 */
public enum DeleteFlagEnum {

    DELETE_FLAG_ZERO("0","未删除"),
    DELETE_FLAG_ONE("1","已删除");

    private final String code;
    private final String desc;

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

    public String getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }
    
    public static DeleteFlagEnum getByCode(String code){
        for(DeleteFlagEnum value: DeleteFlagEnum.values()){
            if(value.getCode().equals(code)){
                return value;
            }
        }
        return DELETE_FLAG_ZERO;
    }
}