在 foodie-dev-service 模块下的 com.imooc.service 包内创建 CarouselService 接口
package com.imooc.service;import com.imooc.pojo.Carousel;import java.util.List;/*** Created by 92578 on 2020/8/22 16:17**/public interface CarouselService {/*** 查询所有轮播图列表* @param isShow* @return*/public List<Carousel> queryAll(Integer isShow);}
在 foodie-dev-service 模块下的 com.imooc.service.impl 包内创建 CarouselServiceImpl 类
package com.imooc.service.impl;import com.imooc.mapper.CarouselMapper;import com.imooc.pojo.Carousel;import com.imooc.service.CarouselService;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 CarouselServiceImpl implements CarouselService {@Autowiredprivate CarouselMapper carouselMapper;/*** 查询所有轮播图列表** @param isShow* @return*/@Transactional(propagation = Propagation.SUPPORTS)@Overridepublic List<Carousel> queryAll(Integer isShow) {Example example = new Example(Carousel.class);example.orderBy("sort").desc();Example.Criteria criteria = example.createCriteria();criteria.andEqualTo("isShow", isShow);List<Carousel> result = carouselMapper.selectByExample(example);return result;}}
在 foodie-dev-common 模块下的 com.imooc.enums 包内创建 YseOrNo 枚举
package com.imooc.enums;/*** 是否枚举* Created by 92578 on 2020/8/16 20:38**/public enum YseOrNo {NO(0, "否"),YES(1, "是");public final Integer type;public final String value;YseOrNo(Integer type, String value) {this.type = type;this.value = value;}}
在 foodie-dev-api 模块下的 com.imooc.controller 包内创建 IndexController 类
package com.imooc.controller;import com.imooc.enums.YseOrNo;import com.imooc.pojo.Carousel;import com.imooc.service.CarouselService;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 {@Autowiredprivate CarouselService carouselService;@ApiOperation(value = "获取首页轮播图列表", notes = "获取首页轮播图列表", httpMethod = "GET")@GetMapping("/carousel")public IMOOCJSONResult carousel(){List<Carousel> list =carouselService.queryAll(YseOrNo.YES.type);return IMOOCJSONResult.ok(list);}}
启动项目,访问 http://localhost:8080/foodie-shop/index.html 轮播图图片加载成功
