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

    1. package com.imooc.service;
    2. import com.imooc.pojo.Carousel;
    3. import java.util.List;
    4. /**
    5. * Created by 92578 on 2020/8/22 16:17
    6. **/
    7. public interface CarouselService {
    8. /**
    9. * 查询所有轮播图列表
    10. * @param isShow
    11. * @return
    12. */
    13. public List<Carousel> queryAll(Integer isShow);
    14. }

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

    1. package com.imooc.service.impl;
    2. import com.imooc.mapper.CarouselMapper;
    3. import com.imooc.pojo.Carousel;
    4. import com.imooc.service.CarouselService;
    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 CarouselServiceImpl implements CarouselService {
    14. @Autowired
    15. private CarouselMapper carouselMapper;
    16. /**
    17. * 查询所有轮播图列表
    18. *
    19. * @param isShow
    20. * @return
    21. */
    22. @Transactional(propagation = Propagation.SUPPORTS)
    23. @Override
    24. public List<Carousel> queryAll(Integer isShow) {
    25. Example example = new Example(Carousel.class);
    26. example.orderBy("sort").desc();
    27. Example.Criteria criteria = example.createCriteria();
    28. criteria.andEqualTo("isShow", isShow);
    29. List<Carousel> result = carouselMapper.selectByExample(example);
    30. return result;
    31. }
    32. }

    在 foodie-dev-common 模块下的 com.imooc.enums 包内创建 YseOrNo 枚举

    1. package com.imooc.enums;
    2. /**
    3. * 是否枚举
    4. * Created by 92578 on 2020/8/16 20:38
    5. **/
    6. public enum YseOrNo {
    7. NO(0, "否"),
    8. YES(1, "是");
    9. public final Integer type;
    10. public final String value;
    11. YseOrNo(Integer type, String value) {
    12. this.type = type;
    13. this.value = value;
    14. }
    15. }

    在 foodie-dev-api 模块下的 com.imooc.controller 包内创建 IndexController 类

    1. package com.imooc.controller;
    2. import com.imooc.enums.YseOrNo;
    3. import com.imooc.pojo.Carousel;
    4. import com.imooc.service.CarouselService;
    5. import com.imooc.utils.IMOOCJSONResult;
    6. import io.swagger.annotations.Api;
    7. import io.swagger.annotations.ApiOperation;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.web.bind.annotation.GetMapping;
    10. import org.springframework.web.bind.annotation.RequestMapping;
    11. import org.springframework.web.bind.annotation.RestController;
    12. import java.util.List;
    13. /**
    14. * Created by 92578 on 2020/8/22 16:30
    15. **/
    16. @Api(value = "首页", tags = {"首页展示的相关接口"})
    17. @RestController
    18. @RequestMapping("index")
    19. public class IndexController {
    20. @Autowired
    21. private CarouselService carouselService;
    22. @ApiOperation(value = "获取首页轮播图列表", notes = "获取首页轮播图列表", httpMethod = "GET")
    23. @GetMapping("/carousel")
    24. public IMOOCJSONResult carousel(){
    25. List<Carousel> list =carouselService.queryAll(YseOrNo.YES.type);
    26. return IMOOCJSONResult.ok(list);
    27. }
    28. }

    启动项目,访问 http://localhost:8080/foodie-shop/index.html 轮播图图片加载成功
    image.png