自学内容网 自学内容网

OJ在线评测系统 后端开发数据库初始化工作 开发库表 建立数据库索引 Mybatis映射初始化接口开发

后端接口开发库表设计

项目主业务流程的开发

1.用户模块

注册(后端已实现)

登录(后端已实现 前端已实现)

2.题目模块

创建题目(管理员)

删除题目(管理员)

修改题目(管理员)

搜索题目(用户)

在线做题(题目详细页)

3.判题模块

提交判题(结果是否正确与错误)

错误处理(内存溢出 安全性 超时)

自主实现 代码沙箱(安全沙箱)

开放接口(提供一个独立的新服务)

创建索引

题目表

题目标题

题目内容 存放题目的介绍 输入输出提示 描述 具体的情况

题目标签(json数组字符串) 栈 队列 链表 简单 中等 困难

题目答案 管理员或者是用户设置的标准答案

提交数 通过题目的人数 便于分析统计

如果说题目不是很复杂 用例文件不大的话 可以直接存在数据库表里面

但是如果用例文件比较大 大于512kb的话

建议单独存放在一个文件中 数据库中只保存文件url

输入用例 1,2

输出用例3 4

时间限制

内存限制

我们这边选择的是一个judegConfig判题配置(json对象)

时间限制

内存限制

judgeCase判题用例(json数组)

每一个元素是 一个输入用例对应一个输出用例

数组中每一个元素 一个输入用例对应一个输出用例

json对象便于扩展

只需要改变对象内部的字段

而不用修改数据库表 可能会影响数据库

{
    "timeLimit":1000,
    "stackLimit":1000
}
[
    {
        "input":"1 2",
        "output":"3 4"
    },
    {
        "input":"1 3",
        "output":"2 4"
    }
]

存json的前提

你不需要根据某个字段去倒查某条数据

你的字段含义相关 属于同一类的数值

你的字段存储空间占比不能太大

    judgeCase text null comment '判题用例(json数组)',
    judgeConfig text null comment '判题配置(json数组)',

全部代码

-- 帖子表
create table if not exists question
(
    id          bigint auto_increment comment 'id' primary key,
    title       varchar(512)                       null comment '标题',
    content     text                               null comment '内容',
    tags        varchar(1024)                      null comment '标签列表(json 数组)',
    answer      text                               null comment '题目答案',
    submitNum   int      default 0                 not null comment '题目提交数',
    acceptedNum int      default 0                 not null comment '题目通过数',
    judgeCase text null comment '判题用例(json数组)',
    judgeConfig text null comment '判题配置(json数组)',

    thumbNum    int      default 0                 not null comment '点赞数',
    favourNum   int      default 0                 not null comment '收藏数',
    userId      bigint                             not null comment '创建用户 id',
    
    createTime  datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    updateTime  datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    isDelete    tinyint  default 0                 not null comment '是否删除',
    index idx_userId (userId)
) comment '题目' collate = utf8mb4_unicode_ci;

题目提交表

提交用户id: userId

题目id: questionId

语言: language

用户的代码: code

判题状态: status (待判题 判题中 成功 失败)

判题信息 判题过程中得到的一些信息 比如说程序失败的原因 程序执行消耗的时间 空间

judgeInfo(json对象)

{
"message": "程序执行信息",
"time": 1000,
"memory": 1000,
}

判题信息枚举值

Accepted

Wrong Answer

Compile Error

Memory Limit Exceeded

Time Limit Exceeded

Presentation Error

Output Limit Exceeded

Waiting

Dangerous Operation

Runtime error

System error

-- 题目提交表(硬删除)
create table if not exists question_submit
(
    id         bigint auto_increment comment 'id' primary key,
    language   varchar(128)                       not null comment '编程语言',
    code       text                               not null comment '用户代码',
    judgeInfo  text                               not null comment '判题信息(json对象)',
    status     int      default 0 comment '判题状态 0判题中 1成功 2成功 3失败',
    questionId bigint                             not null comment '题目id',
    userId     bigint                             not null comment '创建用户 id',
    createTime datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    updateTime datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    index idx_questionId (questionId),
    index idx_userId (userId)
) comment '题目提交表';

后端开发数据库索引

我们建立索引

最先考虑的是我们最多情况下是根据什么来查询表

就是放在where条件里面的东西

 

索引尽量加到区分度多的地方

什么情况下适合加索引呢

如何选择给哪个字段加索引?

答:首先从业务出发 无论是单个索引 还是联合索引

都要从实际查询的语句 字段枚举值的区分度 类型考虑(where指定的字段)

比如说where userId =1 and questionId=2

可以选择根据 userId 和 questionId 分部建立索引 也可以选择给这两个字段建立联合索引

如果这两个字段出现在一起 那么建立联合索引

原则上能不用索引就不用索引

能用单个索引就用单个索引

索引也要占用空间

类似于叫做目录

后端接口的开发 初步

第一步根据功能设计库表

第二步自动生成对数据库基本的增删改查(mapper和service层的基本功能)

第三步编写Controller层 实现基本增删改查和权限校验

第四步根据业务定制开发新的功能和代码

我们要安装一个mybatis x的插件

右键选择表

选择

这样就能一键生成

我们在把question_submit表生成一下

先搬到实体类里面

全部搬进后端模版


原文地址:https://blog.csdn.net/qq_30500575/article/details/142440919

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!