商品评论分页查询 - 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子模块实现
评论分页实现