分页PageVO
package 包名;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* @author 孙永潮
* @create 2022/08/08 15:36
* 这个vo的格式,洋哥(赵洋)和王哥(王健)都是这样写的
*/
@Data
public class PageVO<T> {
@ApiModelProperty(value = "列表")
private List<T> list = new ArrayList<>();
@ApiModelProperty(value = "总条数")
private Long total = 0L;
public PageVO(List<T> list, long total) {
this.list.addAll(list);
this.total += total;
}
}
BasePageDTO
这个是在maven里的公用jar包里的,maven里的jar包也是可以自己写的,
大多数 查询条件DTO需要extends BasePageDTO
package com.gas.base.dto;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotNull;
public class BasePageDTO {
@ApiModelProperty(
value = "当前页",
example = "1",
required = true
)
@NotNull(
message = "当前页条数,值不能为空"
)
private Long current = 1L;
@ApiModelProperty(
value = "当前页条数",
example = "10",
required = true
)
@NotNull(
message = "当前页条数,值不能为空"
)
private Long size = 10L;
public BasePageDTO() {
}
public Long getCurrent() {
return this.current;
}
public Long getSize() {
return this.size;
}
public void setCurrent(final Long current) {
this.current = current;
}
public void setSize(final Long size) {
this.size = size;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof BasePageDTO)) {
return false;
} else {
BasePageDTO other = (BasePageDTO)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$current = this.getCurrent();
Object other$current = other.getCurrent();
if (this$current == null) {
if (other$current != null) {
return false;
}
} else if (!this$current.equals(other$current)) {
return false;
}
Object this$size = this.getSize();
Object other$size = other.getSize();
if (this$size == null) {
if (other$size != null) {
return false;
}
} else if (!this$size.equals(other$size)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof BasePageDTO;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $current = this.getCurrent();
int result = result * 59 + ($current == null ? 43 : $current.hashCode());
Object $size = this.getSize();
result = result * 59 + ($size == null ? 43 : $size.hashCode());
return result;
}
public String toString() {
return "BasePageDTO(current=" + this.getCurrent() + ", size=" + this.getSize() + ")";
}
}
苞米豆的Page
这个baomidou直接引入就行
package com.baomidou.mybatisplus.extension.plugins.pagination;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
public class Page<T> implements IPage<T> {
private static final long serialVersionUID = 8545996863226528798L;
protected List<T> records;
protected long total;
protected long size;
protected long current;
protected List<OrderItem> orders;
protected boolean optimizeCountSql;
protected boolean searchCount;
protected boolean optimizeJoinOfCountSql;
protected String countId;
protected Long maxLimit;
//第28行代码
//这里省略了很多代码
//第240行代码
}
分页查询Controller
/**
* 计量组(Metering) Controller
*
* @author 孙永潮
*/
@Api(tags = {"计量组"})
@Slf4j
@Validated
@RestController
@RequestMapping("metering")
public class MeteringController{
@Resource
private MeteringService service;
@ApiOperation(value = "分页查询", notes="metering-page")
@GetMapping("/page")
//我这里是计量组的分页
@SaCheckPermission("metering-page")
public R<PageVO<返回值VO>> page(@Validated 查询条件DTO dto) {
PageVO<返回值VO> page = service.findPage(dto);
return R.ok(page);
}
}
Optional.ofNullable
ServiceImpl
/**
* 计量组(Metering) Impl
*
* @author 孙永潮
*/
@Service
public class MeteringServiceImpl extends ServiceImpl<MeteringMapper, Metering> implements MeteringService {
//计量组
@Resource
private MeteringMapper mapper;
//计量组设备详情
@Resource
private MeteringDetailsMapper detailsMapper;
/**
* 分页查询
*/
@Override
public PageVO<返回值VO> findPage(查询条件DTO dto) {
//如果查询条件的DTO 没有强制 让前端传页数 和查询条数,可以用下边两行代码
Long current = Optional.ofNullable(sysUserDeptDTO.getCurrent()).orElse(1L);
Long size = Optional.ofNullable(sysUserDeptDTO.getSize()).orElse(10L);
//执行查询
Page<返回值VO> page = baseMapper.findPage(new Page(dto.getCurrent(),dto.getSize()),dto);
//page.getRecords就是我们要查的list
List<返回值VO> list = page.getRecords();
//page.getTotal()其实就是总条数
long total = page.getTotal();
//用枚举赋值
list.forEach(e -> e.setEqTypeName(MeteringEnum.name(e.getEqType())));
return new PageVO(list, total );
}
Mapper
这里写这个mapper,主要是让自己别忘了把分页的参数传过来
//分页查询
Page<返回值VO> findPage(Page page, @Param("model") 查询条件DTO dto);