1. 引入分页插件依赖

      1. <dependency>
      2. <groupId>com.github.pagehelper</groupId>
      3. <artifactId>pagehelper-spring-boot-starter</artifactId>
      4. <version>1.3.0</version>
      5. </dependency>
    2. 配置 yml

      ############################################################
      #
      # web访问端口号 约定:8088
      #
      ############################################################
      server:
      port: 8088
      tomcat:
       uri-encoding: UTF-8
      max-http-header-size: 80KB
      ############################################################
      #
      # 配置数据源信息
      #
      ############################################################
      spring:
      datasource: # 数据源的相关配置
       type: com.zaxxer.hikari.HikariDataSource # 数据源类型:HikariCP
       driver-class-name: com.mysql.cj.jdbc.Driver # mysql驱动
       url: jdbc:mysql://localhost:3306/foodie-shop-dev?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
       username: root
       password: root
       hikari:
         connection-timeout: 30000 # 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQ
         minimum-idle: 5 # 最小连接数
         maximum-pool-size: 20 # 最大连接数
         auto-commit: true # 自动提交
         idle-timeout: 600000 # 连接超时的最大时长(毫秒),超时则被释放(retired),默认:10分钟
         pool-name: DateSourceHikariCP # 连接池名字
         max-lifetime: 1800000 # 连接的生命时长(毫秒),超时而且没被使用则被释放(retired),默认:30分钟
         connection-test-query: SELECT 1
      ############################################################
      #
      # mybatis 配置
      #
      ############################################################
      mybatis:
      type-aliases-package: com.imooc.pojo # 所有POJO类所在包路径
      mapper-locations: classpath:mapper/*.xml # mapper映射文件
      configuration:
       log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
      ############################################################
      #
      # mybatis mapper 配置
      #
      ############################################################
      # 通用 Mapper 配置
      mapper:
      mappers: com.imooc.my.mapper.MyMapper
      not-empty: false # 在进行数据库操作的时候,判断表达式 username != null,是否追加 username != ''
      identity: MYSQL # 数据库方言
      # 分页插件配置
      pagehelper:
      helperDialect: mysql
      supportMethodsArguments: true
      
    3. 使用分页插件,在查询前使用分页插件,原理:统一拦截 sql,为其提供分页功能

    在 foodie-dev-service 模块中修改 ItemService 接口的 queryPagedComments 方法

    package com.imooc.service;
    
    import com.imooc.pojo.Items;
    import com.imooc.pojo.ItemsImg;
    import com.imooc.pojo.ItemsParam;
    import com.imooc.pojo.ItemsSpec;
    import com.imooc.pojo.vo.CommentLevelCountsVO;
    import com.imooc.pojo.vo.ItemCommentVO;
    
    import java.util.List;
    
    /**
     * Created by 92578 on 2020/8/22 16:17
     **/
    public interface ItemService {
    
        /**
         * 根据商品 id 查询详情
         *
         * @param itemId
         * @return
         */
        public Items queryItemById(String itemId);
    
        /**
         * 根据商品 id 查询商品图片列表
         *
         * @param itemId
         * @return
         */
        public List<ItemsImg> queryItemImgList(String itemId);
    
        /**
         * 根据商品 id 查询商品规格
         *
         * @param itemId
         * @return
         */
        public List<ItemsSpec> queryItemSpecList(String itemId);
    
        /**
         * 根据商品 id 查询商品参数
         *
         * @param itemId
         * @return
         */
        public ItemsParam queryItemsParam(String itemId);
    
        /**
         * 根据商品 id 查询商品的评价等级数量
         *
         * @param itemId
         */
        public CommentLevelCountsVO queryCommentCounts(String itemId);
    
        /**
         * 根据商品 id 查询商品的评价(分页)
         *
         * @param itemId
         * @param level
         * @return
         */
        public List<ItemCommentVO> queryPagedComments(String itemId, Integer level, Integer page, Integer pageSize);
    }
    
    1. 分页数据封装到 PagedGridResult.java 传给前端

    在 foodie-dev-common 模块下的 com.imooc.utils 包内创建 PagedGridResult 类

    package com.imooc.utils;
    
    import java.util.List;
    
    /**
     * 
     * @Title: PagedGridResult.java
     * @Package com.imooc.utils
     * @Description: 用来返回分页Grid的数据格式
     * Copyright: Copyright (c) 2019
     */
    public class PagedGridResult {
    
        private int page;            // 当前页数
        private int total;            // 总页数    
        private long records;        // 总记录数
        private List<?> rows;        // 每行显示的内容
    
        public int getPage() {
            return page;
        }
        public void setPage(int page) {
            this.page = page;
        }
        public int getTotal() {
            return total;
        }
        public void setTotal(int total) {
            this.total = total;
        }
        public long getRecords() {
            return records;
        }
        public void setRecords(long records) {
            this.records = records;
        }
        public List<?> getRows() {
            return rows;
        }
        public void setRows(List<?> rows) {
            this.rows = rows;
        }
    }
    

    在 foodie-dev-service 模块中修改 ItemServiceImpl 类的 queryPagedComments 方法

    package com.imooc.service.impl;
    
    import com.github.pagehelper.PageHelper;
    import com.github.pagehelper.PageInfo;
    import com.imooc.enums.CommentLevel;
    import com.imooc.mapper.*;
    import com.imooc.pojo.*;
    import com.imooc.pojo.vo.CommentLevelCountsVO;
    import com.imooc.pojo.vo.ItemCommentVO;
    import com.imooc.service.ItemService;
    import com.imooc.utils.PagedGridResult;
    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 ItemServiceImpl implements ItemService {
    
        @Autowired
        private ItemsMapper itemsMapper;
    
        @Autowired
        private ItemsImgMapper itemsImgMapper;
    
        @Autowired
        private ItemsSpecMapper itemsSpecMapper;
    
        @Autowired
        private ItemsParamMapper itemsParamMapper;
    
        @Autowired
        private ItemsCommentsMapper itemsCommentsMapper;
    
        @Autowired
        private ItemsMapperCustom itemsMapperCustom;
    
        /**
         * 根据商品 id 查询详情
         *
         * @param itemId
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public Items queryItemById(String itemId) {
            return itemsMapper.selectByPrimaryKey(itemId);
        }
    
        /**
         * 根据商品 id 查询商品图片列表
         *
         * @param itemId
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public List<ItemsImg> queryItemImgList(String itemId) {
            Example itemsImgExp = new Example(ItemsImg.class);
            Example.Criteria criteria = itemsImgExp.createCriteria();
            criteria.andEqualTo("itemId", itemId);
    
            return itemsImgMapper.selectByExample(itemsImgExp);
        }
    
        /**
         * 根据商品 id 查询商品规格
         *
         * @param itemId
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public List<ItemsSpec> queryItemSpecList(String itemId) {
            Example itemsSpecExp = new Example(ItemsSpec.class);
            Example.Criteria criteria = itemsSpecExp.createCriteria();
            criteria.andEqualTo("itemId", itemId);
    
            return itemsSpecMapper.selectByExample(itemsSpecExp);
        }
    
        /**
         * 根据商品 id 查询商品参数
         *
         * @param itemId
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public ItemsParam queryItemsParam(String itemId) {
            Example itemsParamExp = new Example(ItemsParam.class);
            Example.Criteria criteria = itemsParamExp.createCriteria();
            criteria.andEqualTo("itemId", itemId);
    
            return itemsParamMapper.selectOneByExample(itemsParamExp);
        }
    
        /**
         * 根据商品 id 查询商品的评价等级数量
         *
         * @param itemId
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public CommentLevelCountsVO queryCommentCounts(String itemId) {
            Integer goodCounts = getCommentCounts(itemId, CommentLevel.GOOD.type);
            Integer normalCounts = getCommentCounts(itemId, CommentLevel.NORMAL.type);
            Integer badCounts = getCommentCounts(itemId, CommentLevel.BAD.type);
            Integer totalCounts = goodCounts + normalCounts + badCounts;
    
            CommentLevelCountsVO countsVO = new CommentLevelCountsVO();
            countsVO.setTotalCounts(totalCounts);
            countsVO.setGoodCounts(goodCounts);
            countsVO.setNormalCounts(normalCounts);
            countsVO.setBadCounts(badCounts);
    
            return countsVO;
        }
    
        /**
         * 根据商品 id 查询商品的评价(分页)
         *
         * @param itemId
         * @param level
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public PagedGridResult queryPagedComments(String itemId, Integer level, Integer page, Integer pageSize) {
            Map<String, Object> map = new HashMap<>();
            map.put("itemId", itemId);
            map.put("level", level);
    
            /**
             * page: 第几页
             * pageSize: 每页显示条数
             */
            PageHelper.startPage(page, pageSize);
    
            List<ItemCommentVO> list = itemsMapperCustom.queryItemComments(map);
    
            return setterPagedGrid(list, page);
        }
    
        private PagedGridResult setterPagedGrid(List<?> list, Integer page) {
            PageInfo<?> pageList = new PageInfo<>(list);
            PagedGridResult grid = new PagedGridResult();
            grid.setPage(page);
            grid.setRows(list);
            grid.setTotal(pageList.getPages());
            grid.setRecords(pageList.getTotal());
            return grid;
        }
    
        @Transactional(propagation = Propagation.SUPPORTS)
        Integer getCommentCounts(String itemId, Integer level) {
            ItemsComments condition = new ItemsComments();
            condition.setItemId(itemId);
            if (level != null) {
                condition.setCommentLevel(level);
            }
    
            return itemsCommentsMapper.selectCount(condition);
        }
    }
    

    在 foodie-dev-service 模块中修改 ItemService 接口的 queryPagedComments 方法

    package com.imooc.service;
    
    import com.imooc.pojo.Items;
    import com.imooc.pojo.ItemsImg;
    import com.imooc.pojo.ItemsParam;
    import com.imooc.pojo.ItemsSpec;
    import com.imooc.pojo.vo.CommentLevelCountsVO;
    import com.imooc.utils.PagedGridResult;
    
    import java.util.List;
    
    /**
     * Created by 92578 on 2020/8/22 16:17
     **/
    public interface ItemService {
    
        /**
         * 根据商品 id 查询详情
         *
         * @param itemId
         * @return
         */
        public Items queryItemById(String itemId);
    
        /**
         * 根据商品 id 查询商品图片列表
         *
         * @param itemId
         * @return
         */
        public List<ItemsImg> queryItemImgList(String itemId);
    
        /**
         * 根据商品 id 查询商品规格
         *
         * @param itemId
         * @return
         */
        public List<ItemsSpec> queryItemSpecList(String itemId);
    
        /**
         * 根据商品 id 查询商品参数
         *
         * @param itemId
         * @return
         */
        public ItemsParam queryItemsParam(String itemId);
    
        /**
         * 根据商品 id 查询商品的评价等级数量
         *
         * @param itemId
         */
        public CommentLevelCountsVO queryCommentCounts(String itemId);
    
        /**
         * 根据商品 id 查询商品的评价(分页)
         *
         * @param itemId
         * @param level
         * @return
         */
        public PagedGridResult queryPagedComments(String itemId, Integer level, Integer page, Integer pageSize);
    }
    

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

    package com.imooc.controller;
    
    import org.springframework.stereotype.Controller;
    
    /**
     * @author 92578
     * @since 1.0
     */
    @Controller
    public class BaseController {
        public static final Integer COMMENT_PAGE_SIZE = 10;
    }
    

    在 foodie-dev-api 模块下的 ItemsController 类中添加 comments 方法

    package com.imooc.controller;
    
    import com.imooc.pojo.Items;
    import com.imooc.pojo.ItemsImg;
    import com.imooc.pojo.ItemsParam;
    import com.imooc.pojo.ItemsSpec;
    import com.imooc.pojo.vo.CommentLevelCountsVO;
    import com.imooc.pojo.vo.ItemInfoVO;
    import com.imooc.service.ItemService;
    import com.imooc.utils.IMOOCJSONResult;
    import com.imooc.utils.PagedGridResult;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import io.swagger.annotations.ApiParam;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    /**
     * Created by 92578 on 2020/8/22 16:30
     **/
    @Api(value = "商品接口", tags = {"商品信息展示的相关接口"})
    @RestController
    @RequestMapping("items")
    public class ItemsController extends BaseController {
    
        @Autowired
        private ItemService itemService;
    
        @ApiOperation(value = "查询商品详情", notes = "查询商品详情", httpMethod = "GET")
        @GetMapping("/info/{itemId}")
        public IMOOCJSONResult info(
                @ApiParam(name = "itemId", value = "商品 id", required = true)
                @PathVariable String itemId) {
            if (StringUtils.isBlank(itemId)) {
                return IMOOCJSONResult.errorMsg(null);
            }
    
            Items item = itemService.queryItemById(itemId);
            List<ItemsImg> itemsImgList = itemService.queryItemImgList(itemId);
            List<ItemsSpec> itemsSpecList = itemService.queryItemSpecList(itemId);
            ItemsParam itemsParam = itemService.queryItemsParam(itemId);
    
            ItemInfoVO itemInfoVO = new ItemInfoVO();
            itemInfoVO.setItem(item);
            itemInfoVO.setItemImgList(itemsImgList);
            itemInfoVO.setItemSpecList(itemsSpecList);
            itemInfoVO.setItemParams(itemsParam);
    
            return IMOOCJSONResult.ok(itemInfoVO);
        }
    
        @ApiOperation(value = "查询商品评价等级", notes = "查询商品评价等级", httpMethod = "GET")
        @GetMapping("/commentLevel")
        public IMOOCJSONResult commentLevel(
                @ApiParam(name = "itemId", value = "商品 id", required = true)
                @RequestParam String itemId) {
            if (StringUtils.isBlank(itemId)) {
                return IMOOCJSONResult.errorMsg(null);
            }
    
            CommentLevelCountsVO countsVO = itemService.queryCommentCounts(itemId);
    
            return IMOOCJSONResult.ok(countsVO);
        }
    
        @ApiOperation(value = "查询商品评论", notes = "查询商品评论", httpMethod = "GET")
        @GetMapping("/comments")
        public IMOOCJSONResult comments(
                @ApiParam(name = "itemId", value = "商品 id", required = true)
                @RequestParam String itemId,
                @ApiParam(name = "level", value = "评价等级", required = false)
                @RequestParam Integer level,
                @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
                @RequestParam Integer page,
                @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
                @RequestParam Integer pageSize) {
            if (StringUtils.isBlank(itemId)) {
                return IMOOCJSONResult.errorMsg(null);
            }
    
            if (page == null) {
                page = 1;
            }
    
            if (pageSize == null) {
                pageSize = COMMENT_PAGE_SIZE;
            }
    
            PagedGridResult grid = itemService.queryPagedComments(itemId, level, page, pageSize);
    
            return IMOOCJSONResult.ok(grid);
        }
    }
    

    启动项目,打开浏览器,访问 http://localhost:8080/foodie-shop/item.html?itemId=cake-1001
    点击“全部评价”,评价内容加载成功
    image.png
    点击“第二页”,内容也是可以加载成功的
    image.png
    点击“好评”,会只显示好评数据,中评和差评同理。
    image.png
    好评记录
    image.png
    中评记录
    image.png
    差评记录