1、实体类加字段
/**
* 自定义的字段,此注解代表表里不存在
*/
@TableField(exist = false)
private Long[] catagoryPath;
2、controller层
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
@Autowired
private AttrGroupService attrGroupService;
@Autowired
private CategoryService categoryService;
@RequestMapping("/info/{attrGroupId}")
public R info(@PathVariable("attrGroupId") Long attrGroupId){
AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
//获取所属分类id
Long catelogId = attrGroup.getCatelogId();
Long path[] = categoryService.findCatelogPath(catelogId);
attrGroup.setCatagoryPath(path);
return R.ok().put("attrGroup", attrGroup);
}
}
3、service接口类
public interface CategoryService extends IService<CategoryEntity> {
/**
* 根据所属分类id查找出所有它的长辈们
* 例如当前分类id是255,父亲分类id是 50,爷爷分类id是 20,重爷爷分类id是5,老祖宗是0
* 父 子 孙 ,返回一个数组列表集合 []
* @param catelogId
* @return
*/
Long[] findCatelogPath(Long catelogId);
}
4、service实现类
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
@Override
public Long[] findCatelogPath(Long catelogId) {
//创建列表用来装当前分类id及所有父分类的id
ArrayList<Long> paths = new ArrayList<>();
List<Long> parentPath = findParentPath(catelogId, paths);
//将数据子父类的分类id 逆序排列
Collections.reverse(parentPath);
return parentPath.toArray(new Long[parentPath.size()]);
}
private List<Long> findParentPath(Long catelogId, ArrayList<Long> paths){
//收集当前节点分类id
paths.add(catelogId);
//根据当前分类id找到具体实体类信息
CategoryEntity entity = this.getById(catelogId);
//判断当前分类实体的父类是否是一级分类0
if (entity.getParentCid() != 0){
//如果当前分类实体的父亲不是一级分类0,就获取父类的id
findParentPath(entity.getParentCid(),paths);
}
//如果父分类找到是0后,直接返回所有当前分类实体的父分类集合
return paths;
}
}
5、测试
@Autowired
CategoryService categoryService;
@Test
public void test(){
Long[] paths = categoryService.findCatelogPath(183l);
for (Long path : paths) {
System.out.println(path);
}
}
返回结果为
2021-06-09 15:02:18.232 INFO 15884 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-06-09 15:02:19.586 INFO 15884 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-06-09 15:02:19.597 DEBUG 15884 --- [ main] c.l.g.p.dao.CategoryDao.selectById : ==> Preparing: SELECT cat_id,parent_cid,icon,name,show_status,product_unit,sort,product_count,cat_level FROM pms_category WHERE cat_id=? AND show_status=1
2021-06-09 15:02:19.629 DEBUG 15884 --- [ main] c.l.g.p.dao.CategoryDao.selectById : ==> Parameters: 183(Long)
2021-06-09 15:02:19.660 DEBUG 15884 --- [ main] c.l.g.p.dao.CategoryDao.selectById : <== Total: 1
2021-06-09 15:02:19.663 DEBUG 15884 --- [ main] c.l.g.p.dao.CategoryDao.selectById : ==> Preparing: SELECT cat_id,parent_cid,icon,name,show_status,product_unit,sort,product_count,cat_level FROM pms_category WHERE cat_id=? AND show_status=1
2021-06-09 15:02:19.663 DEBUG 15884 --- [ main] c.l.g.p.dao.CategoryDao.selectById : ==> Parameters: 26(Long)
2021-06-09 15:02:19.665 DEBUG 15884 --- [ main] c.l.g.p.dao.CategoryDao.selectById : <== Total: 1
2021-06-09 15:02:19.666 DEBUG 15884 --- [ main] c.l.g.p.dao.CategoryDao.selectById : ==> Preparing: SELECT cat_id,parent_cid,icon,name,show_status,product_unit,sort,product_count,cat_level FROM pms_category WHERE cat_id=? AND show_status=1
2021-06-09 15:02:19.667 DEBUG 15884 --- [ main] c.l.g.p.dao.CategoryDao.selectById : ==> Parameters: 1(Long)
2021-06-09 15:02:19.670 DEBUG 15884 --- [ main] c.l.g.p.dao.CategoryDao.selectById : <== Total: 1
1
26
183