在 foodie-dev-service 模块下的 CategoryService 接口添加 getSixNewItemsLazy 方法

    1. package com.imooc.service;
    2. import com.imooc.pojo.Category;
    3. import com.imooc.pojo.vo.CategoryVO;
    4. import com.imooc.pojo.vo.NewItemsVO;
    5. import java.util.List;
    6. /**
    7. * Created by 92578 on 2020/8/22 16:17
    8. **/
    9. public interface CategoryService {
    10. /**
    11. * 查询所有一级分类
    12. *
    13. * @return
    14. */
    15. public List<Category> queryAllRootLevelCat();
    16. /**
    17. * 根据一级分类 id 查询子分类信息
    18. *
    19. * @param rootCatId
    20. * @return
    21. */
    22. public List<CategoryVO> getSubCatlist(Integer rootCatId);
    23. /**
    24. * 查询首页每个一级分类下的六条最新商品数据
    25. *
    26. * @param rootCatId
    27. * @return
    28. */
    29. public List<NewItemsVO> getSixNewItemsLazy(Integer rootCatId);
    30. }

    在 foodie-dev-mapper 模块下的 CategoryMapperCustom 接口添加 getSixNewItemsLazy 方法

    package com.imooc.mapper;
    
    import com.imooc.pojo.vo.CategoryVO;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    import java.util.Map;
    
    public interface CategoryMapperCustom {
    
        public List<CategoryVO> getSubCatList(Integer rootCatId);
    
        public List getSixNewItemsLazy(@Param("paramsMap") Map<String, Object> map);
    }
    

    在 foodie-dev-pojo 模块下面 com.imooc.pojo.vo 包内新建 SimpleItemVO 类

    package com.imooc.pojo.vo;
    
    /**
     * 六个最新商品的简单数据类型
     * Created by 92578 on 2020/8/22 21:28
     **/
    public class SimpleItemVO {
    
        private String itemId;
        private String itemName;
        private String itemUrl;
    
        public String getItemId() {
            return itemId;
        }
    
        public void setItemId(String itemId) {
            this.itemId = itemId;
        }
    
        public String getItemName() {
            return itemName;
        }
    
        public void setItemName(String itemName) {
            this.itemName = itemName;
        }
    
        public String getItemUrl() {
            return itemUrl;
        }
    
        public void setItemUrl(String itemUrl) {
            this.itemUrl = itemUrl;
        }
    }
    

    在 foodie-dev-pojo 模块下面 com.imooc.pojo.vo 包内新建 NewItemsVO 类

    package com.imooc.pojo.vo;
    
    import java.util.List;
    
    /**
     * 最新商品 VO
     * Created by 92578 on 2020/8/22 19:57
     **/
    public class NewItemsVO {
    
        private Integer rootCatId;
        private String rootCatName;
        private String slogan;
        private String catImage;
        private String bgColor;
    
        private List<SimpleItemVO> simpleItemList;
    
        public Integer getRootCatId() {
            return rootCatId;
        }
    
        public void setRootCatId(Integer rootCatId) {
            this.rootCatId = rootCatId;
        }
    
        public String getRootCatName() {
            return rootCatName;
        }
    
        public void setRootCatName(String rootCatName) {
            this.rootCatName = rootCatName;
        }
    
        public String getSlogan() {
            return slogan;
        }
    
        public void setSlogan(String slogan) {
            this.slogan = slogan;
        }
    
        public String getCatImage() {
            return catImage;
        }
    
        public void setCatImage(String catImage) {
            this.catImage = catImage;
        }
    
        public String getBgColor() {
            return bgColor;
        }
    
        public void setBgColor(String bgColor) {
            this.bgColor = bgColor;
        }
    
        public List<SimpleItemVO> getSimpleItemList() {
            return simpleItemList;
        }
    
        public void setSimpleItemList(List<SimpleItemVO> simpleItemList) {
            this.simpleItemList = simpleItemList;
        }
    }
    

    在 foodie-dev-mapper 模块下的 CategoryMapperCustom.xml 文件添加 getSixNewItemsLazy 查询方法

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.imooc.mapper.CategoryMapperCustom">
    
        <resultMap id="myCategoryVO" type="com.imooc.pojo.vo.CategoryVO">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="type" property="type"/>
            <result column="fatherId" property="fatherId"/>
            <!--
                collection 标签:用于定义关联的 list 集合类型的封装规则
                property:对应三级分类的 list 属性名
                ofType:集合的类型,三级分类的 vo
            -->
            <collection property="subCatList" ofType="com.imooc.pojo.vo.SubCategoryVO">
                <id column="subId" property="subId"/>
                <result column="subName" property="subName"/>
                <result column="subType" property="subType"/>
                <result column="subFatherId" property="subFatherId"/>
            </collection>
        </resultMap>
    
        <select id="getSubCatList" resultMap="myCategoryVO" parameterType="int">
            SELECT
                f.id AS id,
                f.`name` AS `name`,
                f.type AS type,
                f.father_id AS fatherId,
                c.id AS subId,
                c.`name` AS subName,
                c.type AS subType,
                c.father_id AS subFatherId
            FROM
                category f
                LEFT JOIN category c ON f.id = c.father_id
            WHERE
                f.father_id = #{rootCatId}
        </select>
    
        <resultMap id="myNewItemsVO" type="com.imooc.pojo.vo.NewItemsVO">
            <id column="rootCatId" property="rootCatId"/>
            <result column="rootCatName" property="rootCatName"/>
            <result column="slogan" property="slogan"/>
            <result column="catImage" property="catImage"/>
            <result column="bgColor" property="bgColor"/>
    
            <collection property="simpleItemList" ofType="com.imooc.pojo.vo.SimpleItemVO">
                <id column="itemId" property="itemId"/>
                <result column="itemName" property="itemName"/>
                <result column="itemUrl" property="itemUrl"/>
            </collection>
        </resultMap>
    
        <select id="getSixNewItemsLazy" resultMap="myNewItemsVO" parameterType="Map">
            SELECT
                f.id AS rootCatId,
                f.`name` AS rootCatName,
                f.slogan AS slogan,
                f.cat_image AS catImage,
                f.bg_color AS bgColor,
                i.id AS itemId,
                i.item_name AS itemName,
                ii.url AS itemUrl,
                i.created_time AS createdTime
            FROM
                category f
                LEFT JOIN items i ON f.id = i.root_cat_id
                LEFT JOIN items_img ii ON i.id = ii.item_id
            WHERE
                f.type = 1
                AND i.root_cat_id = #{paramsMap.rootCatId}
                AND ii.is_main = 1
            ORDER BY
                i.created_time DESC
                LIMIT 0,6
        </select>
    
    </mapper>
    

    在 foodie-dev-service 模块下的 CategoryServiceImpl 类中实现 getSixNewItemsLazy 接口

    package com.imooc.service.impl;
    
    import com.imooc.mapper.CategoryMapper;
    import com.imooc.mapper.CategoryMapperCustom;
    import com.imooc.pojo.Category;
    import com.imooc.pojo.vo.CategoryVO;
    import com.imooc.pojo.vo.NewItemsVO;
    import com.imooc.service.CategoryService;
    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.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * Created by 92578 on 2020/8/22 16:18
     **/
    @Service
    public class CategoryServiceImpl implements CategoryService {
    
        @Autowired
        private CategoryMapper categoryMapper;
    
        @Autowired
        private CategoryMapperCustom categoryMapperCustom;
    
        /**
         * 查询所有一级分类
         *
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public 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;
        }
    
        /**
         * 根据一级分类 id 查询子分类信息
         *
         * @param rootCatId
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public List<CategoryVO> getSubCatlist(Integer rootCatId) {
            return categoryMapperCustom.getSubCatList(rootCatId);
        }
    
        /**
         * 查询首页每个一级分类下的六条最新商品数据
         *
         * @param rootCatId
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public List<NewItemsVO> getSixNewItemsLazy(Integer rootCatId) {
            Map<String, Object> map = new HashMap<>();
            map.put("rootCatId", rootCatId);
    
            return categoryMapperCustom.getSixNewItemsLazy(map);
        }
    }
    

    在 foodie-dev-api 模块下的 IndexController 类添加 sixNewItems 方法

    package com.imooc.controller;
    
    import com.imooc.enums.YseOrNo;
    import com.imooc.pojo.Carousel;
    import com.imooc.pojo.Category;
    import com.imooc.pojo.vo.CategoryVO;
    import com.imooc.pojo.vo.NewItemsVO;
    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 io.swagger.annotations.ApiParam;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    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);
        }
    
        @ApiOperation(value = "获取商品子分类", notes = "获取商品子分类", httpMethod = "GET")
        @GetMapping("/subCat/{rootCatId}")
        public IMOOCJSONResult subCat(
                @ApiParam(name = "rootCatId", value = "一级分类 id", required = true)
                @PathVariable Integer rootCatId) {
            if (rootCatId == null) {
                return IMOOCJSONResult.errorMsg("分类不存在");
            }
    
            List<CategoryVO> list = categoryService.getSubCatlist(rootCatId);
    
            return IMOOCJSONResult.ok(list);
        }
    
        @ApiOperation(value = "查询每个一级分类下的最新六条商品数据", notes = "查询每个一级分类下的最新六条商品数据", httpMethod = "GET")
        @GetMapping("/sixNewItems/{rootCatId}")
        public IMOOCJSONResult sixNewItems(
                @ApiParam(name = "rootCatId", value = "一级分类 id", required = true)
                @PathVariable Integer rootCatId) {
            if (rootCatId == null) {
                return IMOOCJSONResult.errorMsg("分类不存在");
            }
    
            List<NewItemsVO> list = categoryService.getSixNewItemsLazy(rootCatId);
    
            return IMOOCJSONResult.ok(list);
        }
    }
    

    启动项目,打开浏览器,访问 http://localhost:8080/foodie-shop/index.html
    向下滚动页面,发现图片已经懒加载出来了
    image.png