依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.3</version>
</dependency>
工具类
import com.first.pet.vo.BasePageReq;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
/**
* @author yangquan
* @since 2023-09-18
*/
public class PageInfoUtils {
/**
* pageInfo泛型转换
*/
public static <T, R> PageInfo<R> convert(PageInfo<T> pageInfo, Function<T, R> function) {
Page<T> page = (Page<T>) pageInfo.getList();
PageInfo<R> newPageInfo = page.toPageInfo(function::apply);
newPageInfo.setTotal(page.getTotal());
return newPageInfo;
}
/**
* pageInfo泛型转换
*/
public static <T> PageInfo<T> of(Integer num, Integer size, Long total, List<T> list) {
PageInfo<T> result = new PageInfo<>();
result.setPageNum(num);
result.setPageSize(size);
result.setTotal(total);
result.setList(list);
return result;
}
public static <T> PageInfo<T> emptyPageInfo(BasePageReq basePageReq) {
PageInfo<T> pageInfo = new PageInfo<>();
pageInfo.setPageNum(basePageReq.getPageNum());
pageInfo.setPageSize(basePageReq.getPageSize());
pageInfo.setList(new ArrayList<>());
return pageInfo;
}
}
分页参数
/**
* 资源信息分页查询DTO
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BasePageReq implements Serializable {
@ApiModelProperty("第几页")
@Min(value = 1, message = "分页参数错误,页码不应小于1条")
private Integer pageNum;
@ApiModelProperty("每页显示几条")
@Min(value = 1, message = "分页参数错误,条数不应小于1条")
@Max(value = 1000, message = "分页参数错误,条数应小于1000条")
private Integer pageSize;
}
/**
* @author yangquan
* @since 2023-09-17
*/
@ApiModel("管理员信息查询入参")
@Data
public class PlatformManagerUserReq extends BasePageReq {
/**
* 手机号
*/
@ApiModelProperty("手机号")
private String phone;
/**
* 角色Id
*/
@ApiModelProperty("角色Id")
private Integer roleId;
public PlatformManagerUserReq(Integer pageNum, Integer size, String phone, Integer roleId) {
super(pageNum, size);
this.phone = phone;
this.roleId = roleId;
}
}
写法1、
@Override
public PageInfo<PlatformRole> getPlatformRoleList(PlatformRoleReq platformRoleReq) {
//第一种写法 使用mybatis-plus的写法
QueryWrapper<PlatformRole> roleRespWrapper = new QueryWrapper<>();
roleRespWrapper.eq(StringUtils.isNotBlank(platformRoleReq.getRoleName()), "role_name", platformRoleReq.getRoleName());
//设置当前页以及每页记录数
PageInfo<PlatformRole> pageInfo = PageHelper.startPage(platformRoleReq.getPageNum(), platformRoleReq.getSize()).doSelectPageInfo(() ->
platformRoleMapper.selectList(roleRespWrapper));
第二种写法 使用自己写sql的方式
// PageInfo<PlatformRole> pageInfo = PageHelper.startPage(platformRoleReq.getPageNum(), platformRoleReq.getSize()).doSelectPageInfo(() ->
// platformRoleMapper.selectbyRoleId(platformRoleReq));
if (pageInfo.getTotal() == 0) {
return PageInfoUtils.emptyPageInfo(platformRoleReq.getPageNum(), platformRoleReq.getSize());
}
return pageInfo;
}
写法2、
@Override
public PageInfo<PlatformManagerUserResp> managerUserList(PlatformManagerUserReq platformManagerUserReq) {
if (StringUtils.isNotBlank(platformManagerUserReq.getPhone())) {
platformManagerUserReq.setPhone(EncryptUtils.encryptPhoneIdCard(platformManagerUserReq.getPhone()));
}
//设置当前页以及每页记录数
PageInfo<PlatformManagerUserResp> pageInfo = PageHelper.startPage(platformManagerUserReq.getPageNum(), platformManagerUserReq.getPageSize()).doSelectPageInfo(() ->
platformManagerUserMapper.managerUserList(platformManagerUserReq));
if (pageInfo.getTotal() == 0) {
return PageInfoUtils.emptyPageInfo(platformManagerUserReq);
}
return PageInfoUtils.convert(pageInfo, this::platformManagerUserResp);
}
/**
* 数据转换
*
* @param platformManagerUserResp
* @return
*/
private PlatformManagerUserResp platformManagerUserResp(PlatformManagerUserResp platformManagerUserResp) {
platformManagerUserResp.setPhone(EncryptUtils.decryptPhoneIdCard(platformManagerUserResp.getPhone()));
return platformManagerUserResp;
}
这辈子坚持与不坚持都不可怕,怕的是独自走在坚持的道路上。。。