4-8 CS后台项目练习-2
8. 类别管理--添加类别--持久层
8.1. 配置
续前日,无新增
8.2. 规划需要执行的SQL语句
续前日,无新增
8.3. 接口与抽象方法
此前需要执行的SQL语句大致是:
select id from pms_category where name=?;
在csmall-pojo的根包下创建vo.CategorySimpleVO类,用于封装以上查询结果:
@Data
public class CategorySimpleVO implements Serializable {
private Long id;
}
在csmall-product-webapi的CategoryMapper接口中添加抽象方法:
CategorySimpleVO getByName(String name);
8.4. 配置SQL语句
在csmall-product-webapi的CategoryMapper.xml中添加配置:
<!-- CategorySimpleVO getByName(String name); -->
<select id="getByName" resultMap="SimpleResultMap">
select id from pms_category where name=#{name}
</select>
<resultMap id="SimpleResultMap" type="cn.tedu.csmall.pojo.vo.CategorySimpleVO">
<id column="id" property="id" />
</resultMap>
8.5. 测试
在csmall-product-webapi的src\test\resources下创建insert_data.sql文件,用于插入测试数据:
insert into pms_category (name) value ('类别001'), ('类别002');
然后,在CategoryMapperTests中添加测试方法:
@Test
@Sql({"classpath:truncate.sql", "classpath:insert_data.sql"})
public void testGetByNameSuccessfully() {
// 测试数据
String name = "类别001";
// 断言不会抛出异常
assertDoesNotThrow(() -> {
// 执行查询
CategorySimpleVO category = mapper.getByName(name);
// 断言查询结果不为null
assertNotNull(category);
});
}
@Test
@Sql({"classpath:truncate.sql"})
public void testGetByNameFailBecauseNotFound() {
// 测试数据
String name = "类别999";
// 断言不会抛出异常
assertDoesNotThrow(() -> {
// 执行查询
CategorySimpleVO category = mapper.getByName(name);
// 断言查询结果为null
assertNull(category);
});
}
完成后,执行整个测试类(将执行此类中所有测试方法),应该全部通过测试。
9. 类别管理--添加类别--业务逻辑层
9.1. 接口与抽象方法
在使用Dubbol的微服务架构中,需要将业务逻辑层的接口声明在专门的Module中,便于被其它微服务Module依赖,所以,先在csmall-product下创建新的Module,名为csmall-product-service,创建参数:
- Group:
cn.tedu - Artifact:
csmall-product-service - Package Name:
cn.tedu.csmall.product.service
首先,应该调用新Module的pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 父级项目 -->
<parent>
<groupId>cn.tedu</groupId>
<artifactId>csmall-product</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<!-- 当前项目的信息 -->
<groupId>cn.tedu</groupId>
<artifactId>csmall-product-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 当前项目需要使用的依赖项 -->
<dependencies>
<!-- Csmall POJO -->
<dependency>
<groupId>cn.tedu</groupId>
<artifactId>csmall-pojo</artifactId>
</dependency>
</dependencies>
</project>
然后,在csmall-product的pom.xml中补充此子级Module:
<!-- 当前Project的各子级Module -->
<modules>
<module>csmall-product-webapi</module>
<module>csmall-product-service</module> <!-- 新增 -->
</modules>
接下来,需要删除不必要的文件:
- 启动类
src\main\resources及其下的配置文件src\test
接下来,需要创建接口并添加抽象方法,方法的参数应该是封装的对象(因为一个String或Long等简单数据不足以完成添加类别的操作),则先在csmall-pojo的根包下创建dto.CategoryAddNewDTO类,并在类中添加必要的属性:
package cn.tedu.csmall.pojo.dto;
import lombok.Data;
import java.io.Serializable;
@Data
public class CategoryAddNewDTO implements Serializable {
private String name;
private Long parentId;
private String keywords;
private Integer sort;
private String icon;
private Integer isDisplay;
}
然后,在csmall-product-service中,在cn.tedu.csmall.product.service下创建ICategoryService接口:
public interface ICategoryService {
void addNew(CategoryAddNewDTO categoryAddNewDTO);
}
9.2. 实现
在csmall-product-service中只存放业务逻辑层的接口,而业务逻辑层的实现类仍在csmall-product-webapi中,所以,需要在csmall-product-webapi中依赖csmall-product-service。
先在Project的pom.xml中添加对csmall-product-service的依赖管理:
<!-- ===== 原有其它代码 ===== -->
<!-- 依赖管理,主要管理各依赖项的版本,使得子级Module添加依赖时不必指定版本 -->
<dependencyManagement>
<dependencies>
<!-- Csmall Product Service -->
<dependency>
<groupId>cn.tedu</groupId>
<artifactId>csmall-product-service</artifactId>
<version>${csmall.version}</version>
</dependency>
<!-- ===== 原有其它代码 ===== -->
然后,在csmall-product-webapi中添加依赖:
<!-- ===== 原有其它代码 ===== -->
<!-- 当前项目需要使用的依赖项 -->
<dependencies>
<!-- Csmall Product Service -->
<dependency>
<groupId>cn.tedu</groupId>
<artifactId>csmall-product-service</artifactId>
</dependency>
<!-- ===== 原有其它代码 ===== -->
在cn.tedu.csmall.product.webapi下创建service.CategoryServiceImpl类,此类应该实现ICategoryService接口,此类还应该添加@Service注解:
package cn.tedu.csmall.product.webapi.service;
import cn.tedu.csmall.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.service.ICategoryService;
import org.springframework.stereotype.Service;
@Service
public class CategoryServiceImpl implements ICategoryService {
@Override
public void addNew(CategoryAddNewDTO categoryAddNewDTO) {
}
}
关于以上业务的实现分析:
@Autowired
private CategoryMapper categoryMapper;
// 注意:需要创建异常
// 注意:需要在CategoryMapper中补充getById()方法,至少返回:depth
// 注意:需要在CategoryMapper中补充updateIsParentById()方法
public void addNew(CategoryAddNewDTO categoryAddNewDTO) {
// 从参数中取出尝试添加的类别的名称
// 调用categoryMapper.getByName()方法查询
// 判断查询结果是否不为null
// 是:抛出ServiceException
// 从参数中取出父级类别的id:parentId
// 判断parentId是否为0
// 是:此次尝试添加的是一级类别,没有父级类别,则当前depth >>> 1
// 否:此次尝试添加的不是一级类别,则应该存在父级类别,调用categoryMapper.getById()方法查询父级类别的信息
// -- 判断查询结果是否为null
// -- 是:抛出ServiceException
// -- 否:当前depth >>> 父级depth + 1
// 创建Category对象
// 调用BeanUtils.copyProperties()将参数对象中的属性值复制到Category对象中
// 补全Category对象中的属性值:depth >>> 前序运算结果
// 补全Category对象中的属性值:enable >>> 1(默认即启用)
// 补全Category对象中的属性值:isParent >>> 0
// 补全Category对象中的属性值:gmtCreate, gmtModified >>> LocalDateTime.now()
// 调用categoryMapper.insert(Category)插入类别数据,获取返回的受影响的行数
// 判断返回的受影响的行数是否不为1
// 是:抛出ServiceException
// 判断父级类别的isParent是否为0
// 是:调用categoryMapper.updateIsParentById()方法,将父级类别的isParent修改为1,获取返回的受影响的行数
// 判断返回的受影响的行数是否不为1
// 是:抛出ServiceException
}
要实现以上业务,需要先在持久层完成“根据id查询类别信息”的功能,则在CategorySimpleVO中添加private Integer depth;属性(原getByName()方法对应的查询也作对应的修改,虽然不是必须的)。
然后,还需要在CategorySimpleVO中补充private Integer isParent;属性,并且,必须在接下的查询中,查出此值。
然后CategeoryMapper接口中添加:
CategorySimpleVO getById(Long id);
然后在CategoryMapper.xml中配置以上方法映射的SQL:
<!-- CategorySimpleVO getById(Long id); -->
<select id="getById" resultMap="SimpleResultMap">
select id, depth from pms_category where id=#{id}
</select>
完成后,还需要在CategoryMapperTests中添加2个测试,以检验以上功能是否正常运行:
@Test
@Sql({"classpath:truncate.sql", "classpath:insert_data.sql"})
public void testGetByIdSuccessfully() {
// 测试数据
Long id = 1L;
// 断言不会抛出异常
assertDoesNotThrow(() -> {
// 执行查询
CategorySimpleVO category = mapper.getById(id);
// 断言查询结果不为null
assertNotNull(category);
});
}
@Test
@Sql({"classpath:truncate.sql"})
public void testGetByIdFailBecauseNotFound() {
// 测试数据
Long id = -1L;
// 断言不会抛出异常
assertDoesNotThrow(() -> {
// 执行查询
CategorySimpleVO category = mapper.getById(id);
// 断言查询结果为null
assertNull(category);
});
}
在CategoryMapper接口中添加:
int updateIsParentById(@Param("id") Long id, @Param("isParent") Integer isParent);
然后在CategoryMapper.xml中配置以上方法映射的SQL:
<!-- int updateIsParentById(@Param("id") Long id, @Param("isParent") Integer isParent); -->
<update id="updateIsParentById">
update pms_category set is_parent=#{isParent} where id=#{id}
</update>
完成后,还需要在CategoryMapperTests中添加2个测试,以检验以上功能是否正常运行:
@Test
@Sql({"classpath:truncate.sql", "classpath:insert_data.sql"})
public void testUpdateIsParentByIdSuccessfully() {
// 测试数据
Long id = 1L;
Integer isParent = 1;
// 断言不会抛出异常
assertDoesNotThrow(() -> {
// 执行测试
int rows = mapper.updateIsParentById(id, isParent);
// 断言受影响的行数为1
assertEquals(1, rows);
});
}
@Test
@Sql({"classpath:truncate.sql"})
public void testUpdateIsParentByIdFailBecauseNotFound() {
// 测试数据
Long id = -1L;
Integer isParent = 1;
// 断言不会抛出异常
assertDoesNotThrow(() -> {
// 执行测试
int rows = mapper.updateIsParentById(id, isParent);
// 断言受影响的行数为0
assertEquals(0, rows);
});
}
在实现业务逻辑之前,还需要创建自定义的异常类型,由于后续还有不少需要被多个Module共同使用的类、接口等,所以,此异常类型和后续可能被共用的类、接口都应该放在一个公共的Module中,则在Project下创建csmall-common这个新的Module,创建成功后,需要:
- 在
csmall-common中,修改pom.xml中的父项目 - 在
csmall-common中,在pom.xml删除依赖项 - 在
csmall-common中,在pom.xml删除<build>配置 - 在
csmall-common中,删除src/test - 在
csmall-common中,删除src/main/resources - 在
csmall-common中,删除启动类 - 在Project的
pom.xml中,添加<module> - 在Project的
pom.xml中,添加对新Module的依赖管理 - 在
csmall-product-webapi中的pom.xml中,添加对csmall-common的依赖
在csmall-common的根包下创建ex.ServiceException类:
public class ServiceException extends RuntimeException {
// 暂时不加构造方法
}
然后,在csmall-product-webapi中的CategoryServiceImpl中实现业务:
package cn.tedu.csmall.product.webapi.service;
import cn.tedu.csmall.common.ex.ServiceException;
import cn.tedu.csmall.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.pojo.entity.Category;
import cn.tedu.csmall.pojo.vo.CategorySimpleVO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.webapi.mapper.CategoryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
@Service
public class CategoryServiceImpl implements ICategoryService {
@Autowired
CategoryMapper categoryMapper;
@Override
public void addNew(CategoryAddNewDTO categoryAddNewDTO) {
// 从参数中取出尝试添加的类别的名称
String name = categoryAddNewDTO.getName();
// 调用categoryMapper.getByName()方法查询
CategorySimpleVO queryResult = categoryMapper.getByName(name);
// 判断查询结果是否不为null
if (queryResult != null) {
// 是:抛出ServiceException
throw new ServiceException();
}
// 从参数中取出父级类别的id:parentId
Long parentId = categoryAddNewDTO.getParentId();
// 判断parentId是否为0,当前尝试新增的类别的depth默认为1
Integer depth = 1;
CategorySimpleVO parentCategory = null;
if (parentId != 0) {
// 否:此次尝试添加的不是一级类别,则应该存在父级类别,调用categoryMapper.getById()方法查询父级类别的信息
parentCategory = categoryMapper.getById(parentId);
// -- 判断查询结果是否为null
if (parentCategory == null) {
// -- 是:抛出ServiceException
throw new ServiceException();
}
// -- 否:当前depth >>> 父级depth + 1
depth = parentCategory.getDepth() + 1;
}
// 创建Category对象
Category category = new Category();
// 调用BeanUtils.copyProperties()将参数对象中的属性值复制到Category对象中
BeanUtils.copyProperties(categoryAddNewDTO, category);
// 补全Category对象中的属性值:depth >>> 前序运算结果
category.setDepth(depth);
// 补全Category对象中的属性值:enable >>> 1(默认即启用)
category.setEnable(1);
// 补全Category对象中的属性值:isParent >>> 0
category.setIsParent(0);
// 补全Category对象中的属性值:gmtCreate, gmtModified >>> LocalDateTime.now()
LocalDateTime now = LocalDateTime.now();
category.setGmtCreate(now);
category.setGmtModified(now);
// 调用categoryMapper.insert(Category)插入类别数据,获取返回的受影响的行数
int rows = categoryMapper.insert(category);
// 判断返回的受影响的行数是否不为1
if (rows != 1) {
// 是:抛出ServiceException
throw new ServiceException();
}
// 判断父级类别的isParent是否为0
// 以下判断条件有部分多余,但不会报错
if (parentId != 0 && parentCategory != null && parentCategory.getIsParent() == 0) {
// 是:调用categoryMapper.updateIsParentById()方法,将父级类别的isParent修改为1,获取返回的受影响的行数
rows = categoryMapper.updateIsParentById(parentId, 1);
// 判断返回的受影响的行数是否不为1
if (rows != 1) {
// 是:抛出ServiceException
throw new ServiceException();
}
}
}
}
9.3. 测试
在src/test/java下的根包下创建service.CategoryServiceTests测试类,编写并执行测试:
package cn.tedu.csmall.product.webapi.service;
import cn.tedu.csmall.common.ex.ServiceException;
import cn.tedu.csmall.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.service.ICategoryService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
@SpringBootTest
public class CategoryServiceTests {
@Autowired
ICategoryService service;
@Test
@Sql("classpath:truncate.sql")
public void testAddNewSuccessfully() {
// 测试数据
CategoryAddNewDTO category = new CategoryAddNewDTO();
category.setName("大屏智能手机");
category.setParentId(0L);
category.setIcon("未上传类别图标");
category.setKeywords("未设置关键字");
category.setSort(88);
category.setIsDisplay(1);
// 断言不会抛出异常
assertDoesNotThrow(() -> {
// 执行测试
service.addNew(category);
});
}
@Test
@Sql({"classpath:truncate.sql", "classpath:insert_data.sql"})
public void testAddNewFailBecauseNameDuplicate() {
// 测试数据
CategoryAddNewDTO category = new CategoryAddNewDTO();
category.setName("类别001");
// 断言不会抛出异常
assertThrows(ServiceException.class, () -> {
// 执行测试
service.addNew(category);
});
}
@Test
@Sql({"classpath:truncate.sql"})
public void testAddNewFailBecauseParentNotFound() {
// 测试数据
CategoryAddNewDTO category = new CategoryAddNewDTO();
category.setName("类别001");
category.setParentId(-1L);
// 断言不会抛出异常
assertThrows(ServiceException.class, () -> {
// 执行测试
service.addNew(category);
});
}
}
10. 基于Spring JDBC的事务管理
事务:是一种能够保证同一个业务中多个写(增删改)操作要么全部成功,要么失败的机制!
在业务方法上添加@Transactional即可保证此方法是业务性(要么全部成功,要么全部失败)的。
在Spring JDBC中,处理事务的机制大致是:
开启事务:Begin
try {
你的业务方法
提交:Commit
} catch (RuntimeException e) {
回滚:Rollback
}
所以,为了保证事务性,所有的写操作在执行之后,必须有某个判定为失败的标准,且判断定为失败后,必须抛出RuntimeException或其子孙类异常!
- Spring JDBC默认对
RuntimeException进行回滚处理,有必要的话,也可以配置为其它异常类型
11. 类别管理--添加类别--控制器层
12. 类别管理--添加类别--前端页面
4-8 CS后台项目练习-2的更多相关文章
- 4-11 CS后台项目-4 及 Redis缓存数据
使用Redis缓存数据 使用Redis可以提高查询效率,一定程度上可以减轻数据库服务器的压力,从而保护了数据库. 通常,应用Redis的场景有: 高频查询,例如:热搜列表.秒杀 改变频率低的数据,例如 ...
- 4-7 CS后台项目练习-1
1. 关于此项目 此项目是一个自营性质电商类型的项目. 当前目标是设计后台管理相关功能. 2. 关于项目的开发流程 开发项目的标准流程应该有:需求分析.可行性分析.总体设计.详细设计等. 建议课后学习 ...
- 4-10 CS后台项目练习-3 || Redis
13. 类别管理--根据id查询类别详情--持久层 13.1. 规划SQL语句 本次需要执行的SQL语句大致是: select * from pms_category where id=? 关于字段列 ...
- CS通用项目系统搭建——三层架构第一天
CS通用项目:使用三层架构进行搭建 三层架构: 表现层(UI(User Interface)):展示给用户的层面,包含窗体控件数据等信息. 业务逻辑层(BLL(Business Logic Layer ...
- php大力力 [039节] 修改一下后台项目,同时启用印象笔记,要做的事情todo列表,记录在印象笔记,速度快一些
php大力力 [039节] 修改一下后台项目,同时启用印象笔记,要做的事情todo列表,记录在印象笔记,速度快一些
- winform WebBrowser控件中,cs后台代码执行动态生成的js
很多文章都是好介绍C# 后台cs和js如何交互,cs调用js方法(js方法必须是页面上存在的,已经定义好的),js调用cs方法, 但如果想用cs里面执行动态生成的js代码,如何实现呢? 思路大致是这样 ...
- 运行PHP后台项目:xampp下载,安装,配置,运行PHP的web项目
本来没有想着弄PHP,但是有同学叫我帮忙启动一下一个PHP写的后台.着实需要去学习一下. 想着安装xampp软件,一个集合了多个服务器,多个数据库,多个后台语言的管理软件. 一.xampp下载 二.安 ...
- react_结合 redux - 高阶函数 - 高阶组件 - 前端、后台项目打包运行
Redux 独立的集中式状态管理 js 库 - 参见 My Git 不是 react 库,可以与 angular.vue 配合使用,通常和 react 用 yarn add redux import ...
- 使用Java+MySQL+Apache开发后台项目(一)
做前端开发的人越来越多,后端维护的人才越来越稀缺,这种趋势正在慢慢扩展.像我这种人总喜欢反其道而行之,做后端开发的人虽然减少了,但是工作量和工作资质都要求的更高了,随着人工智能的发展,需要后台处理的数 ...
随机推荐
- redis 添加hash报错
报错信息 127.0.0.1:6379> hset ii name ss (error) MISCONF Redis is configured to save RDB snapshots, b ...
- Linux 环境变量配置的 6 种方法,建议收藏
关注「开源Linux」,选择"设为星标" 回复「学习」,有我为您特别筛选的学习资料~ Linux环境变量配置 在自定义安装软件的时候,经常需要配置环境变量,下面列举出各种对环境变量 ...
- call()、apply()、arguments
一.call(),apply() 1.作为函数对象(指函数方法名,不带括号)的方法,需要通过函数对象调用:当对函数调用这两个方法时都会调用函数执行. <script> // 这个函数中,f ...
- 海量数据存储ClickHouse
ClickHouse介绍 ClickHouse的由来和应用场景 俄罗斯Yandex在2016年开源,使用C++编写的列式存储数据库,近几年在OLAP领域大范围应用 官网:https://clickho ...
- HCNP Routing&Switching之链路聚合
前文我们了解了MSTP相关话题,回顾清参考https://www.cnblogs.com/qiuhom-1874/p/16268682.html:今天我们来聊一聊链路聚合相关话题: 链路聚合是链路高可 ...
- SpringCloud基础概念学习笔记(Eureka、Ribbon、Feign、Zuul)
SpringCloud基础概念学习笔记(Eureka.Ribbon.Feign.Zuul) SpringCloud入门 参考: https://springcloud.cc/spring-cloud- ...
- Vben Admin 源码学习:项目初始化
0x00 前言 Vue-Vben-Admin 是一个免费开源的中后台模版.使用了最新的vue3,vite2,TypeScript等主流技术开发,开箱即用的中后台前端解决方案考. 本系列本着学习参考的目 ...
- 我的第一篇随笔-Test
用于测试 中文字符 English character
- 文字轮播与图片轮播?CSS 不在话下
今天,分享一个实际业务中能够用得上的动画技巧. 巧用逐帧动画,配合补间动画实现一个无限循环的轮播效果,像是这样: 看到上述示意图,有同学不禁会发问,这不是个非常简单的位移动画么? 我们来简单分析分析, ...
- Eureka服务下线太慢,电话被告警打爆了
某年某月的某一天,就像一张破碎的脸... 错了,重来. 某天,忽然发现大量的告警,经过多番调查研究考察(此处省略3000字),发现是由于 Eureka 服务下线太慢,而仍然有大量的请求打进来导致的报错 ...