重构分页查询代码

This commit is contained in:
dataprince 2023-09-29 08:08:54 +08:00
parent d413baeb98
commit 891ef9bb09
6 changed files with 35 additions and 72 deletions

View File

@ -1,6 +1,9 @@
package com.ruoyi.common.orm.core.page;
import com.mybatisflex.core.paginate.Page;
import cn.hutool.http.HttpStatus;
import com.ruoyi.common.core.core.text.Convert;
import com.ruoyi.common.core.utils.ServletUtils;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -20,6 +23,16 @@ public class PageQuery implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* 当前记录起始索引
*/
public static final String PAGE_NUM = "pageNum";
/**
* 每页显示记录数
*/
public static final String PAGE_SIZE = "pageSize";
/**
* 分页大小
*/
@ -50,59 +63,19 @@ public class PageQuery implements Serializable {
*/
public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;
// public <T> Page<T> build() {
// Integer pageNum = ObjectUtil.defaultIfNull(getPageNum(), DEFAULT_PAGE_NUM);
// Integer pageSize = ObjectUtil.defaultIfNull(getPageSize(), DEFAULT_PAGE_SIZE);
// if (pageNum <= 0) {
// pageNum = DEFAULT_PAGE_NUM;
// }
// Page<T> page = new Page<>(pageNum, pageSize);
// List<OrderItem> orderItems = buildOrderItem();
// if (CollUtil.isNotEmpty(orderItems)) {
// page.addOrder(orderItems);
// }
// return page;
// }
/**
* 构建排序
*
* 支持的用法如下:
* {isAsc:"asc",orderByColumn:"id"} order by id asc
* {isAsc:"asc",orderByColumn:"id,createTime"} order by id asc,create_time asc
* {isAsc:"desc",orderByColumn:"id,createTime"} order by id desc,create_time desc
* {isAsc:"asc,desc",orderByColumn:"id,createTime"} order by id asc,create_time desc
* 构造分页查询参数
* @return
* @param <T>
*/
// private List<OrderItem> buildOrderItem() {
// if (StringUtils.isBlank(orderByColumn) || StringUtils.isBlank(isAsc)) {
// return null;
// }
// String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
// orderBy = StringUtils.toUnderScoreCase(orderBy);
//
// // 兼容前端排序类型
// isAsc = StringUtils.replaceEach(isAsc, new String[]{"ascending", "descending"}, new String[]{"asc", "desc"});
//
// String[] orderByArr = orderBy.split(StringUtils.SEPARATOR);
// String[] isAscArr = isAsc.split(StringUtils.SEPARATOR);
// if (isAscArr.length != 1 && isAscArr.length != orderByArr.length) {
// throw new ServiceException("排序参数有误");
// }
//
// List<OrderItem> list = new ArrayList<>();
// // 每个字段各自排序
// for (int i = 0; i < orderByArr.length; i++) {
// String orderByStr = orderByArr[i];
// String isAscStr = isAscArr.length == 1 ? isAscArr[0] : isAscArr[i];
// if ("asc".equals(isAscStr)) {
// list.add(OrderItem.asc(orderByStr));
// } else if ("desc".equals(isAscStr)) {
// list.add(OrderItem.desc(orderByStr));
// } else {
// throw new ServiceException("排序参数有误");
// }
// }
// return list;
// }
public static <T> Page<T> build() {
Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), DEFAULT_PAGE_NUM);
Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), DEFAULT_PAGE_SIZE);
if (pageNum <= 0) {
pageNum = DEFAULT_PAGE_NUM;
}
Page<T> page = new Page<>(pageNum, pageSize);
return page;
}
}

View File

@ -10,6 +10,7 @@ import com.ruoyi.common.core.core.page.TableSupport;
import com.ruoyi.common.core.service.ConfigService;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.common.redis.utils.CacheUtils;
@ -139,10 +140,7 @@ public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysCo
@Override
public TableDataInfo<SysConfigVo> selectConfigPage(SysConfigBo config) {
QueryWrapper queryWrapper = buildQueryWrapper(config);
PageDomain pageDomain = TableSupport.buildPageRequest();
Page<SysConfigVo> page = this.getMapper().paginateAs(pageDomain.getPageNum(), pageDomain.getPageSize(), queryWrapper,SysConfigVo.class);
Page<SysConfigVo> page = this.pageAs(PageQuery.build(), queryWrapper,SysConfigVo.class);
return TableDataInfo.build(page);
}

