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开发后台项目(一)
做前端开发的人越来越多,后端维护的人才越来越稀缺,这种趋势正在慢慢扩展.像我这种人总喜欢反其道而行之,做后端开发的人虽然减少了,但是工作量和工作资质都要求的更高了,随着人工智能的发展,需要后台处理的数 ...
随机推荐
- 【CSAPP】Shell Lab 实验笔记
shlab这节是要求写个支持任务(job)功能的简易shell,主要考察了linux信号机制的相关内容.难度上如果熟读了<CSAPP>的"异常控制流"一章,应该是可以不 ...
- 一条Sql的执行过程
一条sql内部是如何执行的: 学习MySQL实战45专栏 sql中的内部执行图: 可以分为两部分:server和存储引擎 server层包含: 连接器.分析器.优化器.执行器,涵盖了MySQL大多数核 ...
- 一款高速的NET版的离线免费OCR
PaddleOCR.Onnx 一款基于Paddle的OCR,项目使用ONNX模型,速度更快.本项目同时支持X64和X86的CPU上使用.本项目是一个基于PaddleOCR的C++代码修改并封装的.NE ...
- CSAPP 之 AttackLab 详解
前言 本篇博客将会介绍 CSAPP 之 AttackLab 的攻击过程,利用缓冲区溢出错误进行代码注入攻击和 ROP 攻击.实验提供了以下几个文件,其中 ctarget 可执行文件用来进行代码注入攻击 ...
- 词云图value传递数据不显示(已解决)
问题描述: 今天在做词云图时,虽然词云图能够展现出来,但是后台传递过来的数据(每个词出现的次数)却不显示. 错误原因: 错误的将tooltip写在了series内部,如图: 解决方案: 将toolti ...
- PKUSC2022 游记
PKUSC2022 游记 Day1 上午随便看了点题,感觉没看什么题就开考了. 开考之后先看 T1,发现 T1 好像不是那么简单. T1 : 九条可怜有两个账号,她每次都会打 \(\rm rating ...
- 每天一个 HTTP 状态码 101
101 Switching Protocols 当客户端的请求具有 Upgrade HTTP 首部,表示要求服务器切换到指定协议:此时服务器端就可以向客户端响应 101 Switching Proto ...
- 动态调试JS脚本文件:(JS源映射 - sourceURL)与 debugger
我们在进行js调试时经常会对js进行调试,chrome 对js提示对支持非常友好,只需要F12就可以打开chrome的调试器 在sources里面就是页面请求后加载的一些资源文件,我们可以找到我们的j ...
- django框架10
内容概要 ajax结合sweetalert forms组件钩子函数 forms组件字段参数 forms组件字段类型 forms组件源码分析 cookie与session简介 django操作cooki ...
- Egg上层框架CabloyJS是如何输出SQL语句日志的?
背景 在Egg开发实践中,经常会遇到一个问题:如何查看刚刚执行过的Egg组装的原生SQL语句呢? 1. 现有方案 可以直接在项目的config配置文件中添加MySQL配置debug: true.这会启 ...