目录一、表的设计二、实体类三、功能梳理四、controller五、service六、mapper一、表的设计一张竞赛表以及一张竞赛题目关联表CREATETABLEtb_exam(exam_idBIGINTUNSIGNEDNOTNULLCOMMENT竞赛id主键,titleVARCHAR(50)NOTNULLCOMMENT竞赛标题,start_timeDATETIMENOTNULLCOMMENT竞赛开始时间,end_timeDATETIMENOTNULLCOMMENT竞赛结束时间,statusTINYINTNOTNULLDEFAULT0COMMENT是否发布 0: 未发布 1: 已发布,create_byBIGINTUNSIGNEDNOTNULLCOMMENT创建人,create_timeDATETIMENOTNULLCOMMENT创建时间,update_byBIGINTUNSIGNEDCOMMENT更新人,update_timeDATETIMECOMMENT更新时间,PRIMARYKEY(exam_id))ENGINEInnoDBDEFAULTCHARSETutf8mb4COMMENT竞赛表;CREATETABLEtb_exam_question(exam_question_idBIGINTUNSIGNEDNOTNULLCOMMENT关系id主键,question_idBIGINTUNSIGNEDNOTNULLCOMMENT题目id,exam_idBIGINTUNSIGNEDNOTNULLCOMMENT竞赛id,question_orderINTNOTNULLCOMMENT题目顺序,create_byBIGINTUNSIGNEDNOTNULLCOMMENT创建人,create_timeDATETIMENOTNULLDEFAULTCURRENT_TIMESTAMPCOMMENT创建时间,update_byBIGINTUNSIGNEDCOMMENT更新人,update_timeDATETIMEONUPDATECURRENT_TIMESTAMPCOMMENT更新时间,PRIMARYKEY(exam_question_id),KEYidx_exam_id(exam_id),KEYidx_question_id(question_id))ENGINEInnoDBDEFAULTCHARSETutf8mb4COMMENT竞赛题目关联表;二、实体类oj-modules/oj-system/src/main/java/com/yj/system/domain/exam包下新增两张表对应的实体类。packagecom.yj.system.domain.exam;importcom.baomidou.mybatisplus.annotation.IdType;importcom.baomidou.mybatisplus.annotation.TableId;importcom.baomidou.mybatisplus.annotation.TableName;importcom.yj.common.core.domain.BaseDO;importlombok.Getter;importlombok.Setter;importjava.time.LocalDateTime;/** * 竞赛表 */GetterSetterTableName(tb_exam)publicclassExamDOextendsBaseDO{/** * 竞赛id主键 */TableId(typeIdType.AUTO)// 如果你的主键是自增的用AUTO如果是雪花ID用ASSIGN_IDprivateLongexamId;/** * 竞赛标题 */privateStringtitle;/** * 竞赛开始时间 */privateLocalDateTimestartTime;/** * 竞赛结束时间 */privateLocalDateTimeendTime;/** * 是否发布 0:未发布 1:已发布 */privateIntegerstatus;}packagecom.yj.system.domain.exam;importcom.baomidou.mybatisplus.annotation.IdType;importcom.baomidou.mybatisplus.annotation.TableId;importcom.baomidou.mybatisplus.annotation.TableName;importcom.yj.common.core.domain.BaseDO;importlombok.Data;importlombok.Getter;importlombok.Setter;importjava.time.LocalDateTime;/** * 竞赛题目关联表 */GetterSetterTableName(tb_exam_question)publicclassExamQuestionDOextendsBaseDO{/** * 竞赛题目关系id主键 */TableId(typeIdType.AUTO)privateLongexamQuestionId;/** * 题目id */privateLongquestionId;/** * 竞赛id */privateLongexamId;}三、功能梳理竟赛列表功能:用户切换到竞赛管理。前端携带着相关参数发起获取竞赛列表的请求。后端接收到请求之后根据请求的参数从数据库当中查询符合条件的竞赛。并且将查询结果返回给前端。(成功或者失败)前端接收到后端返回的响应之后如果成功把查询到的列表数据展示到页面当中。如果失败提示用户失败的原因。四、controlleroj-modules/oj-system/src/main/java/com/yj/system/controller/exam/ExamController.java定义controller类packagecom.yj.system.controller.exam;importcom.yj.common.core.controller.BaseController;importcom.yj.common.core.pojo.TableDataInfo;importcom.yj.system.domain.exam.dto.ExamQueryDTO;importcom.yj.system.service.exam.IExamService;importio.swagger.v3.oas.annotations.tags.Tag;importjakarta.annotation.Resource;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;RestControllerRequestMapping(/exam)Tag(name竞赛管理接口)publicclassExamControllerextendsBaseController{ResourceprivateIExamServiceexamService;GetMapping(/list)publicTableDataInfolist(ExamQueryDTOexamQueryDTO){returngetTableDataInfo(examService.list(examQueryDTO));}}接收参数oj-modules/oj-system/src/main/java/com/yj/system/domain/exam/dto/ExamQueryDTO.javapackagecom.yj.system.domain.exam.dto;importcom.yj.common.core.domain.PageQueryDTO;publicclassExamQueryDTOextendsPageQueryDTO{/** * 竞赛标题 */privateStringtitle;/** * 竞赛开始时间 */privateStringstartTime;/** * 竞赛结束时间 */privateStringendTime;}五、serviceoj-modules/oj-system/src/main/java/com/yj/system/service/exam/IExamService.java定义接口类packagecom.yj.system.service.exam;importcom.yj.system.domain.exam.dto.ExamQueryDTO;importcom.yj.system.domain.exam.vo.ExamVO;importjava.util.List;publicinterfaceIExamService{ListExamVOlist(ExamQueryDTOexamQueryDTO);}oj-modules/oj-system/src/main/java/com/yj/system/service/exam/impl/ExamServiceImpl.java实现接口类packagecom.yj.system.service.exam.impl;importcom.github.pagehelper.PageHelper;importcom.yj.system.domain.exam.dto.ExamQueryDTO;importcom.yj.system.domain.exam.vo.ExamVO;importcom.yj.system.mapper.exam.ExamMapper;importcom.yj.system.service.exam.IExamService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;ServicepublicclassExamServiceImplimplementsIExamService{AutowiredprivateExamMapperexamMapper;OverridepublicListExamVOlist(ExamQueryDTOexamQueryDTO){PageHelper.startPage(examQueryDTO.getPageNum(),examQueryDTO.getPageSize());returnexamMapper.selectExamList(examQueryDTO);}}oj-modules/oj-system/src/main/java/com/yj/system/domain/exam/vo/ExamVO.java返回参数packagecom.yj.system.domain.exam.vo;importcom.fasterxml.jackson.annotation.JsonFormat;importjava.time.LocalDateTime;publicclassExamVO{privateStringtitle;/** * 是否发布 0:未发布 1:已发布 */privateIntegerstatus;JsonFormat(patternyyyy-MM-dd HH:mm:ss)privateLocalDateTimestartTime;JsonFormat(patternyyyy-MM-dd HH:mm:ss)privateLocalDateTimeendTime;privateStringcreateName;JsonFormat(patternyyyy-MM-dd HH:mm:ss)privateLocalDateTimecreateTime;}六、mappermapper方法oj-modules/oj-system/src/main/java/com/yj/system/mapper/exam/ExamMapper.javapackagecom.yj.system.mapper.exam;importcom.baomidou.mybatisplus.core.mapper.BaseMapper;importcom.yj.system.domain.exam.ExamDO;importcom.yj.system.domain.exam.dto.ExamQueryDTO;importcom.yj.system.domain.exam.vo.ExamVO;importorg.apache.ibatis.annotations.Mapper;importjava.util.List;MapperpublicinterfaceExamMapperextendsBaseMapperExamDO{ListExamVOselectExamList(ExamQueryDTOexamQueryDTO);}自定义xml:oj-modules/oj-system/src/main/resources/mapper/question/ExamMapper.xml?xml version1.0 encodingUTF-8 ?!DOCTYPEmapperPUBLIC-//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtdmappernamespacecom.yj.system.mapper.exam.ExamMapper!-- 竞赛列表查询 --selectidselectExamListresultTypecom.yj.system.domain.exam.vo.ExamVOSELECT te.exam_id, te.title, te.start_time, te.end_time, te.status, ts.nick_name AS create_name, te.create_time FROM tb_exam te LEFT JOIN tb_sys_user ts ON te.create_by ts.user_idwhereiftesttitle ! null and title ! AND te.title LIKE CONCAT(%,#{title},%)/ififteststartTime ! null and startTime ! AND te.start_time #{startTime}/ififtestendTime ! null and endTime ! AND te.end_timelt; #{endTime}/if/whereORDER BY te.create_time DESC/select/mapper