微服务项目->在线oj系统(Java-Spring)-后台管理(2)
题库管理
表结构设计
从图表中我们可以看到:
我们需要题目id(唯一),所以我们可以将question_id作为主键
题目标题(字符串)、题目难度(这里用1,2,3表示不同难度,前端统一处理)
创建人(int),创建时间(datetime),当然同理,我们数据库里面也要存修改人和修改时间来增加安全保障
当我们想要添加题目
我们需要题目,难度,时间限制(int)和空间限制(int),题目用例(字符串【json字符串】),默认代码块(字符串),main函数(字符串)
综上,我们可以创建如下表结构:
create table tb_question(
question_id bigint unsigned not null comment '题目id',
title varchar(50) not null comment '题目标题',
difficulty tinyint not null comment '题目难度1:简单 2: 中等 3: 困难',
time_limit int not null comment '时间限制',
space_limit int not null comment '空间限制',
content varchar(1000) not null comment '题目内容',
question_case varchar(1000) comment '题目用例',
default_code varchar(500) not null comment '默认代码块',
main_fuc varchar(500) not null comment 'main函数',
create_by bigint unsigned not null comment '创建人',
create_time datetime not null comment '创建时间',
update_by bigint unsigned comment '更新人',
update_time datetime comment '更新时间',
primary key(`question_id`)
);
创建实体类(Entity)
使用雪花id来确保主键
@TableName("tb_question")
@Data
public class Question extends BaseEntity {@TableId(type = IdType.ASSIGN_ID)private Long questionId;private String title;private Integer difficulty;private Long timeLimit;private Long spaceLimit;private String content;private String questionCase;private String defaultCode;private String mainFuc;
}
创建前端VO
当我们查询的时候,我们需要给前端返回问题ID,问题标题,问题难度,创建人,创建时间
所以下出如下代码
@Data
public class QuestionVO {private Long questionId;private String title;private Integer difficulty;private String createName;private LocalDateTime createTime;
}
创建接受前端类(DTO)
当我们在前端搜索的时候,我们需要给后端传递筛选参数以及页数:
我们需要传递难度,标题,第几页,每页几个
@Getter
@Setter
public class QuestionQueryDTO {private Integer difficulty;private String title;private Integer pageSize=10;private Integer pageNum=1;
}
专属返回结果
我们需要给前端返回值:告诉前端状况:
总数目,题目列表,响应状态码,响应消息
import com.bite.common.core.enums.ResultCode;
import lombok.Getter;
import lombok.Setter;import java.util.ArrayList;
import java.util.List;@Getter
@Setter
public class TableDataInfo {/*** 总记录数*/private long total;/*** 列表数据*/private List<?> rows;/*** 消息状态码*/private int code;/*** 消息内容*/private String msg;/*** 表格数据对象*/public TableDataInfo() {}//未查出任何数据时调用public static TableDataInfo empty() {TableDataInfo rspData = new TableDataInfo();rspData.setCode(ResultCode.SUCCESS.getCode());rspData.setRows(new ArrayList<>());rspData.setMsg(ResultCode.SUCCESS.getMsg());rspData.setTotal(0);return rspData;}//查出数据时调用public static TableDataInfo success(List<?> list,long total) {TableDataInfo rspData = new TableDataInfo();rspData.setCode(ResultCode.SUCCESS.getCode());rspData.setRows(list);rspData.setMsg(ResultCode.SUCCESS.getMsg());rspData.setTotal(total);return rspData;}
}
Controller类
@RestController
@RequestMapping("/question")
@Tag(name = "题目管理接口")
public class QuestionController extends BaseController {@Autowiredprivate IQuestionService questionService;@GetMapping("/list")public TableDataInfo list(QuestionQueryDTO questionQueryDTO) {return questionService.list(questionQueryDTO);}
}
Service类
我们从数据库中取出所需的题目列表,然后进行判断,如果不为空,则进行封装为TableDataInfo类
Mapper类
由于我们的查询条件较多,所以我们这里不使用Mybatis-plus提供的类,使用原始的xml方式
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bite.system.mapper.question.QuestionMapper"><select id="selectQuestionList" resultType="com.bite.system.model.question.vo.QuestionVO">SELECTtq.question_id,tq.title,tq.difficulty,ts.nick_name as create_name,tq.create_timeFROMtb_question tqleft jointb_sys_user tsontq.create_by = ts.user_id<where><if test="difficulty !=null ">AND difficulty = #{difficulty}</if><if test="title !=null and title !='' ">AND title LIKE CONCAT('%',#{title},'%')</if><if test="excludeIdSet !=null and !excludeIdSet.isEmpty()"><foreach collection="excludeIdSet" open=" AND tq.question_id NOT IN( " close=" ) " item="id" separator=",">#{id}</foreach></if></where>ORDER BYcreate_time DESC</select>
</mapper>
我们这里使用了联合查询,因为我们创建题库表的时候,只有创建人,但是没有匿称
剩下的就是我们之前学的一些查询语句