View File

@ -12,6 +12,7 @@ import com.ruoyi.common.core.core.page.PageDomain;
import com.ruoyi.common.core.core.page.TableSupport;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.common.redis.utils.CacheUtils;
@ -85,10 +86,7 @@ public class SysDictDataServiceImpl extends BaseServiceImpl<SysDictDataMapper, S
@Override
public TableDataInfo<SysDictDataVo> selectPage(SysDictDataBo dictDataBo) {
QueryWrapper queryWrapper = buildQueryWrapper(dictDataBo);
PageDomain pageDomain = TableSupport.buildPageRequest();
Page<SysDictDataVo> page = this.getMapper().paginateAs(pageDomain.getPageNum(), pageDomain.getPageSize(), queryWrapper, SysDictDataVo.class);
Page<SysDictDataVo> page = this.pageAs(PageQuery.build(), queryWrapper, SysDictDataVo.class);
return TableDataInfo.build(page);
}

View File

@ -18,6 +18,7 @@ import com.ruoyi.common.core.service.DictService;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StreamUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.common.redis.utils.CacheUtils;
@ -105,10 +106,7 @@ public class SysDictTypeServiceImpl extends BaseServiceImpl<SysDictTypeMapper, S
@Override
public TableDataInfo<SysDictTypeVo> selectPage(SysDictTypeBo dictTypeBo) {
QueryWrapper queryWrapper = buildQueryWrapper(dictTypeBo);
PageDomain pageDomain = TableSupport.buildPageRequest();
Page<SysDictTypeVo> page = this.getMapper().paginateAs(pageDomain.getPageNum(), pageDomain.getPageSize(), queryWrapper, SysDictTypeVo.class);
Page<SysDictTypeVo> page = this.pageAs(PageQuery.build(), queryWrapper, SysDictTypeVo.class);
return TableDataInfo.build(page);
}

View File

@ -11,6 +11,7 @@ import com.ruoyi.common.core.core.page.TableSupport;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.sql.SqlUtil;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.system.domain.bo.SysNoticeBo;
@ -98,10 +99,7 @@ public class SysNoticeServiceImpl extends BaseServiceImpl<SysNoticeMapper, SysNo
@Override
public TableDataInfo<SysNoticeVo> selectPage(SysNoticeBo noticeBo) {
QueryWrapper queryWrapper = buildQueryWrapper(noticeBo);
PageDomain pageDomain = TableSupport.buildPageRequest();
Page<SysNoticeVo> page = this.getMapper().paginateAs(pageDomain.getPageNum(), pageDomain.getPageSize(), queryWrapper, SysNoticeVo.class);
Page<SysNoticeVo> page = this.pageAs(PageQuery.build(), queryWrapper, SysNoticeVo.class);
return TableDataInfo.build(page);
}

View File

@ -9,6 +9,7 @@ import com.ruoyi.common.core.core.page.PageDomain;
import com.ruoyi.common.core.core.page.TableSupport;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.sql.SqlUtil;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.system.domain.bo.SysPostBo;
@ -92,10 +93,7 @@ public class SysPostServiceImpl extends BaseServiceImpl<SysPostMapper, SysPost>
@Override
public TableDataInfo<SysPostVo> selectPage(SysPostBo postBo) {
QueryWrapper queryWrapper = buildQueryWrapper(postBo);
PageDomain pageDomain = TableSupport.buildPageRequest();
Page<SysPostVo> page = this.getMapper().paginateAs(pageDomain.getPageNum(), pageDomain.getPageSize(), queryWrapper, SysPostVo.class);
Page<SysPostVo> page = this.pageAs(PageQuery.build(), queryWrapper, SysPostVo.class);
return TableDataInfo.build(page);
}