提交 3e38eef8 作者: 张开石

1、添加分页插件pageHelper

2、添加全局控制器增强器,处理日期类请求参数
3、添加全局配置类,对时区进行配置
父级 f6610368
......@@ -265,6 +265,11 @@
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>io.minio</groupId>
......
package com.cmeeting.config;
import cn.hutool.core.date.DateUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.beans.PropertyEditorSupport;
import java.util.Date;
/**
* @Description 全局控制器增强器
* @Author zhang kaishi
* @Date 2025/7/17 17:29
**/
@RestControllerAdvice
public class GlobalControllerAdvice {
@InitBinder
public void initBinder(WebDataBinder binder)
{
// Date 类型转换
binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
{
@Override
public void setAsText(String text)
{
setValue(ObjectUtils.isEmpty(text) ? null : DateUtil.parse(text));
}
});
}
}
package com.cmeeting.util;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* @Description
* @Author zhang kaishi
* @Date 2025/7/17 17:16
**/
public class ServletUtil {
/**
* 获取request
*/
public static HttpServletRequest getRequest() {
return getRequestAttributes().getRequest();
}
public static ServletRequestAttributes getRequestAttributes() {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return (ServletRequestAttributes) attributes;
}
}
package com.cmeeting.util.page;
import com.cmeeting.util.R;
import com.cmeeting.util.page.domain.PageDomain;
import com.cmeeting.util.page.domain.TableData;
import com.cmeeting.util.page.domain.TableSupport;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.util.ObjectUtils;
import java.util.List;
public class PageUtil {
/**
* 设置请求分页数据
*/
public static void startPage() {
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize();
if (!ObjectUtils.isEmpty(pageNum) && !ObjectUtils.isEmpty(pageSize)) {
PageHelper.startPage(pageNum, pageSize);
}
}
/**
* 设置请求分页数据
* @param pageNum 页号
* @param pageSize 页大小
*/
public static void startPage(Integer pageNum, Integer pageSize) {
pageNum = ObjectUtils.isEmpty(pageNum) ? TableSupport.DEFAULT_PAGE_NUM : pageNum;
pageSize = ObjectUtils.isEmpty(pageSize) ? TableSupport.DEFAULT_PAGE_SIZE : pageSize;
if (!ObjectUtils.isEmpty(pageNum) && !ObjectUtils.isEmpty(pageSize)) {
PageHelper.startPage(pageNum, pageSize);
}
}
/**
* 分页
* @param pageSize 分页参数
* @param pageNum 分页参数
* @param orderby 排序规则
*/
public static void startPage(Integer pageSize, Integer pageNum, String orderby) {
if (!ObjectUtils.isEmpty(pageNum) && !ObjectUtils.isEmpty(pageSize)) {
if (ObjectUtils.isEmpty(orderby)) {
PageHelper.startPage(pageNum, pageSize);
} else {
PageHelper.startPage(pageNum, pageSize, orderby);
}
} else {
startPage(orderby);
}
}
/**
* 设置请求分页数据
*/
public static void startPage(String orderby) {
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize();
if (!ObjectUtils.isEmpty(pageNum) && !ObjectUtils.isEmpty(pageSize)) {
if (ObjectUtils.isEmpty(orderby)) {
PageHelper.startPage(pageNum, pageSize);
} else {
PageHelper.startPage(pageNum, pageSize, orderby);
}
}
}
/**
* 排序
* @param orderBy
*/
public static void orderBy(String orderBy) {
PageHelper.orderBy(orderBy);
}
/**
* 响应请求分页数据
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public static R getDataTable(List<?> list) {
PageInfo pageInfo = new PageInfo(list);
TableData tableData = new TableData();
tableData.setRows(list);
tableData.setTotal(pageInfo.getTotal());
tableData.setTotalPage(pageInfo.getPages());
return R.ok(tableData);
}
public static R getDataTable(PageInfo<?> pageInfo) {
TableData tableData = new TableData();
tableData.setRows(pageInfo.getList());
tableData.setTotal(pageInfo.getTotal());
tableData.setTotalPage(pageInfo.getPages());
return R.ok(tableData);
}
}
package com.cmeeting.util.page.domain;
import cn.hutool.core.util.StrUtil;
public class PageDomain {
/**
* 当前记录起始索引
*/
private Integer pageNum;
/**
* 每页显示记录数
*/
private Integer pageSize;
/**
* 排序列
*/
private String orderByColumn;
/**
* 排序的方向desc或者asc
*/
private String isAsc = "asc";
public String getOrderBy() {
if (StrUtil.isEmpty(orderByColumn)) {
return "";
}
return StrUtil.toUnderlineCase(orderByColumn) + " " + isAsc;
}
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public String getOrderByColumn() {
return orderByColumn;
}
public void setOrderByColumn(String orderByColumn) {
this.orderByColumn = orderByColumn;
}
public String getIsAsc() {
return isAsc;
}
public void setIsAsc(String isAsc) {
this.isAsc = isAsc;
}
}
package com.cmeeting.util.page.domain;
import lombok.Data;
import java.util.List;
@Data
public class TableData<T> {
private Long total;
private Integer totalPage;
private List<T> rows;
}
package com.cmeeting.util.page.domain;
import cn.hutool.core.util.StrUtil;
import com.cmeeting.util.ServletUtil;
/**
* @Description
* @Author zhang kaishi
* @Date 2025/7/17 17:15
**/
public class TableSupport {
/**
* 当前记录起始索引
*/
public static final String PAGE_NUM = "pageNum";
/**
* 默认分页号
*/
public static final int DEFAULT_PAGE_NUM = 1;
/**
* 每页显示记录数
*/
public static final String PAGE_SIZE = "pageSize";
/**
* 默认每页显示记录数
*/
public static final int DEFAULT_PAGE_SIZE = 10;
/**
* 排序列
*/
public static final String ORDER_BY_COLUMN = "orderByColumn";
/**
* 排序的方向 "desc" 或者 "asc".
*/
public static final String IS_ASC = "isAsc";
/**
* 封装分页对象
*/
public static PageDomain getPageDomain() {
PageDomain pageDomain = new PageDomain();
String pageNum = ServletUtil.getRequest().getParameter(PAGE_NUM);
String pageSize = ServletUtil.getRequest().getParameter(PAGE_SIZE);
pageDomain.setPageNum(StrUtil.isNotBlank(pageNum) ? Integer.parseInt(pageNum) : DEFAULT_PAGE_NUM);
pageDomain.setPageSize(StrUtil.isNotBlank(pageSize) ? Integer.parseInt(pageSize) : DEFAULT_PAGE_SIZE);
return pageDomain;
}
public static PageDomain buildPageRequest() {
return getPageDomain();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论