商品评论分页查询

商品评论分页查询 - SpringBoot 整合 mybatis-pagehelper

一、SpringBoot 整合 mybatis-pagehelper

1.引入分页插件依赖

<!--pagehelper -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.12</version>
</dependency>

2.配置 yml

 api目录路径:src/main/resources/application.yml

分页插件配置
pagehelper:
helperDialect: mysql
supportMethodsArguments: true

3.使用分页插件
/**

  • page: 第几页
  • pageSize: 每页显示条数
    */
    PageHelper.startPage(page, pageSize);

4.分页数据封装到PagedGridResult.java传给前端

PageInfo<?> pageList = new PageInfo<>(list);
PagedGridResult grid = new PagedGridResult();
grid.setPage(page);
grid.setRows(list);
grid.setTotal(pageList.getPages());
grid.setRecords(pageList.getTotal());

二、查询商品评论-分页

1、数据表

商品评价表 items_cooments

create table `foodie-shop-dev`.items_comments
(
    id            varchar(64)  not null comment 'id主键'
        primary key,
    user_id       varchar(64)  null comment '用户id 用户名须脱敏',
    item_id       varchar(64)  not null comment '商品id',
    item_name     varchar(64)  null comment '商品名称',
    item_spec_id  varchar(64)  null comment '商品规格id 可为空',
    sepc_name     varchar(32)  null comment '规格名称 可为空',
    comment_level int          not null comment '评价等级 1:好评 2:中评 3:差评',
    content       varchar(128) not null comment '评价内容',
    created_time  datetime     null comment '创建时间',
    updated_time  datetime     null comment '更新时间'
)
    comment '商品评价表 ' charset = utf8mb4;

商品评价表 items_comments

用户表 users

create table `foodie-shop-dev`.users
(
    id           varchar(64)   not null comment '主键id 用户id'
        primary key,
    username     varchar(32)   not null comment '用户名 用户名',
    password     varchar(64)   not null comment '密码 密码',
    nickname     varchar(32)   null comment '昵称 昵称',
    realname     varchar(128)  null comment '真实姓名',
    face         varchar(1024) not null comment '头像',
    mobile       varchar(32)   null comment '手机号 手机号',
    email        varchar(32)   null comment '邮箱地址 邮箱地址',
    sex          int           null comment '性别 性别 1:男  0:女  2:保密',
    birthday     date          null comment '生日 生日',
    created_time datetime      not null comment '创建时间 创建时间',
    updated_time datetime      not null comment '更新时间 更新时间'
)
    comment '用户表 ' charset = utf8mb4;

用户表 users

2、自定义sql查询语句

SELECT
    ic.comment_level AS commentLevel,
    ic.content AS content,
    ic.sepc_name AS specName,
    ic.created_time AS createdTime,
    u.face AS userFace,
    u.nickname AS nickName 
FROM
    items_comments ic
    LEFT JOIN users u ON ic.user_id = u.id 
WHERE
    ic.item_id = 'cake-1001' 
    AND ic.comment_level = 1

3、Mapper子模块实现

   (1)xml文件定义

         路径:\src\main\resources\mapper\ItemsMapperCustom.xml

在这里插入图片描述
(2)VO定义

        路径: com/imooc/pojo/vo/ItemCommentVO.java             

在这里插入图片描述
(3)接口定义

      路径:com/imooc/mapper/ItemsMapperCustom.java

在这里插入图片描述
4、service子模块实现
(1)分页通用 PagedGridResult.java 引入

     路径:  com/imooc/utils/PagedGridResult.java
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;
    }
}
   (2)接口定义

        路径:com/imooc/service/ItemService.java

       方法:queryPagedComments

在这里插入图片描述

(3)接口实现
com/imooc/service/impl/ItemServiceImpl.java
在这里插入图片描述
5、api子模块实现

    评论分页实现

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值