欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > 新增题目接口开发

新增题目接口开发

2025/4/9 9:03:28 来源:https://blog.csdn.net/m0_64637029/article/details/139969930  浏览:    关键词:新增题目接口开发

文章目录

    • 1.基本设计
    • 2.生成CRUD代码
        • 1.生成五张表的代码
          • 1.subject_info
          • 2.subject_brief
          • 3.subject_judge
          • 4.subject_multiple
          • 5.subject_radio
        • 2.将所有的dao放到mapper文件夹
        • 3.将所有实体类使用lombok简化
        • 4.删除所有mapper的@Param("pageable") Pageable pageable
        • 5.删除所有service的分页查询接口
    • 3.具体实现
        • 1.sun-club-application-controller
          • 1.dto
            • 1.SubjectInfoDTO.java
            • 2.SubjectAnswerDTO.java
          • 2.convert
            • 1.SubjectAnswerDTOConverter.java
            • 2.SubjectInfoDTOConverter.java
        • 2.sun-club-domain
          • 1.entity
            • 1.SubjectAnswerBO.java
            • 2.SubjectInfoBO.java
          • 2.convert
            • 1.SubjectInfoConverter.java
          • 3.service
            • 1.SubjectInfoDomainService.java
        • 3.sun-club-common
          • 1.enums
            • 1.SubjectInfoTypeEnum.java 题目类型枚举
        • 4.sun-club-domain
          • 1.subject包构建一个题目类型工厂
            • 1.SubjectTypeHandler.java
            • 2.BriefTypeHandler.java
            • 3.JudgeTypeHandler.java
            • 4.MultipleTypeHandler.java
            • 5.RadioTypeHandler.java
            • 6.SubjectTypeHandlerFactory.java
          • 2.service包来注入工厂,调用插入方法
            • 1.SubjectInfoDomainService.java
            • 2.SubjectInfoDomainServiceImpl.java
          • 3.单选题的插入
            • 1.RadioTypeHandler.java
        • 5.sun-club-infra
          • 1.SubjectMappingDao.xml的批量插入如果逻辑删除字段为空就设置成0
          • 2.SubjectRadioDao.xml 如果逻辑删除字段为空就设置成0
        • 6.sun-club-application-controller
          • SubjectController.java
        • 7.接口测试

1.基本设计

image-20240527135224721

2.生成CRUD代码

1.生成五张表的代码
1.subject_info

image-20240526172642220

2.subject_brief

image-20240526172735698

3.subject_judge

4.subject_multiple

image-20240526173001618

5.subject_radio

image-20240526173107512

2.将所有的dao放到mapper文件夹

image-20240527131816152

3.将所有实体类使用lombok简化
4.删除所有mapper的@Param(“pageable”) Pageable pageable
5.删除所有service的分页查询接口

3.具体实现

