category_info
CREATE TABLE category_info
(
category_id
int NOT NULL AUTO_INCREMENT COMMENT ‘自增分类ID’,
category_code
varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT ‘分类编码’,
category_name
varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT ‘分类名称’,
p_category_id
int NOT NULL COMMENT ‘父级分类ID’,
icon
varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT ‘图标’,
background
varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT ‘背景图’,
sort
tinyint NOT NULL COMMENT ‘排序号’,
PRIMARY KEY (category_id
) USING BTREE,
UNIQUE KEY idx_key_category_code
(category_code
) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=62 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT=‘分类信息’;
加载所有分类
Controller
@RequestMapping("/loadCategory")
public ResponseVO loadAllCategory(CategoryInfoQuery categoryInfo) {
//设置排序模式
categoryInfo.setOrderBy("sort asc");
//设置查询每个分类的子分类
categoryInfo.setConvert2Tree(true);
List<CategoryInfo> categoryInfoList = categoryInfoService.findListByParam(categoryInfo);
return getSuccessResponseVO(categoryInfoList);
}
Service
categoryInfoService.findListByParam(categoryInfo);
@Override
public List<CategoryInfo> findListByParam(CategoryInfoQuery param) {
List<CategoryInfo> categoryInfoList = this.categoryInfoMapper.selectList(param);
if (param.getConvert2Tree() != null && param.getConvert2Tree()) {
//从列表形式变成树状,Constants.ZERO代表父分类的id为0(顶级分类)
categoryInfoList = convertLine2Tree(categoryInfoList, Constants.ZERO);
}
return categoryInfoList;
}
private List<CategoryInfo> convertLine2Tree(List<CategoryInfo> dataList, Integer pid) {
List<CategoryInfo> children = new ArrayList();
for (CategoryInfo m : dataList) {
//最低级的分类就不会进入这个if语句,即为该递归函数的出口
// m.getpCategoryId().equals(pid)根据传入的pid构建树
if (m.getCategoryId() != null && m.getpCategoryId() != null && m.getpCategoryId().equals(pid)) {
//递归调用,此时m的分类id为m的子分类
m.setChildren(convertLine2Tree(dataList, m.getCategoryId()));
children.add(m);
}
}
return children;
}
保存分类
Controller
@RequestMapping("/saveCategory")
public ResponseVO saveCategory(@NotNull Integer pCategoryId,
Integer categoryId,
@NotEmpty String categoryCode,
@NotEmpty String categoryName,
String icon,
String background) {
categoryInfoService.saveCategory(pCategoryId, categoryCode, categoryName, icon,background,categoryId);
return getSuccessResponseVO(null);
}
Service
categoryInfoService.saveCategory
@Override
public void saveCategory(Integer pCategoryId, String categoryCode, String categoryName, String icon, String background, Integer categoryId) {
CategoryInfo categoryInfo = categoryInfoMapper.selectByCategoryCode(categoryCode);
//分类编号需唯一,新增分类时传入的categoryId为空
f(categoryId == null && categoryInfo!=null){
throw new BusinessException("分类编号已存在");
}
//没传入pid既默认为顶级分类
if (pCategoryId==null){
pCategoryId=0;
}
//该分类已存在,就更新对应信息
categoryInfo = categoryInfoMapper.selectByCategoryId(categoryId);
if(categoryInfo!=null){
categoryInfo.setCategoryName(categoryName);
categoryInfo.setpCategoryId(pCategoryId);
categoryInfo.setCategoryCode(categoryCode);
categoryInfo.setIcon(icon);
categoryInfo.setBackground(background);
categoryInfoMapper.updateByCategoryId(categoryInfo,categoryId);
//更新redis
this.upateRedis();
return;
}
//新增分类
//查询最大的sort
Integer sort = categoryInfoMapper.selectMaxSort(pCategoryId);
sort++;
categoryInfo = new CategoryInfo();
categoryInfo.setCategoryName(categoryName);
categoryInfo.setpCategoryId(pCategoryId);
categoryInfo.setCategoryCode(categoryCode);
categoryInfo.setIcon(icon);
categoryInfo.setBackground(background);
categoryInfo.setSort(sort);
categoryInfoMapper.insert(categoryInfo);
//更新redis
this.upateRedis();
}
upateRedis();
private void upateRedis(){
//查询所有父分类
List<CategoryInfo> parents = categoryInfoMapper.selectParentList();
for (CategoryInfo parent : parents) {
//为每个父分类添加其子分类
List<CategoryInfo> children = categoryInfoMapper.selectByParetnId(parent.getCategoryId());
parent.setChildren(children);
}
redisComponent.saveCategoryList(parents);
}
upateRedis();
public void saveCategoryList(List<CategoryInfo> categoryInfoList) {
redisUtils.set(Constants.REDIS_KEY_CATEGORY_LIST, categoryInfoList);
}
删除分类
Controller
@RequestMapping("/delCategory")
public ResponseVO delCategory(@NotNull Integer categoryId) {
categoryInfoService.delCategory(categoryId);
return getSuccessResponseVO(null);
}
Service
categoryInfoService.delCategory(categoryId);
@Override
public void delCategory(Integer categoryId) {
VideoInfoQuery videoInfoQuery = new VideoInfoQuery();
videoInfoQuery.setCategoryIdOrPCategoryId(categoryId);
//根据分类id查询视频数量
Integer count = videoInfoService.findCountByParam(videoInfoQuery);
if (count > 0) {
throw new BusinessException("分类下有视频信息,无法删除");
}
CategoryInfoQuery categoryInfoQuery = new CategoryInfoQuery();
categoryInfoQuery.setCategoryIdOrPCategoryId(categoryId);
categoryInfoMapper.deleteByParam(categoryInfoQuery);
//刷新缓存
upateRedis();
}
VideoInfoMapper.xml
<!--补充的条件-->
<if test="query.categoryIdOrPCategoryId!=null">
and (category_id = #{query.categoryIdOrPCategoryId} or p_category_id = #{query.categoryIdOrPCategoryId})
</if>
改变分类排序
Controller
@RequestMapping("/changeSort")
public ResponseVO changeSort(@NotNull Integer pCategoryId,
@NotEmpty String categoryIds) {
categoryInfoService.changeSort(pCategoryId, categoryIds);
return getSuccessResponseVO(null);
}
Service categoryInfoService.changeSort(pCategoryId, categoryIds);
public void changeSort(Integer pCategoryId, String categoryIds) {
//使用数组形式将新的的分类id列表以要排列的顺序传入
String[] split = categoryIds.split(",");
int idx=1;
for (String s : split) {
//根据数组中的id查询分类
CategoryInfo categoryInfo = categoryInfoMapper.selectByCategoryId(Integer.parseInt(s));
//设置排序
categoryInfo.setSort(idx++);
//更新信息
categoryInfoMapper.updateByCategoryId(categoryInfo,categoryInfo.getCategoryId());
}
//更新redis
this.upateRedis();
}