数据库表设计:
CREATE TABLE `pms_category` (
`cat_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '分类id',
`name` char(50) DEFAULT NULL COMMENT '分类名称',
`parent_cid` bigint(20) DEFAULT NULL COMMENT '父分类id',
`cat_level` int(11) DEFAULT NULL COMMENT '层级',
`sort` int(11) DEFAULT NULL COMMENT '排序',
PRIMARY KEY (`cat_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1433 DEFAULT CHARSET=utf8mb4 COMMENT='商品三级分类';
部分数据:
Entity:
@Data
@TableName("pms_category")
public class CategoryEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 分类id
*/
@TableId
private Long catId;
/**
* 分类名称
*/
private String name;
/**
* 父分类id
*/
private Long parentCid;
/**
* 层级
*/
private Integer catLevel;
/**
* 排序
*/
private Integer sort;
@TableField(exist=false)
private List<CategoryEntity> children;
}
Dao接口:
// 这里用的是mybatis-plus
@Mapper
public interface CategoryDao extends BaseMapper<CategoryEntity> {
}
Service接口:
public interface CategoryService extends IService<CategoryEntity> {
List<CategoryEntity> listWithTree();
}
Service实现:
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
@Override
public List<CategoryEntity> listWithTree() {
// 1 查所有分类
// baseMapper相当于CategoryDao,
List<CategoryEntity> entities = baseMapper.selectList(null);
// 2 构成三级列表
List<CategoryEntity> level1Menus = entities.stream().filter(categoryEntity ->
categoryEntity.getParentCid() == 0)
.map(menu -> {
menu.setChildren(getChildren(menu, entities));
return menu;
})
.sorted((menu1, menu2) -> {
return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
})
.collect(Collectors.toList());
return level1Menus;
}
/**
* 设置子菜单
* @param root 当前要设置子菜单的对象
* @param all 所有菜单数据
* @return
*/
private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all) {
List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
return categoryEntity.getParentCid() == root.getCatId();
}).map(categoryEntity -> {
categoryEntity.setChildren(getChildren(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;
}
}
结果显示:
流式API
(1)filter
filter用于通过设置的条件过滤出需要的元素
// 只需要取出parentCid为0的实体对象,即一级条目
List<CategoryEntity> level1Menus = entities.stream().filter(categoryEntity ->
categoryEntity.getParentCid() == 0)
(2)map
map用于改变每个元素,返回改变后的元素
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
// 获取对应的平方数
List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());
// 通过filter()过滤之后给一级实体对象设置子元素(二级和三级条目)
.map(menu -> {
menu.setChildren(getChildren(menu, entities));
return menu;
})
(3)sorted
sorted用于排序
// 对输出的 10 个随机数进行排序
Random random = new Random();
random.ints().limit(10).sorted().forEach(System.out::println);
// 利用sort字段值进行排序
.sorted((menu1, menu2) -> {
return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
})