在CategoryController里修改添加 list中的方法
package com.yu.tesemeishi.product.controller;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.yu.tesemeishi.product.entity.CategoryEntity;
import com.yu.tesemeishi.product.service.CategoryService;
import com.yu.common.utils.PageUtils;
import com.yu.common.utils.R;
/**
* 商品三级分类
*
* @date 2021-06-26 13:34:27
*/
@RestController
@RequestMapping("product/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
/**
* 列表
*/
@RequestMapping("/list/tree")
//@RequiresPermissions("product:category:list")
public R list(@RequestParam Map<String, Object> params){
//查出所有分类以及子分类,以树结构组装起来
List<CategoryEntity> categoryEntities = categoryService.listWithTree();
return R.ok().put("data", categoryEntities);
}
/**
* 信息
*/
@RequestMapping("/info/{catId}")
//@RequiresPermissions("product:category:info")
public R info(@PathVariable("catId") Long catId){
CategoryEntity category = categoryService.getById(catId);
return R.ok().put("category", category);
}
/**
* 保存
*/
@RequestMapping("/save")
//@RequiresPermissions("product:category:save")
public R save(@RequestBody CategoryEntity category){
categoryService.save(category);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
//@RequiresPermissions("product:category:update")
public R update(@RequestBody CategoryEntity category){
categoryService.updateById(category);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
//@RequiresPermissions("product:category:delete")
public R delete(@RequestBody Long[] catIds){
categoryService.removeByIds(Arrays.asList(catIds));
return R.ok();
}
}
对应的在service中修改
package com.yu.tesemeishi.product.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.yu.common.utils.PageUtils;
import com.yu.tesemeishi.product.entity.CategoryEntity;
import java.util.List;
import java.util.Map;
/**
* 商品三级分类
*
* @author yudengsheng
* @email yudengsheng@gmail.com
* @date 2021-06-26 13:34:27
*/
public interface CategoryService extends IService<CategoryEntity> {
PageUtils queryPage(Map<String, Object> params);
List<CategoryEntity> listWithTree();
}
对应的实现类
package com.yu.tesemeishi.product.service.impl;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.common.utils.PageUtils;
import com.yu.common.utils.Query;
import com.yu.tesemeishi.product.dao.CategoryDao;
import com.yu.tesemeishi.product.entity.CategoryEntity;
import com.yu.tesemeishi.product.service.CategoryService;
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<CategoryEntity> page = this.page(
new Query<CategoryEntity>().getPage(params),
new QueryWrapper<CategoryEntity>()
);
return new PageUtils(page);
}
@Override
public List<CategoryEntity> listWithTree() {
//1、查出所有分类 要查那个表就是查那个dao的entity
List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
//2、组装成父子树形结构
return categoryEntities;
}
}
启动项目看看又没有数据
http://localhost:8010/product/category/list/tree
从所有中拿到一级分类:添加在实现类中
List<CategoryEntity> collect1Menus = categoryEntities.stream().filter(categoryEntity ->
categoryEntity.getParentCid() == 0
).collect(Collectors.toList());
二級菜單 在实现类中编写还需要在CategoryEntity实体类中添加一个属性
/**
* 自定义的属性,用于存储子分类数据
*/
@TableField(exist=false)
private List<CategoryEntity> children;
package com.yu.tesemeishi.product.service.impl;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.common.utils.PageUtils;
import com.yu.common.utils.Query;
import com.yu.tesemeishi.product.dao.CategoryDao;
import com.yu.tesemeishi.product.entity.CategoryEntity;
import com.yu.tesemeishi.product.service.CategoryService;
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<CategoryEntity> page = this.page(
new Query<CategoryEntity>().getPage(params),
new QueryWrapper<CategoryEntity>()
);
return new PageUtils(page);
}
@Override
public List<CategoryEntity> listWithTree() {
//1、查出所有分类 要查那个表就是查那个dao的entity
List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
//2、组装成父子树形结构
//2.1先找到所有的一节分类
// List<CategoryEntity> collect1Menus = categoryEntities.stream().filter((categoryEntity) -> {
// return categoryEntity.getCatId() == 0;
// }).collect(Collectors.toList());
//简化的写法
List<CategoryEntity> collect1Menus = categoryEntities.stream().filter(categoryEntity ->
categoryEntity.getParentCid() == 0
).map((menu)->{
menu.setChildren(getChildrens(menu,categoryEntities));
return menu;
}).sorted((menu1,menu2) -> { //sorted 用來排序
return (menu1.getSort() == null? 0 : menu1.getSort()) - (menu2.getSort() == null? 0 : menu2.getSort());
}).collect(Collectors.toList());
return collect1Menus;
}
/**
* 递归找所有的子菜单、中途要排序
*/
private List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> all){
List<CategoryEntity> children = all.stream().filter(categoryEntity ->
categoryEntity.getParentCid() == root.getCatId()
).map(categoryEntity -> {
categoryEntity.setChildren(getChildrens(categoryEntity, all));
return categoryEntity;
}).sorted((menu1,menu2) -> {
return (menu1.getSort() == null? 0 : menu1.getSort()) - (menu2.getSort() == null? 0 : menu2.getSort());
}).collect(Collectors.toList());
return children;
}
}
测试结果