在 foodie-dev-service 模块下面的 com.imooc.service 包内创建 CategoryService 接口
package com.imooc.service;import com.imooc.pojo.Category;import java.util.List;/*** Created by 92578 on 2020/8/22 16:17**/public interface CategoryService {/*** 查询所有一级分类** @return*/public List<Category> queryAllRootLevelCat();}
在 foodie-dev-service 模块下面的 com.imooc.service.impl 包内创建 CategoryServiceImpl 类
package com.imooc.service.impl;import com.imooc.mapper.CategoryMapper;import com.imooc.pojo.Category;import com.imooc.service.CategoryService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import tk.mybatis.mapper.entity.Example;import java.util.List;/*** Created by 92578 on 2020/8/22 16:18**/@Servicepublic class CategoryServiceImpl implements CategoryService {@Autowiredprivate CategoryMapper categoryMapper;/*** 查询所有一级分类** @return*/@Transactional(propagation = Propagation.SUPPORTS)@Overridepublic 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;}}
在 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
找到获取商品分类(一级分类)接口,点击“调试”->“发送”,成功获取相应数据
访问 http://localhost:8080/foodie-shop/index.html
