当前位置: 首页 > news >正文

第四阶段10-添加类别,类别列表mapper层,service层,controller层

63. 添加类别–Mapper层

插入类别数据的功能此前已经完成!

64. 添加类别–Service层

在项目的根包下创建pojo.dto.CategoryAddNewDTO类:

@Data
public class CategoryAddNewDTO implements Serializable {/*** 类别名称*/private String name;/*** 父级类别id,如果无父级,则为0*/private Long parentId;/*** 关键词列表,各关键词使用英文的逗号分隔*/private String keywords;/*** 排序序号*/private Integer sort;/*** 图标图片的URL*/private String icon;/*** 是否启用,1=启用,0=未启用*/private Integer enable;/*** 是否显示在导航栏中,1=启用,0=未启用*/private Integer isDisplay;}

在项目的根包下创建service.ICategoryService接口,并在接口中添加抽象方法:

public interface ICategoryService {void addNew(CategoryAddNewDTO categoryAddNewDTO);
}

在项目的根包下创建service.impl.CategoryServiceImpl类,实现以上接口,并在类上添加@Service注解,然后,实现接口中的方法:

@Service
public class CategoryServiceImpl implements ICategoryService {public void addNew(CategoryAddNewDTO categoryAddNewDTO) {// 调用Mapper对象的countByName()检查类别名称是否已经被占用// 是:抛出异常(ERR_CONFLICT)// 如果parentId不是0,调用Mapper对象的getStandardById()查询类别详情// 判断查询结果是否为null// 是:父级类别不存在,抛出异常(NOT_FOUND)// depth(深度)的值:如果父级类别id为0,则depth为1,否则,depth为父级depth+1// 创建Category类的对象// 将参数对象中的各属性值复制到以上新创建的Category类的对象中// 补全Category类的对象的属性值:depth// 补全Category类的对象的属性值:isParent >>> 固定为0// 调用Mapper对象的insert()方法插入类别数据// 如果现在添加的类别的父级类别不是0,// 如果父级类别的isParent仍为0// 则将父级类别的isParent更新为1}}

具体实现为:

@Override
public void addNew(CategoryAddNewDTO categoryAddNewDTO) {log.debug("开始处理【添加类别】的业务,参数:{}", categoryAddNewDTO);// 调用Mapper对象的countByName()检查类别名称是否已经被占用String name = categoryAddNewDTO.getName();int countByName = categoryMapper.countByName(name);if (countByName > 0) {// 是:抛出异常(ERR_CONFLICT)String message = "添加类别失败,尝试添加的类别名称【" + name + "】已经被占用!";log.warn(message);throw new ServiceException(ServiceCode.ERR_CONFLICT, message);}Integer depth = 1;CategoryStandardVO parentCategory = null;Long parentId = categoryAddNewDTO.getParentId();if (parentId != 0) {// 如果parentId不是0,调用Mapper对象的getStandardById()查询类别详情parentCategory = categoryMapper.getStandardById(parentId);// 判断查询结果是否为nullif (parentCategory == null) {// 是:父级类别不存在,抛出异常(NOT_FOUND)String message = "添加类别失败,选定的父级类别不存在!";log.warn(message);throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);} else {depth = parentCategory.getDepth() + 1;}}// 创建Category类的对象Category category = new Category();// 将参数对象中的各属性值复制到以上新创建的Category类的对象中BeanUtils.copyProperties(categoryAddNewDTO, category);// 补全Category类的对象的属性值:depthcategory.setDepth(depth);// 补全Category类的对象的属性值:isParent >>> 固定为0category.setIsParent(0);// 调用Mapper对象的insert()方法插入类别数据categoryMapper.insert(category);// 如果现在添加的类别的父级类别不是0if (parentId != 0) {// 如果父级类别的isParent仍为0if (parentCategory.getIsParent() == 0) {// 则将父级类别的isParent更新为1Category updateParentCategory = new Category();updateParentCategory.setId(parentId);updateParentCategory.setIsParent(1);categoryMapper.update(updateParentCategory);}}
}

在项目的src/test/java下的根包下创建CategoryServiceTests类,对以上方法进行测试:

@Slf4j
@SpringBootTest
public class CategoryServiceTests {@AutowiredICategoryService service;@Testvoid addNew() {CategoryAddNewDTO categoryAddNewDTO = new CategoryAddNewDTO();categoryAddNewDTO.setParentId(74L);categoryAddNewDTO.setName("热带水果");try {service.addNew(categoryAddNewDTO);log.debug("添加类别成功!");} catch (ServiceException e) {log.debug("捕获到异常,其中的消息:{}", e.getMessage());}}}

65. 添加类别–Controller层

在项目的根包下创建controller.CategoryController类,在类上添加@RestController注解和@RequestMapping("/categories")注解,在类中自动装配ICategoryService类型的对象:

@RestController
@RequestMapping("/categories")
public class CategoryController {@Autowiredprivate ICategoryService categoryService;
}

然后,在此类中处理请求:

@PostMapping("/add-new")
public JsonResult<Void> addNew(CategoryAddNewDTO categoryAddNewDTO) {log.debug("xxx");categoryService.addNew(categoryAddNewDTO);return JsonResult.ok();
}

完成后,重启项目,可以通过在线API文档进行调试。

调试通过后,应该补充在线API文档的相关说明。

提示:接下来,在课上不再对请求参数的基本格式进行检查。

66. 删除类别–Mapper层

67. 删除类别–Service层

ICategoryService接口中添加抽象方法:

/*** 根据id删除类别** @param id 类别id*/
void delete(Long id);

CategoryServiceImpl中实现以上方法:

public void delete(Long id) {// 调用Mapper对象的getStandardById()执行查询// 判断查询结果是否为null// 是:数据已经不存在,抛出异常(NOT_FOUND)// 判断以上查询结果中的isParent是否为1// 是:当前尝试删除的是父级类别,抛出异常(CONFLICT)// 调用BrandCategoryMapper对象的countByCategory()执行统计// 判断统计结果是否大于0// 是:当前尝试删除的类别关联到了某些品牌,抛出异常(CONFLICT)// 调用CategoryAttributeTemplateMapper对象的countByCategory()执行统计// 判断统计结果是否大于0// 是:当前尝试删除的类别关联到了某些属性模板,抛出异常(CONFLICT)// 调用SpuMapper对象的countByCategory()执行统计// 判断统计结果是否大于0// 是:当前尝试删除的类别关联到了某些SPU,抛出异常(CONFLICT)// 调用Mapper对象的deleteById()执行删除// 从此前的查询结果中找到当前删除的类别的父级类别ID// 判断父级类别ID是否不为0// 调用Mapper对象的countByParentId()执行统计// 判断统计结果是否等于0// 是:将父级类别的isParent更新为0
}

具体实现为:

@Override
public void delete(Long id) {// 调用Mapper对象的getStandardById()执行查询CategoryStandardVO currentCategory = categoryMapper.getStandardById(id);// 判断查询结果是否为nullif (currentCategory == null) {// 是:数据已经不存在,抛出异常(NOT_FOUND)String message = "删除类别失败,尝试删除的类别不存在!";log.warn(message);throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);}// 判断以上查询结果中的isParent是否为1if (currentCategory.getIsParent() == 1) {// 是:当前尝试删除的是父级类别,抛出异常(CONFLICT)String message = "删除类别失败,尝试删除的类别仍包含子级类别!";log.warn(message);throw new ServiceException(ServiceCode.ERR_CONFLICT, message);}{// 调用BrandCategoryMapper对象的countByCategory()执行统计int count = brandCategoryMapper.countByCategory(id);// 判断统计结果是否大于0if (count > 0) {// 是:当前尝试删除的类别关联到了某些品牌,抛出异常(CONFLICT)String message = "删除类别失败,尝试删除的类别关联了某些品牌!";log.warn(message);throw new ServiceException(ServiceCode.ERR_CONFLICT, message);}}{// 调用CategoryAttributeTemplateMapper对象的countByCategory()执行统计int count = categoryAttributeTemplateMapper.countByCategory(id);// 判断统计结果是否大于0if (count > 0) {// 是:当前尝试删除的类别关联到了某些属性模板,抛出异常(CONFLICT)String message = "删除类别失败,尝试删除的类别关联了某些属性模板!";log.warn(message);throw new ServiceException(ServiceCode.ERR_CONFLICT, message);}}{// 调用SpuMapper对象的countByCategory()执行统计int count = spuMapper.countByCategory(id);// 判断统计结果是否大于0if (count > 0) {// 是:当前尝试删除的类别关联到了某些SPU,抛出异常(CONFLICT)String message = "删除类别失败,尝试删除的类别关联了某些SPU!";log.warn(message);throw new ServiceException(ServiceCode.ERR_CONFLICT, message);}}// 调用Mapper对象的deleteById()执行删除log.debug("即将执行删除类别,参数:{}", id);categoryMapper.deleteById(id);// 从此前的查询结果中找到当前删除的类别的父级类别IDLong parentId = currentCategory.getParentId();// 判断父级类别ID是否不为0if (parentId != 0) {// 调用Mapper对象的countByParentId()执行统计int count = categoryMapper.countByParentId(parentId);// 判断统计结果是否等于0if (count == 0) {// 是:将父级类别的isParent更新为0Category parentCategory = new Category();parentCategory.setId(parentId);parentCategory.setIsParent(0);categoryMapper.update(parentCategory);}}
}

CategoryServiceTests中编写并执行测试:

@Test
void delete() {Long id = 74L;try {service.delete(id);log.debug("删除类别成功!");} catch (ServiceException e) {log.debug("捕获到异常,其中的消息:{}", e.getMessage());}
}
http://www.lryc.cn/news/23212.html

相关文章:

