2.01 实现电商首页轮播图后台功能

步骤1:创建轮播图数据库表

CREATE TABLE `carousel` (
  `id` varchar(64) NOT NULL COMMENT '主键',
  `image_url` varchar(128) NOT NULL COMMENT '图片 图片地址',
  `background_color` varchar(32) DEFAULT NULL COMMENT '背景色',
  `item_id` varchar(64) DEFAULT NULL COMMENT '商品id 商品id',
  `cat_id` varchar(64) DEFAULT NULL COMMENT '商品分类id 商品分类id',
  `type` int(11) NOT NULL COMMENT '轮播图类型 轮播图类型,用于判断,可以根据商品id或者分类进行页面跳转,1:商品 2:分类',
  `sort` int(11) NOT NULL COMMENT '轮播图展示顺序',
  `is_show` int(11) NOT NULL COMMENT '是否展示',
  `create_time` datetime NOT NULL COMMENT '创建时间 创建时间',
  `update_time` datetime NOT NULL COMMENT '更新时间 更新',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='轮播图 ';

步骤2:使用MyBatis数据库逆向生成工具生成pojo和mapper类

CarouselMapper和Carousel

步骤3:创建接口和实现类

import com.one.pojo.Carousel;
import java.util.List;
public interface CarouselService {
    /**
     * 查询所有轮播图列表
     * @param isShow
     * @return
     */
    public List<Carousel> queryAll(Integer isShow);

}

import com.one.mapper.CarouselMapper;
import com.one.pojo.Carousel;
import com.one.service.carousel.CarouselService;
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;
@Service
public class CarouselServiceImpl implements CarouselService {
    @Autowired
    private CarouselMapper carouselMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public 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;
    }
}

步骤4:创建对应的api接口的枚举数据

/**
 * @Desc: 是否 枚举
 */
public enum YesOrNo {
    NO(0, "否"),
    YES(1, "是");

    public final Integer type;
    public final String value;

    YesOrNo(Integer type, String value) {
        this.type = type;
        this.value = value;
    }
}

步骤5:创建api接口访问类和对应的请求方法

import com.one.enums.YesOrNo;
import com.one.pojo.Carousel;
import com.one.service.carousel.CarouselService;
import com.one.utils.JSONResult;
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;
@Api(value = "首页", tags = {"首页展示的相关接口"})
@RestController
@RequestMapping("index")
public class IndexController {
    @Autowired
    private CarouselService carouselService;

    @ApiOperation(value = "获取首页轮播图列表", notes = "获取首页轮播图列表", httpMethod = "GET")
    @GetMapping("/carousel")
    public JSONResult carousel() {
        List<Carousel> list = carouselService.queryAll(YesOrNo.YES.type);
        return JSONResult.ok(list);
    }
}

你可能感兴趣的:(java架构笔记,轮播图后台功能,springboot)