谷粒商城学习——P45商品服务-API-三级分类-查询-递归树形结构数据获取
作者:互联网
业务:查询出商品分类并组合成树结构返回
关键代码
package com.atguigu.gulimall.product.service.impl; import org.springframework.stereotype.Service; import java.util.Comparator; 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.atguigu.common.utils.PageUtils; import com.atguigu.common.utils.Query; import com.atguigu.gulimall.product.dao.CategoryDao; import com.atguigu.gulimall.product.entity.CategoryEntity; import com.atguigu.gulimall.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、查出所有分类 List<CategoryEntity> entities = baseMapper.selectList(null); //2、组装成父子的树形结构 //2.1)、找到所有的一级分类 return entities.stream().filter(x -> x.getParentCid() == 0).peek(x-> x.setChildren(getChildren(x,entities))) .sorted(Comparator.comparingInt(CategoryEntity::getSort)).collect(Collectors.toList()); // entities.stream().filter(x -> x.getParentCid() == 0).map(x->{ // x.setChildren(getChildren(x,entities)); // return x; // }).sorted(Comparator.comparingInt(CategoryEntity::getSort)).collect(Collectors.toList()); } private List<CategoryEntity> getChildren(CategoryEntity x, List<CategoryEntity> entities) { return entities.stream().filter(y -> x.getCatId().equals(y.getParentCid())).peek(z-> z.setChildren(getChildren(z,entities))).sorted(Comparator.comparingInt(CategoryEntity::getSort)).collect(Collectors.toList()); } }View Code
知识点:
1.baseMapper.selectList
CategoryServiceImpl 继承了ServiceImpl,传入了泛型CategoryDao(M),ServiceImpl中又注入了protected M baseMapper;,因此可以直接使用baseMapper代表
ServiceImpl<>中泛型的mapper
CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity>
public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> { protected Log log = LogFactory.getLog(this.getClass()); @Autowired protected M baseMapper;
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
2. .map,map 方法用于映射每个元素到对应的结果,参考java菜鸟教程:java8新特性。示例:以下代码片段使用 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());
3. peek peek和map用法一样,唯一不同的是,参考Java 8 Stream peek 与 map的区别
peek接收一个没有返回值的λ表达式,可以做一些输出,外部处理等。map接收一个有返回值的λ表达式
4.sort。流元素按照哪个规则排序。用法
.stream().sorted((x1,x2)-> x1-x2)
x1和x2代表列表相邻的两个元素,x1在前x2在后
x1-x2<0 不变
x1-x2>0 交换位置
如果上面这种写法不涉及其他逻辑,只是按照对象的某个属性排序,可以简化为java8方法引用的语法(方法引用使用一对冒号 :: 。)
标签:map,ServiceImpl,List,entities,API,谷粒,import,P45,com 来源: https://www.cnblogs.com/yanan7890/p/14877991.html