使用步骤:
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;
}
}