1.sun-club-application-controller
1.dto
1.SubjectInfoDTO.java
package com.sunxiansheng.subject.application.dto;import lombok.Data;import java.io.Serializable;
import java.util.List;/*** 题目信息表(SubjectInfo)实体类** @author makejava* @since 2024-05-26 17:26:43*/
@Data
public class SubjectInfoDTO implements Serializable {private static final long serialVersionUID = -99877276843752542L;/*** 题目名称*/private String subjectName;/*** 题目难度*/private Integer subjectDifficult;/*** 题目类型 1单选 2多选 3判断 4简答*/private Integer subjectType;/*** 题目分数*/private Integer subjectScore;/*** 题目解析*/private String subjectParse;/*** 题目答案*/private String subjectAnswer;/*** 分类id*/private List<Integer> categoryIds;/*** 标签id*/private List<Integer> labelIds;/*** 答案选项*/private List<SubjectAnswerDTO> optionList;
}
2.SubjectAnswerDTO.java
package com.sunxiansheng.subject.application.dto;import lombok.Data;import java.io.Serializable;/*** Description: 题目答案dto* @Author sun* @Create 2024/5/27 13:39* @Version 1.0*/
@Data
public class SubjectAnswerDTO implements Serializable {/*** 答案选项标识*/private Integer optionType;/*** 答案*/private String optionContent;/*** 是否正确*/private Integer isCorrect;}
2.convert
1.SubjectAnswerDTOConverter.java
package com.sunxiansheng.subject.application.convert;import com.sunxiansheng.subject.application.dto.SubjectAnswerDTO;
import com.sunxiansheng.subject.domain.entity.SubjectAnswerBO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;import java.util.List;/*** Description: DTO与BO转换* @Author sun* @Create 2024/5/24 9:40* @Version 1.0*/
@Mapper
public interface SubjectAnswerDTOConverter {SubjectAnswerDTOConverter INSTANCE= Mappers.getMapper(SubjectAnswerDTOConverter.class);// 将SubjectAnswerDTO转换为SubjectAnswerBOSubjectAnswerBO convertDTO2BO(SubjectAnswerDTO subjectAnswerDTO);// 将SubjectAnswerBO转换为SubjectAnswerDTOSubjectAnswerDTO convertBO2DTO(SubjectAnswerBO subjectAnswerBO);// 将SubjectAnswerDTO集合转换为SubjectAnswerBO集合List<SubjectAnswerBO> convertDTO2BO(List<SubjectAnswerDTO> subjectAnswerDTOList);// 将SubjectAnswerBO集合转换为SubjectAnswerDTO集合List<SubjectAnswerDTO> convertBO2DTO(List<SubjectAnswerBO> subjectAnswerBOList);
}
2.SubjectInfoDTOConverter.java
package com.sunxiansheng.subject.application.convert;import com.sunxiansheng.subject.application.dto.SubjectInfoDTO;
import com.sunxiansheng.subject.domain.entity.SubjectInfoBO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;import java.util.List;/*** Description: DTO与BO转换* @Author sun* @Create 2024/5/24 9:40* @Version 1.0*/
@Mapper
public interface SubjectInfoDTOConverter {SubjectInfoDTOConverter INSTANCE= Mappers.getMapper(SubjectInfoDTOConverter.class);// 将SubjectInfoDTO转换为SubjectInfoBOSubjectInfoBO convertDTO2BO(SubjectInfoDTO subjectInfoDTO);// 将SubjectInfoBO转换为SubjectInfoDTOSubjectInfoDTO convertBO2DTO(SubjectInfoBO subjectInfoBO);// 将SubjectInfoDTO集合转换为SubjectInfoBO集合List<SubjectInfoBO> convertDTO2BO(List<SubjectInfoDTO> subjectInfoDTOList);// 将SubjectInfoBO集合转换为SubjectInfoDTO集合List<SubjectInfoDTO> convertBO2DTO(List<SubjectInfoBO> subjectInfoBOList);
}
2.sun-club-domain
1.entity
1.SubjectAnswerBO.java
package com.sunxiansheng.subject.domain.entity;import lombok.Data;import java.io.Serializable;/*** Description: 题目答案dto* @Author sun* @Create 2024/5/27 13:39* @Version 1.0*/
@Data
public class SubjectAnswerBO implements Serializable {/*** 答案选项标识*/private Integer optionType;/*** 答案*/private String optionContent;/*** 是否正确*/private Integer isCorrect;}
2.SubjectInfoBO.java
package com.sunxiansheng.subject.domain.entity;import lombok.Data;import java.io.Serializable;
import java.util.List;/*** 题目信息表(SubjectInfo)实体类** @author makejava* @since 2024-05-26 17:26:43*/
@Data
public class SubjectInfoBO implements Serializable {private static final long serialVersionUID = -99877276843752542L;/*** 题目名称*/private String subjectName;/*** 题目难度*/private Integer subjectDifficult;/*** 题目类型 1单选 2多选 3判断 4简答*/private Integer subjectType;/*** 题目分数*/private Integer subjectScore;/*** 题目解析*/private String subjectParse;/*** 题目答案*/private String subjectAnswer;/*** 分类id*/private List<Integer> categoryIds;/*** 标签id*/private List<Integer> labelIds;/*** 答案选项*/private List<SubjectAnswerBO> optionList;
}
2.convert
1.SubjectInfoConverter.java
package com.sunxiansheng.subject.domain.convert;import com.sunxiansheng.subject.domain.entity.SubjectInfoBO;
import com.sunxiansheng.subject.infra.basic.entity.SubjectInfo;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;import java.util.List;/*** Description: 题目标签转换器* @Author sun* @Create 2024/5/24 9:18* @Version 1.0*/
@Mapper // mapstruct的注解
public interface SubjectInfoConverter {SubjectInfoConverter INSTANCE = Mappers.getMapper(SubjectInfoConverter.class);// 将BO转换为entitySubjectInfo convertBoToSubjectInfo(SubjectInfoBO subjectInfoBO);// 将entity转换为BOSubjectInfoBO convertSubjectInfoToBo(SubjectInfo subjectInfo);// 将List<entity>转换为List<BO>List<SubjectInfoBO> convertSubjectInfoToBo(List<SubjectInfo> subjectInfoList);// 将List<BO>转换为List<entity>List<SubjectInfo> convertBoToSubjectInfo(List<SubjectInfoBO> subjectInfoBOList);
}
3.service
1.SubjectInfoDomainService.java
package com.sunxiansheng.subject.domain.service;import com.sunxiansheng.subject.domain.entity.SubjectInfoBO;/*** Description:* @Author sun* @Create 2024/5/24 9:03* @Version 1.0*/
public interface SubjectInfoDomainService {/*** 新增题目* @param subjectInfoBO*/Boolean add(SubjectInfoBO subjectInfoBO);}
3.sun-club-common
1.enums
1.SubjectInfoTypeEnum.java 题目类型枚举
package com.sunxiansheng.subject.common.enums;import lombok.Getter;/*** Description: 题目类型枚举* @Author sun* @Create 2024/5/24 9:53* @Version 1.0*/
@Getter
public enum SubjectInfoTypeEnum {RADIO(1,"单选"),MULTIPLE(2,"多选"),JUDGE(3,"判断"),BRIEF(4,"简答");public int code;public String desc;SubjectInfoTypeEnum(int code, String desc) {this.code = code;this.desc = desc;}/*** 根据code获取枚举* @param code* @return*/public static SubjectInfoTypeEnum getByCode(int code) {for (SubjectInfoTypeEnum value : values()) {if (value.code == code) {return value;}}return null;}
}
4.sun-club-domain
1.subject包构建一个题目类型工厂
1.SubjectTypeHandler.java
package com.sunxiansheng.subject.domain.handler.subject;import com.sunxiansheng.subject.common.enums.SubjectInfoTypeEnum;
import com.sunxiansheng.subject.domain.entity.SubjectInfoBO;/*** Description:* @Author sun* @Create 2024/5/27 21:12* @Version 1.0*/
public interface SubjectTypeHandler {/*** 枚举身份的识别* @return*/SubjectInfoTypeEnum getHandlerType();/*** 实际题目的插入* @param subjectInfoBO*/void add(SubjectInfoBO subjectInfoBO);
}
2.BriefTypeHandler.java
package com.sunxiansheng.subject.domain.handler.subject;import com.sunxiansheng.subject.common.enums.SubjectInfoTypeEnum;
import com.sunxiansheng.subject.domain.entity.SubjectInfoBO;
import org.springframework.stereotype.Component;/*** Description:* @Author sun* @Create 2024/5/27 21:16* @Version 1.0*/
@Component
public class BriefTypeHandler implements SubjectTypeHandler{@Overridepublic SubjectInfoTypeEnum getHandlerType() {return SubjectInfoTypeEnum.BRIEF;}@Overridepublic void add(SubjectInfoBO subjectInfoBO) {}
}
3.JudgeTypeHandler.java
package com.sunxiansheng.subject.domain.handler.subject;import com.sunxiansheng.subject.common.enums.SubjectInfoTypeEnum;
import com.sunxiansheng.subject.domain.entity.SubjectInfoBO;
import org.springframework.stereotype.Component;/*** Description:* @Author sun* @Create 2024/5/27 21:16* @Version 1.0*/
@Component
public class JudgeTypeHandler implements SubjectTypeHandler{@Overridepublic SubjectInfoTypeEnum getHandlerType() {return SubjectInfoTypeEnum.JUDGE;}@Overridepublic void add(SubjectInfoBO subjectInfoBO) {}
}
4.MultipleTypeHandler.java
package com.sunxiansheng.subject.domain.handler.subject;import com.sunxiansheng.subject.common.enums.SubjectInfoTypeEnum;
import com.sunxiansheng.subject.domain.entity.SubjectInfoBO;
import org.springframework.stereotype.Component;/*** Description:* @Author sun* @Create 2024/5/27 21:16* @Version 1.0*/
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{@Overridepublic SubjectInfoTypeEnum getHandlerType() {return SubjectInfoTypeEnum.MULTIPLE;}@Overridepublic void add(SubjectInfoBO subjectInfoBO) {}
}
5.RadioTypeHandler.java
package com.sunxiansheng.subject.domain.handler.subject;import com.sunxiansheng.subject.common.enums.SubjectInfoTypeEnum;
import com.sunxiansheng.subject.domain.entity.SubjectInfoBO;
import org.springframework.stereotype.Component;/*** Description:* @Author sun* @Create 2024/5/27 21:16* @Version 1.0*/
@Component
public class RadioTypeHandler implements SubjectTypeHandler{@Overridepublic SubjectInfoTypeEnum getHandlerType() {return SubjectInfoTypeEnum.RADIO;}@Overridepublic void add(SubjectInfoBO subjectInfoBO) {// 单选题目的插入}
}
6.SubjectTypeHandlerFactory.java
package com.sunxiansheng.subject.domain.handler.subject;import com.sunxiansheng.subject.common.enums.SubjectInfoTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** Description: 题目类型工厂* @Author sun* @Create 2024/5/27 21:25* @Version 1.0*/
@Component
public class SubjectTypeHandlerFactory implements InitializingBean {// 将所有的题目类型对象注入到List中@Resourceprivate List<SubjectTypeHandler> subjectTypeHandlerList;// 这个map是存储题目类型枚举和具体的题目类型对象的,方便取出private Map<SubjectInfoTypeEnum, SubjectTypeHandler> handlerMap = new HashMap<>();/*** 简单工厂核心方法:根据输入的类型,返回对应类型的对象* @param subjectType* @return*/public SubjectTypeHandler getHandler(int subjectType) {// 首先根据这个枚举码来获取对应的枚举对象SubjectInfoTypeEnum subjectInfoTypeEnum = SubjectInfoTypeEnum.getByCode(subjectType);// 然后通过map,根据不同类型的枚举码,返回对应类型的题目类型对象return handlerMap.get(subjectInfoTypeEnum);}/*** 这个方法bean装载完毕之后就会执行,可以进行初始化操作* @throws Exception*/@Overridepublic void afterPropertiesSet() throws Exception {// 初始化存储题目类型的mapsubjectTypeHandlerList.forEach(subjectTypeHandler -> {handlerMap.put(subjectTypeHandler.getHandlerType(), subjectTypeHandler);});}
}
2.service包来注入工厂,调用插入方法
1.SubjectInfoDomainService.java
package com.sunxiansheng.subject.domain.service;import com.sunxiansheng.subject.domain.entity.SubjectInfoBO;/*** Description:* @Author sun* @Create 2024/5/24 9:03* @Version 1.0*/
public interface SubjectInfoDomainService {/*** 新增题目* @param subjectInfoBO*/void add(SubjectInfoBO subjectInfoBO);}
2.SubjectInfoDomainServiceImpl.java
package com.sunxiansheng.subject.domain.service.impl;import com.google.common.collect.Lists;
import com.sunxiansheng.subject.domain.convert.SubjectInfoConverter;
import com.sunxiansheng.subject.domain.entity.SubjectInfoBO;
import com.sunxiansheng.subject.domain.handler.subject.SubjectTypeHandler;
import com.sunxiansheng.subject.domain.handler.subject.SubjectTypeHandlerFactory;
import com.sunxiansheng.subject.domain.service.SubjectInfoDomainService;
import com.sunxiansheng.subject.infra.basic.entity.SubjectInfo;
import com.sunxiansheng.subject.infra.basic.entity.SubjectMapping;
import com.sunxiansheng.subject.infra.basic.service.SubjectInfoService;
import com.sunxiansheng.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;/*** Description:* @Author sun* @Create 2024/5/24 9:03* @Version 1.0*/
@Service
@Slf4j
public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {@Resourceprivate SubjectInfoService subjectInfoService;@Resourceprivate SubjectMappingService subjectMappingService;@Resourceprivate SubjectTypeHandlerFactory subjectTypeHandlerFactory;@Overridepublic void add(SubjectInfoBO subjectInfoBO) {// 打印日志if (log.isInfoEnabled()) {log.info("SubjectInfoDomainServiceImpl add SubjectInfoBO, SubjectInfoBO:{}", subjectInfoBO);}// 将BO转换为entitySubjectInfo subjectInfo = SubjectInfoConverter.INSTANCE.convertBoToSubjectInfo(subjectInfoBO);// 向SubjectInfo表中插入数据subjectInfoService.insert(subjectInfo);// 从工厂中获取对应的题目对象,并执行对应的插入逻辑SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfo.getSubjectType());handler.add(subjectInfoBO);// 处理映射表// 首先获取分类idList<Long> categoryIds = subjectInfoBO.getCategoryIds();// 然后获取标签idList<Long> labelIds = subjectInfoBO.getLabelIds();// 这个映射表是多对多的关系,假如有两个分类id和两个标签id则会有四条映射记录// 构建一个list,用于存储映射表的实体类List<SubjectMapping> subjectMappings = new ArrayList<>();labelIds.forEach(laberId -> {categoryIds.forEach(categoryId -> {SubjectMapping subjectMapping = new SubjectMapping();subjectMapping.setSubjectId(subjectInfoBO.getId());subjectMapping.setLabelId(laberId);subjectMapping.setCategoryId(categoryId);subjectMappings.add(subjectMapping);});});// 批量插入映射表subjectMappingService.insertBatch(subjectMappings);}
}
3.单选题的插入
1.RadioTypeHandler.java
package com.sunxiansheng.subject.domain.handler.subject;import com.sunxiansheng.subject.common.enums.SubjectInfoTypeEnum;
import com.sunxiansheng.subject.domain.entity.SubjectInfoBO;
import com.sunxiansheng.subject.infra.basic.entity.SubjectRadio;
import com.sunxiansheng.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;/*** Description:* @Author sun* @Create 2024/5/27 21:16* @Version 1.0*/
@Component
public class RadioTypeHandler implements SubjectTypeHandler {@Resourceprivate SubjectRadioService subjectRadioService;@Overridepublic SubjectInfoTypeEnum getHandlerType() {return SubjectInfoTypeEnum.RADIO;}@Overridepublic void add(SubjectInfoBO subjectInfoBO) {// 一个单选题目是有多个选项,所以需要一个listList<SubjectRadio> subjectRadioList = new ArrayList<>();// 将bo中的选项列表转换为list<SubjectRadio>subjectInfoBO.getOptionList().forEach(subjectAnswerBO -> {SubjectRadio subjectRadio = new SubjectRadio();// 设置基本信息subjectRadio.setOptionType(subjectAnswerBO.getOptionType());subjectRadio.setOptionContent(subjectAnswerBO.getOptionContent());subjectRadio.setIsCorrect(subjectAnswerBO.getIsCorrect());// 设置题目idsubjectRadio.setSubjectId(subjectInfoBO.getId());subjectRadioList.add(subjectRadio);});// 批量插入subjectRadioService.batchInsert(subjectRadioList);}
}
5.sun-club-infra
1.SubjectMappingDao.xml的批量插入如果逻辑删除字段为空就设置成0
2.SubjectRadioDao.xml 如果逻辑删除字段为空就设置成0

image-20240528142721846

6.sun-club-application-controller
SubjectController.java
package com.sunxiansheng.subject.application.controller;import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.sunxiansheng.subject.application.convert.SubjectAnswerDTOConverter;
import com.sunxiansheng.subject.application.convert.SubjectInfoDTOConverter;
import com.sunxiansheng.subject.application.dto.SubjectInfoDTO;
import com.sunxiansheng.subject.common.eneity.Result;
import com.sunxiansheng.subject.domain.convert.SubjectInfoConverter;
import com.sunxiansheng.subject.domain.entity.SubjectAnswerBO;
import com.sunxiansheng.subject.domain.entity.SubjectInfoBO;
import com.sunxiansheng.subject.domain.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.util.List;/*** Description: 刷题微服务控制器* @Author sun* @Create 2024/5/23 16:42* @Version 1.0*/
@RestController
@Slf4j
public class SubjectController {@Resourceprivate SubjectInfoDomainService subjectInfoDomainService;private final SubjectAnswerDTOConverter subjectAnswerDTOConverter;public SubjectController(SubjectAnswerDTOConverter subjectAnswerDTOConverter) {this.subjectAnswerDTOConverter = subjectAnswerDTOConverter;}/*** 新增题目* @param subjectInfoDTO* @return*/@PostMapping("/add")public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {try {// 打印日志if (log.isInfoEnabled()) {log.info("SubjectController add SubjectInfoDTO, subjectInfoDTO:{}", JSON.toJSONString(subjectInfoDTO));}// 参数校验Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()), "题目名称不能为空");Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()), "题目所属分类不能为空");Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()), "题目所属标签不能为空");// 转换DTO为BOSubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTO2BO(subjectInfoDTO);List<SubjectAnswerBO> subjectAnswerBOS = subjectAnswerDTOConverter.convertDTO2BO(subjectInfoDTO.getOptionList());subjectInfoBO.setOptionList(subjectAnswerBOS);// 新增题目subjectInfoDomainService.add(subjectInfoBO);return Result.ok(true);} catch (Exception e) {log.error("SubjectController add error, subjectInfoDTO:{}", JSON.toJSONString(subjectInfoDTO), e);return Result.fail("新增题目失败");}}
}
7.接口测试

image-20240528153343996

image-20240528153350317

image-20240528153406939

image-20240528153419034

image-20240528153429556

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词