文章目录
- 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.单选题的插入
-
- 5.sun-club-infra
- 1.SubjectMappingDao.xml的批量插入如果逻辑删除字段为空就设置成0
- 2.SubjectRadioDao.xml 如果逻辑删除字段为空就设置成0
- 6.sun-club-application-controller
-
- 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;
@Data
public class SubjectInfoDTO implements Serializable {
private static final long serialVersionUID = -99877276843752542L;
private String subjectName;
private Integer subjectDifficult;
private Integer subjectType;
private Integer subjectScore;
private String subjectParse;
private String subjectAnswer;
private List<Integer> categoryIds;
private List<Integer> labelIds;
private List<SubjectAnswerDTO> optionList;
}
2.SubjectAnswerDTO.java
package com.sunxiansheng.subject.application.dto;
import lombok.Data;
import java.io.Serializable;
@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;
@Mapper
public interface SubjectAnswerDTOConverter {
SubjectAnswerDTOConverter INSTANCE= Mappers.getMapper(SubjectAnswerDTOConverter.class);
SubjectAnswerBO convertDTO2BO(SubjectAnswerDTO subjectAnswerDTO);
SubjectAnswerDTO convertBO2DTO(SubjectAnswerBO subjectAnswerBO);
List<SubjectAnswerBO> convertDTO2BO(List<SubjectAnswerDTO> subjectAnswerDTOList);
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;
@Mapper
public interface SubjectInfoDTOConverter {
SubjectInfoDTOConverter INSTANCE= Mappers.getMapper(SubjectInfoDTOConverter.class);
SubjectInfoBO convertDTO2BO(SubjectInfoDTO subjectInfoDTO);
SubjectInfoDTO convertBO2DTO(SubjectInfoBO subjectInfoBO);
List<SubjectInfoBO> convertDTO2BO(List<SubjectInfoDTO> subjectInfoDTOList);
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;
@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;
@Data
public class SubjectInfoBO implements Serializable {
private static final long serialVersionUID = -99877276843752542L;
private String subjectName;
private Integer subjectDifficult;
private Integer subjectType;
private Integer subjectScore;
private String subjectParse;
private String subjectAnswer;
private List<Integer> categoryIds;
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;
@Mapper
public interface SubjectInfoConverter {
SubjectInfoConverter INSTANCE = Mappers.getMapper(SubjectInfoConverter.class);
SubjectInfo convertBoToSubjectInfo(SubjectInfoBO subjectInfoBO);
SubjectInfoBO convertSubjectInfoToBo(SubjectInfo subjectInfo);
List<SubjectInfoBO> convertSubjectInfoToBo(List<SubjectInfo> subjectInfoList);
List<SubjectInfo> convertBoToSubjectInfo(List<SubjectInfoBO> subjectInfoBOList);
}
3.service
1.SubjectInfoDomainService.java
package com.sunxiansheng.subject.domain.service;
import com.sunxiansheng.subject.domain.entity.SubjectInfoBO;
public interface SubjectInfoDomainService {
Boolean add(SubjectInfoBO subjectInfoBO);
}
3.sun-club-common
1.enums
1.SubjectInfoTypeEnum.java 题目类型枚举
package com.sunxiansheng.subject.common.enums;
import lombok.Getter;
@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;
}
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;
public interface SubjectTypeHandler {
SubjectInfoTypeEnum getHandlerType();
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;
@Component
public class BriefTypeHandler implements SubjectTypeHandler{
@Override
public SubjectInfoTypeEnum getHandlerType() {
return SubjectInfoTypeEnum.BRIEF;
}
@Override
public 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;
@Component
public class JudgeTypeHandler implements SubjectTypeHandler{
@Override
public SubjectInfoTypeEnum getHandlerType() {
return SubjectInfoTypeEnum.JUDGE;
}
@Override
public 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;
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{
@Override
public SubjectInfoTypeEnum getHandlerType() {
return SubjectInfoTypeEnum.MULTIPLE;
}
@Override
public 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;
@Component
public class RadioTypeHandler implements SubjectTypeHandler{
@Override
public SubjectInfoTypeEnum getHandlerType() {
return SubjectInfoTypeEnum.RADIO;
}
@Override
public 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;
@Component
public class SubjectTypeHandlerFactory implements InitializingBean {
@Resource
private List<SubjectTypeHandler> subjectTypeHandlerList;
private Map<SubjectInfoTypeEnum, SubjectTypeHandler> handlerMap = new HashMap<>();
public SubjectTypeHandler getHandler(int subjectType) {
SubjectInfoTypeEnum subjectInfoTypeEnum = SubjectInfoTypeEnum.getByCode(subjectType);
return handlerMap.get(subjectInfoTypeEnum);
}
@Override
public void afterPropertiesSet() throws Exception {
subjectTypeHandlerList.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;
public interface SubjectInfoDomainService {
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;
@Service
@Slf4j
public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {
@Resource
private SubjectInfoService subjectInfoService;
@Resource
private SubjectMappingService subjectMappingService;
@Resource
private SubjectTypeHandlerFactory subjectTypeHandlerFactory;
@Override
public void add(SubjectInfoBO subjectInfoBO) {
if (log.isInfoEnabled()) {
log.info("SubjectInfoDomainServiceImpl add SubjectInfoBO, SubjectInfoBO:{}", subjectInfoBO);
}
SubjectInfo subjectInfo = SubjectInfoConverter.INSTANCE.convertBoToSubjectInfo(subjectInfoBO);
subjectInfoService.insert(subjectInfo);
SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfo.getSubjectType());
handler.add(subjectInfoBO);
List<Long> categoryIds = subjectInfoBO.getCategoryIds();
List<Long> labelIds = subjectInfoBO.getLabelIds();
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;
@Component
public class RadioTypeHandler implements SubjectTypeHandler {
@Resource
private SubjectRadioService subjectRadioService;
@Override
public SubjectInfoTypeEnum getHandlerType() {
return SubjectInfoTypeEnum.RADIO;
}
@Override
public void add(SubjectInfoBO subjectInfoBO) {
List<SubjectRadio> subjectRadioList = new ArrayList<>();
subjectInfoBO.getOptionList().forEach(subjectAnswerBO -> {
SubjectRadio subjectRadio = new SubjectRadio();
subjectRadio.setOptionType(subjectAnswerBO.getOptionType());
subjectRadio.setOptionContent(subjectAnswerBO.getOptionContent());
subjectRadio.setIsCorrect(subjectAnswerBO.getIsCorrect());
subjectRadio.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;
@RestController
@Slf4j
public class SubjectController {
@Resource
private SubjectInfoDomainService subjectInfoDomainService;
private final SubjectAnswerDTOConverter subjectAnswerDTOConverter;
public SubjectController(SubjectAnswerDTOConverter subjectAnswerDTOConverter) {
this.subjectAnswerDTOConverter = subjectAnswerDTOConverter;
}
@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()), "题目所属标签不能为空");
SubjectInfoBO 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.接口测试