在 foodie-dev-mapper 模块下的 com.imooc.mapper 包内创建 CategoryMapperCustom 接口

    1. package com.imooc.mapper;
    2. import com.imooc.pojo.vo.CategoryVO;
    3. import java.util.List;
    4. public interface CategoryMapperCustom {
    5. public List<CategoryVO> getSubCatList(Integer rootCatId);
    6. }

    在 foodie-dev-mapper 模块下的 resources 目录内创建 CategoryMapperCustom XML 文件
    resultMap:映射到某一个具体的 pojo 类,

    • id:代表主键
    • column:对应 sql 中的列
    • property:对应实体类中的属性名
    • collection:用于定义关联的 list 集合类型的封装规则

      • property:对应 pojo 类的属性
      • ofType:此属性对应的类型是什么 ```xml <?xml version=”1.0” encoding=”UTF-8”?> <!DOCTYPE mapper PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN” “http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    VO(View Object) 与 BO(Business Object) <br />BO 相当于业务型的,是从前端业务层封装过后的一些数据传入到后端,是发送一个请求过来的。如果是从内部传给前端,前端可以是 H5,可以是手机端,是展示在显示层的,显示层又称表现层(View Object)<br />在 foodie-dev-pojo 模块下新建 com.imooc.pojo.vo 包,创建 CategoryVO 类
    ```java
    package com.imooc.pojo.vo;
    
    import java.util.List;
    
    /**
     * 二级分类 VO
     * Created by 92578 on 2020/8/22 19:57
     **/
    public class CategoryVO {
    
        private Integer id;
        private String name;
        private String type;
        private Integer fatherId;
    
        // 三级分类 VO list
        private List<SubCategoryVO> subCatList;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public Integer getFatherId() {
            return fatherId;
        }
    
        public void setFatherId(Integer fatherId) {
            this.fatherId = fatherId;
        }
    
        public List<SubCategoryVO> getSubCatList() {
            return subCatList;
        }
    
        public void setSubCatList(List<SubCategoryVO> subCatList) {
            this.subCatList = subCatList;
        }
    }
    

    在 foodie-dev-pojo 模块下的 com.imooc.pojo.vo 包内创建 SubCategoryVO 类

    package com.imooc.pojo.vo;
    
    /**
     * Created by 92578 on 2020/8/22 20:01
     **/
    public class SubCategoryVO {
    
        private Integer subId;
        private String subName;
        private String subType;
        private Integer subFatherId;
    
        public Integer getSubId() {
            return subId;
        }
    
        public void setSubId(Integer subId) {
            this.subId = subId;
        }
    
        public String getSubName() {
            return subName;
        }
    
        public void setSubName(String subName) {
            this.subName = subName;
        }
    
        public String getSubType() {
            return subType;
        }
    
        public void setSubType(String subType) {
            this.subType = subType;
        }
    
        public Integer getSubFatherId() {
            return subFatherId;
        }
    
        public void setSubFatherId(Integer subFatherId) {
            this.subFatherId = subFatherId;
        }
    }
    

    在 foodie-dev-service 模块下的 CategoryService 接口添加 getSubCatlist 方法

    package com.imooc.service;
    
    import com.imooc.pojo.Category;
    import com.imooc.pojo.vo.CategoryVO;
    
    import java.util.List;
    
    /**
     * Created by 92578 on 2020/8/22 16:17
     **/
    public interface CategoryService {
    
        /**
         * 查询所有一级分类
         *
         * @return
         */
        public List<Category> queryAllRootLevelCat();
    
        /**
         * 根据一级分类 id 查询子分类信息
         *
         * @param rootCatId
         * @return
         */
        public List<CategoryVO> getSubCatlist(Integer rootCatId);
    }
    

    在 foodie-dev-service 模块下的 CategoryServiceImpl 类中实现 getSubCatlist 接口

    package com.imooc.service.impl;
    
    import com.imooc.mapper.CategoryMapper;
    import com.imooc.mapper.CategoryMapperCustom;
    import com.imooc.pojo.Category;
    import com.imooc.pojo.vo.CategoryVO;
    import com.imooc.service.CategoryService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    import tk.mybatis.mapper.entity.Example;
    
    import java.util.List;
    
    /**
     * Created by 92578 on 2020/8/22 16:18
     **/
    @Service
    public class CategoryServiceImpl implements CategoryService {
    
        @Autowired
        private CategoryMapper categoryMapper;
    
        @Autowired
        private CategoryMapperCustom categoryMapperCustom;
    
        /**
         * 查询所有一级分类
         *
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public List<Category> queryAllRootLevelCat() {
            Example example = new Example(Category.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("type", 1);
    
            List<Category> result = categoryMapper.selectByExample(example);
    
            return result;
        }
    
        /**
         * 根据一级分类 id 查询子分类信息
         *
         * @param rootCatId
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public List<CategoryVO> getSubCatlist(Integer rootCatId) {
            return categoryMapperCustom.getSubCatList(rootCatId);
        }
    }
    

    在 foodie-dev-api 模块下的 IndexController 类中添加 subCat 方法

    package com.imooc.controller;
    
    import com.imooc.enums.YseOrNo;
    import com.imooc.pojo.Carousel;
    import com.imooc.pojo.Category;
    import com.imooc.pojo.vo.CategoryVO;
    import com.imooc.service.CarouselService;
    import com.imooc.service.CategoryService;
    import com.imooc.utils.IMOOCJSONResult;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import io.swagger.annotations.ApiParam;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    /**
     * Created by 92578 on 2020/8/22 16:30
     **/
    @Api(value = "首页", tags = {"首页展示的相关接口"})
    @RestController
    @RequestMapping("index")
    public class IndexController {
    
        @Autowired
        private CarouselService carouselService;
    
        @Autowired
        private CategoryService categoryService;
    
        @ApiOperation(value = "获取首页轮播图列表", notes = "获取首页轮播图列表", httpMethod = "GET")
        @GetMapping("/carousel")
        public IMOOCJSONResult carousel() {
            List<Carousel> list = carouselService.queryAll(YseOrNo.YES.type);
    
            return IMOOCJSONResult.ok(list);
        }
    
        @ApiOperation(value = "获取商品分类(一级分类)", notes = "获取商品分类(一级分类)", httpMethod = "GET")
        @GetMapping("/cats")
        public IMOOCJSONResult cats() {
            List<Category> list = categoryService.queryAllRootLevelCat();
    
            return IMOOCJSONResult.ok(list);
        }
    
        @ApiOperation(value = "获取商品子分类", notes = "获取商品子分类", httpMethod = "GET")
        @GetMapping("/subCat/{rootCatId}")
        public IMOOCJSONResult subCat(
                @ApiParam(name = "rootCatId", value = "一级分类 id", required = true)
                @PathVariable Integer rootCatId) {
            if (rootCatId == null) {
                return IMOOCJSONResult.errorMsg("分类不存在");
            }
    
            List<CategoryVO> list = categoryService.getSubCatlist(rootCatId);
    
            return IMOOCJSONResult.ok(list);
        }
    }
    

    启动项目,访问 http://localhost:8088/doc.html
    找到“获取商品子分类”,点击“调试”,输入参数值“1”,点击“发送”
    image.png
    访问 http://localhost:8080/foodie-shop/index.html 子菜单已经可以展示出来
    image.png