在 foodie-dev-service 模块下面的 com.imooc.service 包内创建 CategoryService 接口

    1. package com.imooc.service;
    2. import com.imooc.pojo.Category;
    3. import java.util.List;
    4. /**
    5. * Created by 92578 on 2020/8/22 16:17
    6. **/
    7. public interface CategoryService {
    8. /**
    9. * 查询所有一级分类
    10. *
    11. * @return
    12. */
    13. public List<Category> queryAllRootLevelCat();
    14. }

    在 foodie-dev-service 模块下面的 com.imooc.service.impl 包内创建 CategoryServiceImpl 类

    1. package com.imooc.service.impl;
    2. import com.imooc.mapper.CategoryMapper;
    3. import com.imooc.pojo.Category;
    4. import com.imooc.service.CategoryService;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. import tk.mybatis.mapper.entity.Example;
    8. import java.util.List;
    9. /**
    10. * Created by 92578 on 2020/8/22 16:18
    11. **/
    12. @Service
    13. public class CategoryServiceImpl implements CategoryService {
    14. @Autowired
    15. private CategoryMapper categoryMapper;
    16. /**
    17. * 查询所有一级分类
    18. *
    19. * @return
    20. */
    21. @Transactional(propagation = Propagation.SUPPORTS)
    22. @Override
    23. public List<Category> queryAllRootLevelCat() {
    24. Example example = new Example(Category.class);
    25. Example.Criteria criteria = example.createCriteria();
    26. criteria.andEqualTo("type", 1);
    27. List<Category> result = categoryMapper.selectByExample(example);
    28. return result;
    29. }
    30. }

    在 foodie-dev-api 模块下面的 IndexController 类新增 cats 方法

    package com.imooc.controller;
    
    import com.imooc.enums.YseOrNo;
    import com.imooc.pojo.Carousel;
    import com.imooc.pojo.Category;
    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 org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    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);
        }
    }
    

    启动项目,打开浏览器,访问 http://localhost:8088/doc.html
    找到获取商品分类(一级分类)接口,点击“调试”->“发送”,成功获取相应数据
    image.png
    访问 http://localhost:8080/foodie-shop/index.html
    image.png