做头像的网站空白,西宁集团网站建设,东莞市网站设计,凤岗镇网站仿做文章目录 1.基本设计2.生成CRUD代码1.生成五张表的代码1.subject_info2.subject_brief3.subject_judge4.subject_multiple5.subject_radio 2.将所有的dao放到mapper文件夹3.将所有实体类使用lombok简化4.删除所有mapper的Param(pageable) Pageable pageable5.删除所… 文章目录 1.基本设计2.生成CRUD代码1.生成五张表的代码1.subject_info2.subject_brief3.subject_judge4.subject_multiple5.subject_radio 2.将所有的dao放到mapper文件夹3.将所有实体类使用lombok简化4.删除所有mapper的Param(pageable) Pageable pageable5.删除所有service的分页查询接口 3.具体实现1.sun-club-application-controller1.dto1.SubjectInfoDTO.java2.SubjectAnswerDTO.java 2.convert1.SubjectAnswerDTOConverter.java2.SubjectInfoDTOConverter.java 2.sun-club-domain1.entity1.SubjectAnswerBO.java2.SubjectInfoBO.java 2.convert1.SubjectInfoConverter.java 3.service1.SubjectInfoDomainService.java 3.sun-club-common1.enums1.SubjectInfoTypeEnum.java 题目类型枚举 4.sun-club-domain1.subject包构建一个题目类型工厂1.SubjectTypeHandler.java2.BriefTypeHandler.java3.JudgeTypeHandler.java4.MultipleTypeHandler.java5.RadioTypeHandler.java6.SubjectTypeHandlerFactory.java 2.service包来注入工厂调用插入方法1.SubjectInfoDomainService.java2.SubjectInfoDomainServiceImpl.java 3.单选题的插入1.RadioTypeHandler.java 5.sun-club-infra1.SubjectMappingDao.xml的批量插入如果逻辑删除字段为空就设置成02.SubjectRadioDao.xml 如果逻辑删除字段为空就设置成0 6.sun-club-application-controllerSubjectController.java 7.接口测试 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
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 ListInteger categoryIds;/*** 标签id*/private ListInteger labelIds;/*** 答案选项*/private ListSubjectAnswerDTO 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集合ListSubjectAnswerBO convertDTO2BO(ListSubjectAnswerDTO subjectAnswerDTOList);// 将SubjectAnswerBO集合转换为SubjectAnswerDTO集合ListSubjectAnswerDTO convertBO2DTO(ListSubjectAnswerBO 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集合ListSubjectInfoBO convertDTO2BO(ListSubjectInfoDTO subjectInfoDTOList);// 将SubjectInfoBO集合转换为SubjectInfoDTO集合ListSubjectInfoDTO convertBO2DTO(ListSubjectInfoBO 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 ListInteger categoryIds;/*** 标签id*/private ListInteger labelIds;/*** 答案选项*/private ListSubjectAnswerBO 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);// 将Listentity转换为ListBOListSubjectInfoBO convertSubjectInfoToBo(ListSubjectInfo subjectInfoList);// 将ListBO转换为ListentityListSubjectInfo convertBoToSubjectInfo(ListSubjectInfoBO 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 ListSubjectTypeHandler subjectTypeHandlerList;// 这个map是存储题目类型枚举和具体的题目类型对象的方便取出private MapSubjectInfoTypeEnum, 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);// 处理映射表// 首先获取分类idListLong categoryIds subjectInfoBO.getCategoryIds();// 然后获取标签idListLong labelIds subjectInfoBO.getLabelIds();// 这个映射表是多对多的关系假如有两个分类id和两个标签id则会有四条映射记录// 构建一个list用于存储映射表的实体类ListSubjectMapping 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) {// 一个单选题目是有多个选项所以需要一个listListSubjectRadio subjectRadioList new ArrayList();// 将bo中的选项列表转换为listSubjectRadiosubjectInfoBO.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 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 ResultBoolean 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);ListSubjectAnswerBO 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.接口测试