  • linux内核启动分析(一)
  • wireshark常见使用操作讲解以及几个故障解决案例分享
  • 利用逻辑分析仪解析串口通讯数据
  • 新整理的前端面试题
  • 数据仓库-数仓分层
  • 【Linux】Linux根文件系统扩容
  • RPC编程:Hessian RPC一个老的RPC框架(一)
  • 逆向 x蜂窝 zzzghostsigh
  • QML 鼠标事件
  • 极智项目 | 实战pytorch arcface人脸识别
  • 【IP技术】ipv4和ipv6是什么?
  • linux基本功系列之uniq命令实战
  • 六、SpringBoot项目搭建
  • 【LeetCode】2363. 合并相似的物品
  • 华为OD机试题,用 Java 解【出租车计费】问题
  • 【人脸识别】DDL:数据分布知识蒸馏思想,提升困难样本(遮挡、低分辨率等)识别效果
  • 如何管理好仓库/库房?
  • Unity Lighting -- Unity的光源简介
  • Android仿网易云音乐歌单详情页
  • linux基本功系列之free命令实战
  • 华为OD机试模拟题 用 C++ 实现 - 连续子串(2023.Q1)
  • 【软考——系统架构师】UML 建模与架构文档化
  • Spring中常用注解
  • 基于SpringCloud的可靠消息最终一致性06:轮询事务消息
  • Python Flask + Echarts 轻松制作动态酷炫大屏( 附代码)
  • Wepack(1):SourceMap讲解以及使用
  • 华为OD机试题,用 Java 解【最多等和不相交连续子序列】问题
  • Kubernetes06:Controller
  • 采购文件中 RFI、RFQ、RFP、IFB的区别
  • linux升级gcc版本详细教程