MyBatis-Plus中分页插件IPage的使用

MyBatis-Plus中分页插件IPage的使用

使用步骤:

1.服务层的接口需要继承 IService<实体类> ,定义分页查询方法,其返回值类型是 IPage<实体类> .

2.服务的实现类要继承 ServiceImpl< Mapper接口类,实体类 > ,重写分页查询方法.

3.可以定义一个Page类

controller:

@PostMapping("/getBomPage")
@ApiOperation(value = "BOM列表分页查询", notes = "BOM列表分页查询")
public IPage<BomDetailPageDTO> getBomPage(@RequestBody PageParams<ConditionBomDetailDTO> params) {
     return baseService.getBomPage(params);
}

service:

/**
* BOM列表分页查询
* @param params 分页参数
* @return 结果
*/
IPage<BomDetailPageDTO> getBomPage(PageParams<ConditionBomDetailDTO> params);

serviceImpl:

@Override
 public IPage<BomDetailPageDTO> getBomPage(PageParams<ConditionBomDetailDTO> params) {
     IPage<BomBasicDTO> page = params.buildPage();
     ConditionBomDetailDTO model = params.getModel();
     return carTaiZhangMapper.getBomPage(page, model);
 }

说明:carTaiZhangMapper为对应的Mapper接口类,getListPage为自定义的分页查询方法。

在对应的Mapper接口类(如上述的carTaiZhangMapper)继承 BaseMapper<实体类>.
mapper:

/**
 * BOM列表分页查询
 * @param page 入参
 * @param model 入参
 * @return 查询结果
 */
IPage<BomDetailPageDTO> getBomPage(IPage<BomBasicDTO> page, @Param("model") ConditionBomDetailDTO model);

编写映射文件Mapper.xml,创建对应方法的SQL语句:

<select id="getBomPage" resultMap="getBomPageMap">
     select *
     from view_bom_data
     <where>
         1=1
         <if test="model.projectId != null">
             and project_id=${model.projectId }
         </if>
     </where>
 </select>

PageParams.java


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dalezhuang.basic.base.entity.SuperEntity;
import com.dalezhuang.basic.database.mybatis.conditions.Wraps;
import com.dalezhuang.basic.utils.StrPool;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页参数
 *
 */
@Data
@NoArgsConstructor
@ApiModel(value = "PageParams", description = "分页参数")
public class PageParams<T> {

    @NotNull(message = "查询对象model不能为空")
    @ApiModelProperty(value = "查询参数", required = true)
    private T model;

    @ApiModelProperty(value = "页面大小", example = "10")
    private long size = 10;

    @ApiModelProperty(value = "当前页", example = "1")
    private long current = 1;

    @ApiModelProperty(value = "排序,默认createTime", allowableValues = "id,createTime,updateTime", example = "id")
    private String sort = SuperEntity.FIELD_ID;

    @ApiModelProperty(value = "排序规则, 默认descending", allowableValues = "descending,ascending", example = "descending")
    private String order = "descending";

    @ApiModelProperty("扩展参数")
    private Map<String, Object> extra = new HashMap<>(16);

    public PageParams(long current, long size) {
        this.size = size;
        this.current = current;
    }

    /**
     * 构建分页对象
     *
     * @return 分页对象
     */
    @JsonIgnore
    public <E> IPage<E> buildPage() {
        PageParams params = this;
        return new Page(params.getCurrent(), params.getSize());
    }

    /**
     * 构建分页对象
     * <p>
     * 支持多个字段排序,用法:
     * eg.1, 参数:{order:"name,id", order:"descending,ascending" }。 排序: name desc, id asc
     * eg.2, 参数:{order:"name", order:"descending,ascending" }。 排序: name desc
     * eg.3, 参数:{order:"name,id", order:"descending" }。 排序: name desc
     *
     * @param entityClazz 字段中标注了@TableName 或 @TableId 注解的实体类。
     * @return 分页对象
     * @since 3.5.0
     */
    @JsonIgnore
    public <E> IPage<E> buildPage(Class<?> entityClazz) {
        PageParams params = this;
        //没有排序参数
        if (StrUtil.isEmpty(params.getSort())) {
            return new Page(params.getCurrent(), params.getSize());
        }

        Page<E> page = new Page(params.getCurrent(), params.getSize());

        List<OrderItem> orders = new ArrayList<>();
        String[] sortArr = StrUtil.splitToArray(params.getSort(), StrPool.COMMA);
        String[] orderArr = StrUtil.splitToArray(params.getOrder(), StrPool.COMMA);

        int len = Math.min(sortArr.length, orderArr.length);
        for (int i = 0; i < len; i++) {
            String humpSort = sortArr[i];
            // 简单的 驼峰 转 下划线
            String underlineSort = Wraps.getDbField(humpSort, entityClazz);
            orders.add(StrUtil.equalsAny(orderArr[i], "ascending", "ascend") ? OrderItem.asc(underlineSort) : OrderItem.desc(underlineSort));
        }

        page.setOrders(orders);

        return page;
    }

    /**
     * 计算当前分页偏移量
     */
    @JsonIgnore
    public long offset() {
        long current = this.current;
        if (current <= 1L) {
            return 0L;
        }
        return (current - 1) * this.size;
    }

    @JsonIgnore
    public PageParams<T> put(String key, Object value) {
        if (this.extra == null) {
            this.extra = new HashMap<>(16);
        }
        this.extra.put(key, value);
        return this;
    }

    @JsonIgnore
    public PageParams<T> putAll(Map<String, Object> extra) {
        if (this.extra == null) {
            this.extra = new HashMap<>(16);
        }
        this.extra.putAll(extra);
        return this;
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大胖东

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值